LCOV - code coverage report
Current view: top level - gcc - omp-low.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.7 % 9494 8892
Test Date: 2026-12-05 16:10:02 Functions: 94.9 % 136 129
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Lowering pass for OMP directives.  Converts OMP directives into explicit
       2              :    calls to the runtime library (libgomp), data marshalling to implement data
       3              :    sharing and copying clauses, offloading to accelerators, and more.
       4              : 
       5              :    Contributed by Diego Novillo <dnovillo@redhat.com>
       6              : 
       7              :    Copyright (C) 2005-2026 Free Software Foundation, Inc.
       8              : 
       9              : This file is part of GCC.
      10              : 
      11              : GCC is free software; you can redistribute it and/or modify it under
      12              : the terms of the GNU General Public License as published by the Free
      13              : Software Foundation; either version 3, or (at your option) any later
      14              : version.
      15              : 
      16              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      17              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      18              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      19              : for more details.
      20              : 
      21              : You should have received a copy of the GNU General Public License
      22              : along with GCC; see the file COPYING3.  If not see
      23              : <http://www.gnu.org/licenses/>.  */
      24              : 
      25              : #include "config.h"
      26              : #include "system.h"
      27              : #include "coretypes.h"
      28              : #include "backend.h"
      29              : #include "target.h"
      30              : #include "tree.h"
      31              : #include "gimple.h"
      32              : #include "tree-pass.h"
      33              : #include "ssa.h"
      34              : #include "cgraph.h"
      35              : #include "pretty-print.h"
      36              : #include "diagnostic-core.h"
      37              : #include "fold-const.h"
      38              : #include "stor-layout.h"
      39              : #include "internal-fn.h"
      40              : #include "gimple-iterator.h"
      41              : #include "gimple-fold.h"
      42              : #include "gimplify.h"
      43              : #include "gimplify-me.h"
      44              : #include "gimple-walk.h"
      45              : #include "tree-iterator.h"
      46              : #include "tree-inline.h"
      47              : #include "langhooks.h"
      48              : #include "tree-dfa.h"
      49              : #include "tree-ssa.h"
      50              : #include "splay-tree.h"
      51              : #include "omp-general.h"
      52              : #include "omp-low.h"
      53              : #include "gimple-low.h"
      54              : #include "alloc-pool.h"
      55              : #include "symbol-summary.h"
      56              : #include "tree-nested.h"
      57              : #include "context.h"
      58              : #include "gomp-constants.h"
      59              : #include "gimple-pretty-print.h"
      60              : #include "stringpool.h"
      61              : #include "attribs.h"
      62              : #include "omp-offload.h"
      63              : 
      64              : /* Lowering of OMP parallel and workshare constructs proceeds in two
      65              :    phases.  The first phase scans the function looking for OMP statements
      66              :    and then for variables that must be replaced to satisfy data sharing
      67              :    clauses.  The second phase expands code for the constructs, as well as
      68              :    re-gimplifying things when variables have been replaced with complex
      69              :    expressions.
      70              : 
      71              :    Final code generation is done by pass_expand_omp.  The flowgraph is
      72              :    scanned for regions which are then moved to a new
      73              :    function, to be invoked by the thread library, or offloaded.  */
      74              : 
      75              : /* Context structure.  Used to store information about each parallel
      76              :    directive in the code.  */
      77              : 
      78              : struct omp_context
      79              : {
      80              :   /* This field must be at the beginning, as we do "inheritance": Some
      81              :      callback functions for tree-inline.cc (e.g., omp_copy_decl)
      82              :      receive a copy_body_data pointer that is up-casted to an
      83              :      omp_context pointer.  */
      84              :   copy_body_data cb;
      85              : 
      86              :   /* The tree of contexts corresponding to the encountered constructs.  */
      87              :   struct omp_context *outer;
      88              :   gimple *stmt;
      89              : 
      90              :   /* Map variables to fields in a structure that allows communication
      91              :      between sending and receiving threads.  */
      92              :   splay_tree field_map;
      93              :   tree record_type;
      94              :   tree sender_decl;
      95              :   tree receiver_decl;
      96              : 
      97              :   /* These are used just by task contexts, if task firstprivate fn is
      98              :      needed.  srecord_type is used to communicate from the thread
      99              :      that encountered the task construct to task firstprivate fn,
     100              :      record_type is allocated by GOMP_task, initialized by task firstprivate
     101              :      fn and passed to the task body fn.  */
     102              :   splay_tree sfield_map;
     103              :   tree srecord_type;
     104              : 
     105              :   /* A chain of variables to add to the top-level block surrounding the
     106              :      construct.  In the case of a parallel, this is in the child function.  */
     107              :   tree block_vars;
     108              : 
     109              :   /* Label to which GOMP_cancel{,llation_point} and explicit and implicit
     110              :      barriers should jump to during omplower pass.  */
     111              :   tree cancel_label;
     112              : 
     113              :   /* The sibling GIMPLE_OMP_FOR simd with _simt_ clause or NULL
     114              :      otherwise.  */
     115              :   gimple *simt_stmt;
     116              : 
     117              :   /* For task reductions registered in this context, a vector containing
     118              :      the length of the private copies block (if constant, otherwise NULL)
     119              :      and then offsets (if constant, otherwise NULL) for each entry.  */
     120              :   vec<tree> task_reductions;
     121              : 
     122              :   /* A hash map from the reduction clauses to the registered array
     123              :      elts.  */
     124              :   hash_map<tree, unsigned> *task_reduction_map;
     125              : 
     126              :   /* And a hash map from the lastprivate(conditional:) variables to their
     127              :      corresponding tracking loop iteration variables.  */
     128              :   hash_map<tree, tree> *lastprivate_conditional_map;
     129              : 
     130              :   /* And a hash map from the lastprivate iteration variables to their
     131              :      corresponding loop index, only used for SIMD loops with SIMT.  */
     132              :   hash_map<tree, int> *lastprivate_iv_map;
     133              : 
     134              :   /* And a hash map from the allocate variables to their corresponding
     135              :      allocators.  */
     136              :   hash_map<tree, tree> *allocate_map;
     137              : 
     138              :   /* A tree_list of the reduction clauses in this context. This is
     139              :     only used for checking the consistency of OpenACC reduction
     140              :     clauses in scan_omp_for and is not guaranteed to contain a valid
     141              :     value outside of this function. */
     142              :   tree local_reduction_clauses;
     143              : 
     144              :   /* A tree_list of the reduction clauses in outer contexts. This is
     145              :     only used for checking the consistency of OpenACC reduction
     146              :     clauses in scan_omp_for and is not guaranteed to contain a valid
     147              :     value outside of this function. */
     148              :   tree outer_reduction_clauses;
     149              : 
     150              :   /* Nesting depth of this context.  Used to beautify error messages re
     151              :      invalid gotos.  The outermost ctx is depth 1, with depth 0 being
     152              :      reserved for the main body of the function.  */
     153              :   int depth;
     154              : 
     155              :   /* True if this parallel directive is nested within another.  */
     156              :   bool is_nested;
     157              : 
     158              :   /* True if this construct can be cancelled.  */
     159              :   bool cancellable;
     160              : 
     161              :   /* True if lower_omp_1 should look up lastprivate conditional in parent
     162              :      context.  */
     163              :   bool combined_into_simd_safelen1;
     164              : 
     165              :   /* True if there is nested scan context with inclusive clause.  */
     166              :   bool scan_inclusive;
     167              : 
     168              :   /* True if there is nested scan context with exclusive clause.  */
     169              :   bool scan_exclusive;
     170              : 
     171              :   /* True in the second simd loop of for simd with inscan reductions.  */
     172              :   bool for_simd_scan_phase;
     173              : 
     174              :   /* True if there is order(concurrent) clause on the construct.  */
     175              :   bool order_concurrent;
     176              : 
     177              :   /* True if there is bind clause on the construct (i.e. a loop construct).  */
     178              :   bool loop_p;
     179              : 
     180              :   /* Only used for omp target contexts.  True if a teams construct is
     181              :      strictly nested in it.  */
     182              :   bool teams_nested_p;
     183              : 
     184              :   /* Only used for omp target contexts.  True if an OpenMP construct other
     185              :      than teams is strictly nested in it.  */
     186              :   bool nonteams_nested_p;
     187              : 
     188              :   /* Candidates for adjusting OpenACC privatization level.  */
     189              :   vec<tree> oacc_privatization_candidates;
     190              : };
     191              : 
     192              : static splay_tree all_contexts;
     193              : static int taskreg_nesting_level;
     194              : static int target_nesting_level;
     195              : static bitmap make_addressable_vars;
     196              : static bitmap global_nonaddressable_vars;
     197              : static vec<omp_context *> taskreg_contexts;
     198              : static vec<gomp_task *> task_cpyfns;
     199              : 
     200              : static void scan_omp (gimple_seq *, omp_context *);
     201              : static tree scan_omp_1_op (tree *, int *, void *);
     202              : static bool omp_maybe_offloaded_ctx (omp_context *ctx);
     203              : 
     204              : #define WALK_SUBSTMTS  \
     205              :     case GIMPLE_BIND: \
     206              :     case GIMPLE_TRY: \
     207              :     case GIMPLE_CATCH: \
     208              :     case GIMPLE_EH_FILTER: \
     209              :     case GIMPLE_ASSUME: \
     210              :     case GIMPLE_TRANSACTION: \
     211              :       /* The sub-statements for these should be walked.  */ \
     212              :       *handled_ops_p = false; \
     213              :       break;
     214              : 
     215              : /* Return whether CTX represents an OpenACC 'parallel' or 'serial' construct.
     216              :    (This doesn't include OpenACC 'kernels' decomposed parts.)  */
     217              : 
     218              : static bool
     219        34266 : is_oacc_parallel_or_serial (omp_context *ctx)
     220              : {
     221        34266 :   enum gimple_code outer_type = gimple_code (ctx->stmt);
     222        34266 :   return ((outer_type == GIMPLE_OMP_TARGET)
     223        34266 :           && ((gimple_omp_target_kind (ctx->stmt)
     224              :                == GF_OMP_TARGET_KIND_OACC_PARALLEL)
     225        15924 :               || (gimple_omp_target_kind (ctx->stmt)
     226        34266 :                   == GF_OMP_TARGET_KIND_OACC_SERIAL)));
     227              : }
     228              : 
     229              : /* Return whether CTX represents an OpenACC 'kernels' construct.
     230              :    (This doesn't include OpenACC 'kernels' decomposed parts.)  */
     231              : 
     232              : static bool
     233        49900 : is_oacc_kernels (omp_context *ctx)
     234              : {
     235        49900 :   enum gimple_code outer_type = gimple_code (ctx->stmt);
     236        49900 :   return ((outer_type == GIMPLE_OMP_TARGET)
     237        49900 :           && (gimple_omp_target_kind (ctx->stmt)
     238        49900 :               == GF_OMP_TARGET_KIND_OACC_KERNELS));
     239              : }
     240              : 
     241              : /* Return whether CTX represents an OpenACC 'kernels' decomposed part.  */
     242              : 
     243              : static bool
     244        21645 : is_oacc_kernels_decomposed_part (omp_context *ctx)
     245              : {
     246        21645 :   enum gimple_code outer_type = gimple_code (ctx->stmt);
     247        21645 :   return ((outer_type == GIMPLE_OMP_TARGET)
     248        21645 :           && ((gimple_omp_target_kind (ctx->stmt)
     249              :                == GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED)
     250        14552 :               || (gimple_omp_target_kind (ctx->stmt)
     251              :                   == GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE)
     252        14552 :               || (gimple_omp_target_kind (ctx->stmt)
     253        21645 :                   == GF_OMP_TARGET_KIND_OACC_DATA_KERNELS)));
     254              : }
     255              : 
     256              : /* Return true if STMT corresponds to an OpenMP target region.  */
     257              : static bool
     258        44713 : is_omp_target (gimple *stmt)
     259              : {
     260        44713 :   if (gimple_code (stmt) == GIMPLE_OMP_TARGET)
     261              :     {
     262         6523 :       int kind = gimple_omp_target_kind (stmt);
     263         6523 :       return (kind == GF_OMP_TARGET_KIND_REGION
     264         6523 :               || kind == GF_OMP_TARGET_KIND_DATA
     265         6523 :               || kind == GF_OMP_TARGET_KIND_ENTER_DATA
     266         6523 :               || kind == GF_OMP_TARGET_KIND_EXIT_DATA);
     267              :     }
     268              :   return false;
     269              : }
     270              : 
     271              : /* If DECL is the artificial dummy VAR_DECL created for non-static
     272              :    data member privatization, return the underlying "this" parameter,
     273              :    otherwise return NULL.  */
     274              : 
     275              : tree
     276       562292 : omp_member_access_dummy_var (tree decl)
     277              : {
     278       562292 :   if (!VAR_P (decl)
     279       349073 :       || !DECL_ARTIFICIAL (decl)
     280       172024 :       || !DECL_IGNORED_P (decl)
     281       165935 :       || !DECL_HAS_VALUE_EXPR_P (decl)
     282       575581 :       || !lang_hooks.decls.omp_disregard_value_expr (decl, false))
     283              :     return NULL_TREE;
     284              : 
     285         2260 :   tree v = DECL_VALUE_EXPR (decl);
     286         2260 :   if (TREE_CODE (v) != COMPONENT_REF)
     287              :     return NULL_TREE;
     288              : 
     289        16164 :   while (1)
     290         9197 :     switch (TREE_CODE (v))
     291              :       {
     292         6967 :       case COMPONENT_REF:
     293         6967 :       case MEM_REF:
     294         6967 :       case INDIRECT_REF:
     295         6967 :       CASE_CONVERT:
     296         6967 :       case POINTER_PLUS_EXPR:
     297         6967 :         v = TREE_OPERAND (v, 0);
     298         6967 :         continue;
     299         2211 :       case PARM_DECL:
     300         2211 :         if (DECL_CONTEXT (v) == current_function_decl
     301         2211 :             && DECL_ARTIFICIAL (v)
     302         4422 :             && TREE_CODE (TREE_TYPE (v)) == POINTER_TYPE)
     303         2211 :           return v;
     304              :         return NULL_TREE;
     305              :       default:
     306              :         return NULL_TREE;
     307              :       }
     308              : }
     309              : 
     310              : /* Helper for unshare_and_remap, called through walk_tree.  */
     311              : 
     312              : static tree
     313         1579 : unshare_and_remap_1 (tree *tp, int *walk_subtrees, void *data)
     314              : {
     315         1579 :   tree *pair = (tree *) data;
     316         1579 :   if (*tp == pair[0])
     317              :     {
     318          233 :       *tp = unshare_expr (pair[1]);
     319          233 :       *walk_subtrees = 0;
     320              :     }
     321         1346 :   else if (IS_TYPE_OR_DECL_P (*tp))
     322          244 :     *walk_subtrees = 0;
     323         1579 :   return NULL_TREE;
     324              : }
     325              : 
     326              : /* Return unshare_expr (X) with all occurrences of FROM
     327              :    replaced with TO.  */
     328              : 
     329              : static tree
     330          150 : unshare_and_remap (tree x, tree from, tree to)
     331              : {
     332          150 :   tree pair[2] = { from, to };
     333          150 :   x = unshare_expr (x);
     334          150 :   walk_tree (&x, unshare_and_remap_1, pair, NULL);
     335          150 :   return x;
     336              : }
     337              : 
     338              : /* Convenience function for calling scan_omp_1_op on tree operands.  */
     339              : 
     340              : static inline tree
     341       359829 : scan_omp_op (tree *tp, omp_context *ctx)
     342              : {
     343       359829 :   struct walk_stmt_info wi;
     344              : 
     345       359829 :   memset (&wi, 0, sizeof (wi));
     346       359829 :   wi.info = ctx;
     347       359829 :   wi.want_locations = true;
     348              : 
     349       359829 :   return walk_tree (tp, scan_omp_1_op, &wi, NULL);
     350              : }
     351              : 
     352              : static void lower_omp (gimple_seq *, omp_context *);
     353              : static tree lookup_decl_in_outer_ctx (tree, omp_context *);
     354              : static tree maybe_lookup_decl_in_outer_ctx (tree, omp_context *);
     355              : 
     356              : /* Return true if CTX is for an omp parallel.  */
     357              : 
     358              : static inline bool
     359       818898 : is_parallel_ctx (omp_context *ctx)
     360              : {
     361        64682 :   return gimple_code (ctx->stmt) == GIMPLE_OMP_PARALLEL;
     362              : }
     363              : 
     364              : 
     365              : /* Return true if CTX is for an omp task.  */
     366              : 
     367              : static inline bool
     368       246123 : is_task_ctx (omp_context *ctx)
     369              : {
     370        40728 :   return gimple_code (ctx->stmt) == GIMPLE_OMP_TASK;
     371              : }
     372              : 
     373              : 
     374              : /* Return true if CTX is for an omp taskloop.  */
     375              : 
     376              : static inline bool
     377         8461 : is_taskloop_ctx (omp_context *ctx)
     378              : {
     379         8461 :   return gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
     380         8461 :          && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_TASKLOOP;
     381              : }
     382              : 
     383              : 
     384              : /* Return true if CTX is for a host omp teams.  */
     385              : 
     386              : static inline bool
     387       552189 : is_host_teams_ctx (omp_context *ctx)
     388              : {
     389       552189 :   return gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
     390       552189 :          && gimple_omp_teams_host (as_a <gomp_teams *> (ctx->stmt));
     391              : }
     392              : 
     393              : /* Return true if CTX is for an omp parallel or omp task or host omp teams
     394              :    (the last one is strictly not a task region in OpenMP speak, but we
     395              :    need to treat it similarly).  */
     396              : 
     397              : static inline bool
     398       699815 : is_taskreg_ctx (omp_context *ctx)
     399              : {
     400       699815 :   return is_parallel_ctx (ctx) || is_task_ctx (ctx) || is_host_teams_ctx (ctx);
     401              : }
     402              : 
     403              : /* Return true if EXPR is variable sized.  If IS_REF is true, then
     404              :    EXPR is assumed to be a reference and the object it refers
     405              :    to is checked instead.  */
     406              : 
     407              : static inline bool
     408       965298 : is_variable_sized (const_tree expr, bool is_ref = false)
     409              : {
     410       965298 :   if (is_ref)
     411         2220 :     expr = TREE_TYPE (expr);
     412       965298 :   return !TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
     413              : }
     414              : 
     415              : /* Lookup variables.  The "maybe" form
     416              :    allows for the variable form to not have been entered, otherwise we
     417              :    assert that the variable must have been entered.  */
     418              : 
     419              : static inline tree
     420       760027 : lookup_decl (tree var, omp_context *ctx)
     421              : {
     422      1519945 :   tree *n = ctx->cb.decl_map->get (var);
     423       760027 :   return *n;
     424              : }
     425              : 
     426              : static inline tree
     427       761965 : maybe_lookup_decl (const_tree var, omp_context *ctx)
     428              : {
     429       761965 :   tree *n = ctx->cb.decl_map->get (const_cast<tree> (var));
     430       761965 :   return n ? *n : NULL_TREE;
     431              : }
     432              : 
     433              : static inline tree
     434       132908 : lookup_field (tree var, omp_context *ctx)
     435              : {
     436       132908 :   splay_tree_node n;
     437       265816 :   n = splay_tree_lookup (ctx->field_map, (splay_tree_key) var);
     438       132908 :   return (tree) n->value;
     439              : }
     440              : 
     441              : static inline tree
     442       176651 : lookup_sfield (splay_tree_key key, omp_context *ctx)
     443              : {
     444       176651 :   splay_tree_node n;
     445       176651 :   n = splay_tree_lookup (ctx->sfield_map
     446              :                          ? ctx->sfield_map : ctx->field_map, key);
     447       176651 :   return (tree) n->value;
     448              : }
     449              : 
     450              : static inline tree
     451          969 : lookup_sfield (tree var, omp_context *ctx)
     452              : {
     453          969 :   return lookup_sfield ((splay_tree_key) var, ctx);
     454              : }
     455              : 
     456              : static inline tree
     457       233269 : maybe_lookup_field (splay_tree_key key, omp_context *ctx)
     458              : {
     459       233269 :   splay_tree_node n;
     460       233269 :   n = splay_tree_lookup (ctx->field_map, key);
     461       233269 :   return n ? (tree) n->value : NULL_TREE;
     462              : }
     463              : 
     464              : static inline tree
     465       233269 : maybe_lookup_field (tree var, omp_context *ctx)
     466              : {
     467         2555 :   return maybe_lookup_field ((splay_tree_key) var, ctx);
     468              : }
     469              : 
     470              : /* Return true if DECL should be copied by pointer.  SHARED_CTX is
     471              :    the parallel context if DECL is to be shared.  */
     472              : 
     473              : static bool
     474       242938 : use_pointer_for_field (tree decl, omp_context *shared_ctx)
     475              : {
     476       472620 :   if (AGGREGATE_TYPE_P (TREE_TYPE (decl))
     477       224284 :       || TYPE_ATOMIC (TREE_TYPE (decl))
     478       467222 :       || POLY_INT_CST_P (DECL_SIZE (decl)))
     479              :     return true;
     480              : 
     481              :   /* We can only use copy-in/copy-out semantics for shared variables
     482              :      when we know the value is not accessible from an outer scope.  */
     483       224254 :   if (shared_ctx)
     484              :     {
     485        30372 :       gcc_assert (!is_gimple_omp_oacc (shared_ctx->stmt));
     486              : 
     487              :       /* ??? Trivially accessible from anywhere.  But why would we even
     488              :          be passing an address in this case?  Should we simply assert
     489              :          this to be false, or should we have a cleanup pass that removes
     490              :          these from the list of mappings?  */
     491        30372 :       if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, shared_ctx)))
     492              :         return true;
     493              : 
     494              :       /* For variables with DECL_HAS_VALUE_EXPR_P set, we cannot tell
     495              :          without analyzing the expression whether or not its location
     496              :          is accessible to anyone else.  In the case of nested parallel
     497              :          regions it certainly may be.  */
     498        30372 :       if (TREE_CODE (decl) != RESULT_DECL && DECL_HAS_VALUE_EXPR_P (decl))
     499              :         return true;
     500              : 
     501              :       /* Do not use copy-in/copy-out for variables that have their
     502              :          address taken.  */
     503        29296 :       if (is_global_var (decl))
     504              :         {
     505              :           /* For file scope vars, track whether we've seen them as
     506              :              non-addressable initially and in that case, keep the same
     507              :              answer for the duration of the pass, even when they are made
     508              :              addressable later on e.g. through reduction expansion.  Global
     509              :              variables which weren't addressable before the pass will not
     510              :              have their privatized copies address taken.  See PR91216.  */
     511         1295 :           if (!TREE_ADDRESSABLE (decl))
     512              :             {
     513          480 :               if (!global_nonaddressable_vars)
     514           75 :                 global_nonaddressable_vars = BITMAP_ALLOC (NULL);
     515          480 :               bitmap_set_bit (global_nonaddressable_vars, DECL_UID (decl));
     516              :             }
     517          815 :           else if (!global_nonaddressable_vars
     518         1050 :                    || !bitmap_bit_p (global_nonaddressable_vars,
     519          235 :                                      DECL_UID (decl)))
     520              :             return true;
     521              :         }
     522        28001 :       else if (TREE_ADDRESSABLE (decl))
     523              :         return true;
     524              : 
     525              :       /* lower_send_shared_vars only uses copy-in, but not copy-out
     526              :          for these.  */
     527        18853 :       if (TREE_READONLY (decl)
     528        18853 :           || ((TREE_CODE (decl) == RESULT_DECL
     529        12020 :                || TREE_CODE (decl) == PARM_DECL)
     530          673 :               && DECL_BY_REFERENCE (decl)))
     531              :         return false;
     532              : 
     533              :       /* Disallow copy-in/out in nested parallel if
     534              :          decl is shared in outer parallel, otherwise
     535              :          each thread could store the shared variable
     536              :          in its own copy-in location, making the
     537              :          variable no longer really shared.  */
     538        11788 :       if (shared_ctx->is_nested)
     539              :         {
     540         2494 :           omp_context *up;
     541              : 
     542         6057 :           for (up = shared_ctx->outer; up; up = up->outer)
     543         5765 :             if ((is_taskreg_ctx (up)
     544         3700 :                  || (gimple_code (up->stmt) == GIMPLE_OMP_TARGET
     545          429 :                      && is_gimple_omp_offloaded (up->stmt)))
     546         6194 :                 && maybe_lookup_decl (decl, up))
     547              :               break;
     548              : 
     549         2494 :           if (up)
     550              :             {
     551         2202 :               tree c;
     552              : 
     553         2202 :               if (gimple_code (up->stmt) == GIMPLE_OMP_TARGET)
     554              :                 {
     555          309 :                   for (c = gimple_omp_target_clauses (up->stmt);
     556         2031 :                        c; c = OMP_CLAUSE_CHAIN (c))
     557         1932 :                     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
     558         1932 :                         && OMP_CLAUSE_DECL (c) == decl)
     559              :                       break;
     560              :                 }
     561              :               else
     562         1893 :                 for (c = gimple_omp_taskreg_clauses (up->stmt);
     563         6925 :                      c; c = OMP_CLAUSE_CHAIN (c))
     564         6468 :                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
     565         6468 :                       && OMP_CLAUSE_DECL (c) == decl)
     566              :                     break;
     567              : 
     568         2202 :               if (c)
     569         1646 :                 goto maybe_mark_addressable_and_ret;
     570              :             }
     571              :         }
     572              : 
     573              :       /* For tasks avoid using copy-in/out.  As tasks can be
     574              :          deferred or executed in different thread, when GOMP_task
     575              :          returns, the task hasn't necessarily terminated.  */
     576        10142 :       if (is_task_ctx (shared_ctx))
     577              :         {
     578         2201 :           tree outer;
     579          555 :         maybe_mark_addressable_and_ret:
     580         2201 :           outer = maybe_lookup_decl_in_outer_ctx (decl, shared_ctx);
     581         2201 :           if (is_gimple_reg (outer) && !omp_member_access_dummy_var (outer))
     582              :             {
     583              :               /* Taking address of OUTER in lower_send_shared_vars
     584              :                  might need regimplification of everything that uses the
     585              :                  variable.  */
     586          986 :               if (!make_addressable_vars)
     587          493 :                 make_addressable_vars = BITMAP_ALLOC (NULL);
     588          986 :               bitmap_set_bit (make_addressable_vars, DECL_UID (outer));
     589          986 :               TREE_ADDRESSABLE (outer) = 1;
     590              :             }
     591              :           return true;
     592              :         }
     593              :     }
     594              : 
     595              :   return false;
     596              : }
     597              : 
     598              : /* Construct a new automatic decl similar to VAR.  */
     599              : 
     600              : static tree
     601       243743 : omp_copy_decl_2 (tree var, tree name, tree type, omp_context *ctx)
     602              : {
     603       243743 :   tree copy = copy_var_decl (var, name, type);
     604              : 
     605       243743 :   DECL_CONTEXT (copy) = current_function_decl;
     606              : 
     607       243743 :   if (ctx)
     608              :     {
     609       243462 :       DECL_CHAIN (copy) = ctx->block_vars;
     610       243462 :       ctx->block_vars = copy;
     611              :     }
     612              :   else
     613          281 :     record_vars (copy);
     614              : 
     615              :   /* If VAR is listed in make_addressable_vars, it wasn't
     616              :      originally addressable, but was only later made so.
     617              :      We don't need to take address of privatizations
     618              :      from that var.  */
     619       243743 :   if (TREE_ADDRESSABLE (var)
     620       243743 :       && ((make_addressable_vars
     621         3470 :            && bitmap_bit_p (make_addressable_vars, DECL_UID (var)))
     622        43495 :           || (global_nonaddressable_vars
     623          515 :               && bitmap_bit_p (global_nonaddressable_vars, DECL_UID (var)))))
     624         2079 :     TREE_ADDRESSABLE (copy) = 0;
     625              : 
     626       243743 :   return copy;
     627              : }
     628              : 
     629              : static tree
     630       243743 : omp_copy_decl_1 (tree var, omp_context *ctx)
     631              : {
     632       243743 :   return omp_copy_decl_2 (var, DECL_NAME (var), TREE_TYPE (var), ctx);
     633              : }
     634              : 
     635              : /* Build tree nodes to access the field for VAR on the receiver side.  */
     636              : 
     637              : static tree
     638       128446 : build_receiver_ref (tree var, bool by_ref, omp_context *ctx)
     639              : {
     640       128446 :   tree x, field = lookup_field (var, ctx);
     641              : 
     642              :   /* If the receiver record type was remapped in the child function,
     643              :      remap the field into the new record type.  */
     644       128446 :   x = maybe_lookup_field (field, ctx);
     645       128446 :   if (x != NULL)
     646        11280 :     field = x;
     647              : 
     648       128446 :   x = build_simple_mem_ref (ctx->receiver_decl);
     649       128446 :   TREE_THIS_NOTRAP (x) = 1;
     650       128446 :   x = omp_build_component_ref (x, field);
     651       128446 :   if (by_ref)
     652              :     {
     653        37898 :       x = build_simple_mem_ref (x);
     654        37898 :       TREE_THIS_NOTRAP (x) = 1;
     655              :     }
     656              : 
     657       128446 :   return x;
     658              : }
     659              : 
     660              : /* Build tree nodes to access VAR in the scope outer to CTX.  In the case
     661              :    of a parallel, this is a component reference; for workshare constructs
     662              :    this is some variable.  */
     663              : 
     664              : static tree
     665       120990 : build_outer_var_ref (tree var, omp_context *ctx,
     666              :                      enum omp_clause_code code = OMP_CLAUSE_ERROR)
     667              : {
     668       120990 :   tree x;
     669       120990 :   omp_context *outer = ctx->outer;
     670       121379 :   for (; outer; outer = outer->outer)
     671              :     {
     672        88633 :       if (gimple_code (outer->stmt) == GIMPLE_OMP_TASKGROUP)
     673          373 :         continue;
     674        88276 :       if (gimple_code (outer->stmt) == GIMPLE_OMP_SCOPE
     675        88260 :           && !maybe_lookup_decl (var, outer))
     676           16 :         continue;
     677              :       break;
     678              :     }
     679              : 
     680       120990 :   if (is_global_var (maybe_lookup_decl_in_outer_ctx (var, ctx)))
     681              :     x = var;
     682       112141 :   else if (is_variable_sized (var))
     683              :     {
     684           51 :       x = TREE_OPERAND (DECL_VALUE_EXPR (var), 0);
     685           51 :       x = build_outer_var_ref (x, ctx, code);
     686           51 :       x = build_simple_mem_ref (x);
     687              :     }
     688       112090 :   else if (is_taskreg_ctx (ctx))
     689              :     {
     690        66786 :       bool by_ref = use_pointer_for_field (var, NULL);
     691        66786 :       x = build_receiver_ref (var, by_ref, ctx);
     692              :     }
     693        45304 :   else if ((gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
     694        43603 :             && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
     695        15018 :            || ctx->loop_p
     696        14245 :            || code == OMP_CLAUSE_ALLOCATE
     697        59293 :            || (code == OMP_CLAUSE_PRIVATE
     698           40 :                && (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
     699              :                    || gimple_code (ctx->stmt) == GIMPLE_OMP_SECTIONS
     700              :                    || gimple_code (ctx->stmt) == GIMPLE_OMP_SINGLE)))
     701              :     {
     702              :       /* #pragma omp simd isn't a worksharing construct, and can reference
     703              :          even private vars in its linear etc. clauses.
     704              :          Similarly for OMP_CLAUSE_PRIVATE with outer ref, that can refer
     705              :          to private vars in all worksharing constructs.  */
     706        31335 :       x = NULL_TREE;
     707        31335 :       if (outer && is_taskreg_ctx (outer))
     708         1002 :         x = lookup_decl (var, outer);
     709        30333 :       else if (outer)
     710        23987 :         x = maybe_lookup_decl_in_outer_ctx (var, ctx);
     711        24989 :       if (x == NULL_TREE)
     712              :         x = var;
     713              :     }
     714        13969 :   else if (code == OMP_CLAUSE_LASTPRIVATE && is_taskloop_ctx (ctx))
     715              :     {
     716          689 :       gcc_assert (outer);
     717          689 :       splay_tree_node n
     718         1378 :         = splay_tree_lookup (outer->field_map,
     719          689 :                              (splay_tree_key) &DECL_UID (var));
     720          689 :       if (n == NULL)
     721              :         {
     722          610 :           if (is_global_var (maybe_lookup_decl_in_outer_ctx (var, outer)))
     723              :             x = var;
     724              :           else
     725          384 :             x = lookup_decl (var, outer);
     726              :         }
     727              :       else
     728              :         {
     729           79 :           tree field = (tree) n->value;
     730              :           /* If the receiver record type was remapped in the child function,
     731              :              remap the field into the new record type.  */
     732           79 :           x = maybe_lookup_field (field, outer);
     733           79 :           if (x != NULL)
     734            0 :             field = x;
     735              : 
     736           79 :           x = build_simple_mem_ref (outer->receiver_decl);
     737           79 :           x = omp_build_component_ref (x, field);
     738           79 :           if (use_pointer_for_field (var, outer))
     739           59 :             x = build_simple_mem_ref (x);
     740              :         }
     741              :     }
     742        13280 :   else if (outer)
     743        13088 :     x = lookup_decl (var, outer);
     744          192 :   else if (omp_privatize_by_reference (var))
     745              :     /* This can happen with orphaned constructs.  If var is reference, it is
     746              :        possible it is shared and as such valid.  */
     747              :     x = var;
     748           94 :   else if (omp_member_access_dummy_var (var))
     749              :     x = var;
     750              :   else
     751            0 :     gcc_unreachable ();
     752              : 
     753       120990 :   if (x == var)
     754              :     {
     755        17368 :       tree t = omp_member_access_dummy_var (var);
     756        17368 :       if (t)
     757              :         {
     758          220 :           x = DECL_VALUE_EXPR (var);
     759          220 :           tree o = maybe_lookup_decl_in_outer_ctx (t, ctx);
     760          220 :           if (o != t)
     761           54 :             x = unshare_and_remap (x, t, o);
     762              :           else
     763          166 :             x = unshare_expr (x);
     764              :         }
     765              :     }
     766              : 
     767       120990 :   if (omp_privatize_by_reference (var))
     768         3300 :     x = build_simple_mem_ref (x);
     769              : 
     770       120990 :   return x;
     771              : }
     772              : 
     773              : /* Build tree nodes to access the field for VAR on the sender side.  */
     774              : 
     775              : static tree
     776       175603 : build_sender_ref (splay_tree_key key, omp_context *ctx)
     777              : {
     778       175603 :   tree field = lookup_sfield (key, ctx);
     779       175603 :   tree tmp = ctx->sender_decl;
     780       175603 :   if (POINTER_TYPE_P (TREE_TYPE (tmp)))
     781          454 :     tmp = build_fold_indirect_ref (tmp);
     782       175603 :   return omp_build_component_ref (tmp, field);
     783              : }
     784              : 
     785              : static tree
     786       171443 : build_sender_ref (tree var, omp_context *ctx)
     787              : {
     788            0 :   return build_sender_ref ((splay_tree_key) var, ctx);
     789              : }
     790              : 
     791              : /* Add a new field for VAR inside the structure CTX.  If BY_REF is true,
     792              :    use a pointer to the VAR rather than VAR itself.
     793              :    MASK is a bit mask of other options.  Bits are interpreted as:
     794              :       1: Install VAR in ctx->field_map.
     795              :       2: Install VAR in ctx->sfield_map.
     796              :       4: VAR is an array, convert it to a pointer.
     797              :       8: Use DECL_UID (VAR) instead of VAR as key.
     798              :      16: Use DECL_NAME (VAR) instead of VAR as key.
     799              :      32: Don't dereference omp_is_reference types.
     800              :    KEY_EXPR allows specifying something other than VAR as the lookup key.
     801              :    If specified, it also overrides the 8 and 16 MASK bits.  */
     802              : 
     803              : static void
     804       138153 : install_var_field (tree var, bool by_ref, int mask, omp_context *ctx,
     805              :                    tree key_expr = NULL_TREE)
     806              : {
     807       138153 :   tree field, type, sfield = NULL_TREE;
     808       138153 :   splay_tree_key key = (splay_tree_key) var;
     809              : 
     810       138153 :   if (key_expr)
     811              :     /* Allow caller to explicitly set the expression used as the key.  */
     812        47367 :     key = (splay_tree_key) key_expr;
     813              :   else
     814              :     {
     815        90786 :       if ((mask & 16) != 0)
     816              :         {
     817          627 :           key = (splay_tree_key) &DECL_NAME (var);
     818          627 :           gcc_checking_assert (key != (splay_tree_key) var);
     819              :         }
     820        90786 :       if ((mask & 8) != 0)
     821              :         {
     822         1622 :           key = (splay_tree_key) &DECL_UID (var);
     823         1622 :           gcc_checking_assert (key != (splay_tree_key) var);
     824              :         }
     825              :     }
     826       138153 :   gcc_assert ((mask & 1) == 0
     827              :               || !splay_tree_lookup (ctx->field_map, key));
     828       138153 :   gcc_assert ((mask & 2) == 0 || !ctx->sfield_map
     829              :               || !splay_tree_lookup (ctx->sfield_map, key));
     830       138153 :   gcc_assert ((mask & 3) == 3
     831              :               || !is_gimple_omp_oacc (ctx->stmt));
     832              : 
     833       138153 :   type = TREE_TYPE (var);
     834       138153 :   if ((mask & 16) != 0)
     835          627 :     type = lang_hooks.decls.omp_array_data (var, true);
     836              : 
     837              :   /* Prevent redeclaring the var in the split-off function with a restrict
     838              :      pointer type.  Note that we only clear type itself, restrict qualifiers in
     839              :      the pointed-to type will be ignored by points-to analysis.  */
     840       138153 :   if (POINTER_TYPE_P (type)
     841       138153 :       && TYPE_RESTRICT (type))
     842         7111 :     type = build_qualified_type (type,
     843         7111 :                                  TYPE_QUALS (type)
     844              :                                  .without (TYPE_QUAL_RESTRICT));
     845              : 
     846       138153 :   if (mask & 4)
     847              :     {
     848          425 :       gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
     849          425 :       type = build_pointer_type (build_pointer_type (type));
     850              :     }
     851       137728 :   else if (by_ref)
     852        67943 :     type = build_pointer_type (type);
     853        69785 :   else if ((mask & (32 | 3)) == 1
     854        69785 :            && omp_privatize_by_reference (var))
     855          163 :     type = TREE_TYPE (type);
     856              : 
     857       138153 :   field = build_decl (DECL_SOURCE_LOCATION (var),
     858       138153 :                       FIELD_DECL, DECL_NAME (var), type);
     859              : 
     860              :   /* Remember what variable this field was created for.  This does have a
     861              :      side effect of making dwarf2out ignore this member, so for helpful
     862              :      debugging we clear it later in delete_omp_context.  */
     863       138153 :   DECL_ABSTRACT_ORIGIN (field) = var;
     864       138153 :   if ((mask & 16) == 0 && type == TREE_TYPE (var))
     865              :     {
     866        65169 :       SET_DECL_ALIGN (field, DECL_ALIGN (var));
     867        65169 :       DECL_USER_ALIGN (field) = DECL_USER_ALIGN (var);
     868        65169 :       TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (var);
     869              :     }
     870              :   else
     871        72984 :     SET_DECL_ALIGN (field, TYPE_ALIGN (type));
     872              : 
     873       138153 :   if ((mask & 3) == 3)
     874              :     {
     875       137087 :       insert_field_into_struct (ctx->record_type, field);
     876       137087 :       if (ctx->srecord_type)
     877              :         {
     878          682 :           sfield = build_decl (DECL_SOURCE_LOCATION (var),
     879          682 :                                FIELD_DECL, DECL_NAME (var), type);
     880          682 :           DECL_ABSTRACT_ORIGIN (sfield) = var;
     881          682 :           SET_DECL_ALIGN (sfield, DECL_ALIGN (field));
     882          682 :           DECL_USER_ALIGN (sfield) = DECL_USER_ALIGN (field);
     883          682 :           TREE_THIS_VOLATILE (sfield) = TREE_THIS_VOLATILE (field);
     884          682 :           insert_field_into_struct (ctx->srecord_type, sfield);
     885              :         }
     886              :     }
     887              :   else
     888              :     {
     889         1066 :       if (ctx->srecord_type == NULL_TREE)
     890              :         {
     891          551 :           tree t;
     892              : 
     893          551 :           ctx->srecord_type = lang_hooks.types.make_type (RECORD_TYPE);
     894          551 :           ctx->sfield_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
     895         2037 :           for (t = TYPE_FIELDS (ctx->record_type); t ; t = TREE_CHAIN (t))
     896              :             {
     897         1486 :               sfield = build_decl (DECL_SOURCE_LOCATION (t),
     898         1486 :                                    FIELD_DECL, DECL_NAME (t), TREE_TYPE (t));
     899         1486 :               DECL_ABSTRACT_ORIGIN (sfield) = DECL_ABSTRACT_ORIGIN (t);
     900         1486 :               insert_field_into_struct (ctx->srecord_type, sfield);
     901         2972 :               splay_tree_insert (ctx->sfield_map,
     902         1486 :                                  (splay_tree_key) DECL_ABSTRACT_ORIGIN (t),
     903              :                                  (splay_tree_value) sfield);
     904              :             }
     905              :         }
     906         1066 :       sfield = field;
     907         1066 :       insert_field_into_struct ((mask & 1) ? ctx->record_type
     908              :                                 : ctx->srecord_type, field);
     909              :     }
     910              : 
     911       138153 :   if (mask & 1)
     912       137759 :     splay_tree_insert (ctx->field_map, key, (splay_tree_value) field);
     913       138153 :   if ((mask & 2) && ctx->sfield_map)
     914         1076 :     splay_tree_insert (ctx->sfield_map, key, (splay_tree_value) sfield);
     915       138153 : }
     916              : 
     917              : static tree
     918       243312 : install_var_local (tree var, omp_context *ctx)
     919              : {
     920       243312 :   tree new_var = omp_copy_decl_1 (var, ctx);
     921       243312 :   insert_decl_map (&ctx->cb, var, new_var);
     922       243312 :   return new_var;
     923              : }
     924              : 
     925              : /* Adjust the replacement for DECL in CTX for the new context.  This means
     926              :    copying the DECL_VALUE_EXPR, and fixing up the type.  */
     927              : 
     928              : static void
     929       216696 : fixup_remapped_decl (tree decl, omp_context *ctx, bool private_debug)
     930              : {
     931       216696 :   tree new_decl, size;
     932              : 
     933       216696 :   new_decl = lookup_decl (decl, ctx);
     934              : 
     935       216696 :   TREE_TYPE (new_decl) = remap_type (TREE_TYPE (decl), &ctx->cb);
     936              : 
     937       432248 :   if ((!TREE_CONSTANT (DECL_SIZE (new_decl)) || private_debug)
     938       216885 :       && DECL_HAS_VALUE_EXPR_P (decl))
     939              :     {
     940         1333 :       tree ve = DECL_VALUE_EXPR (decl);
     941         1333 :       walk_tree (&ve, copy_tree_body_r, &ctx->cb, NULL);
     942         1333 :       SET_DECL_VALUE_EXPR (new_decl, ve);
     943         1333 :       DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
     944              :     }
     945              : 
     946       216696 :   if (!TREE_CONSTANT (DECL_SIZE (new_decl)))
     947              :     {
     948         1144 :       size = remap_decl (DECL_SIZE (decl), &ctx->cb);
     949         1144 :       if (size == error_mark_node)
     950          321 :         size = TYPE_SIZE (TREE_TYPE (new_decl));
     951         1144 :       DECL_SIZE (new_decl) = size;
     952              : 
     953         1144 :       size = remap_decl (DECL_SIZE_UNIT (decl), &ctx->cb);
     954         1144 :       if (size == error_mark_node)
     955          308 :         size = TYPE_SIZE_UNIT (TREE_TYPE (new_decl));
     956         1144 :       DECL_SIZE_UNIT (new_decl) = size;
     957              :     }
     958       216696 : }
     959              : 
     960              : /* The callback for remap_decl.  Search all containing contexts for a
     961              :    mapping of the variable; this avoids having to duplicate the splay
     962              :    tree ahead of time.  We know a mapping doesn't already exist in the
     963              :    given context.  Create new mappings to implement default semantics.  */
     964              : 
     965              : static tree
     966       478989 : omp_copy_decl (tree var, copy_body_data *cb)
     967              : {
     968       478989 :   omp_context *ctx = (omp_context *) cb;
     969       478989 :   tree new_var;
     970              : 
     971       478989 :   if (TREE_CODE (var) == LABEL_DECL)
     972              :     {
     973       191410 :       if (FORCED_LABEL (var) || DECL_NONLOCAL (var))
     974              :         return var;
     975       191404 :       new_var = create_artificial_label (DECL_SOURCE_LOCATION (var));
     976       191404 :       DECL_CONTEXT (new_var) = current_function_decl;
     977       191404 :       insert_decl_map (&ctx->cb, var, new_var);
     978       191404 :       return new_var;
     979              :     }
     980              : 
     981       376883 :   while (!is_taskreg_ctx (ctx))
     982              :     {
     983       357269 :       ctx = ctx->outer;
     984       357269 :       if (ctx == NULL)
     985              :         return var;
     986       297444 :       new_var = maybe_lookup_decl (var, ctx);
     987       297444 :       if (new_var)
     988              :         return new_var;
     989              :     }
     990              : 
     991        19614 :   if (is_global_var (var) || decl_function_context (var) != ctx->cb.src_fn)
     992              :     return var;
     993              : 
     994         5876 :   return error_mark_node;
     995              : }
     996              : 
     997              : /* Create a new context, with OUTER_CTX being the surrounding context.  */
     998              : 
     999              : static omp_context *
    1000       144018 : new_omp_context (gimple *stmt, omp_context *outer_ctx)
    1001              : {
    1002       144018 :   omp_context *ctx = XCNEW (omp_context);
    1003              : 
    1004       144018 :   splay_tree_insert (all_contexts, (splay_tree_key) stmt,
    1005              :                      (splay_tree_value) ctx);
    1006       144018 :   ctx->stmt = stmt;
    1007              : 
    1008       144018 :   if (outer_ctx)
    1009              :     {
    1010        78506 :       ctx->outer = outer_ctx;
    1011        78506 :       ctx->cb = outer_ctx->cb;
    1012        78506 :       ctx->cb.block = NULL;
    1013        78506 :       ctx->depth = outer_ctx->depth + 1;
    1014              :     }
    1015              :   else
    1016              :     {
    1017        65512 :       ctx->cb.src_fn = current_function_decl;
    1018        65512 :       ctx->cb.dst_fn = current_function_decl;
    1019        65512 :       ctx->cb.src_node = cgraph_node::get (current_function_decl);
    1020        65512 :       gcc_checking_assert (ctx->cb.src_node);
    1021        65512 :       ctx->cb.dst_node = ctx->cb.src_node;
    1022        65512 :       ctx->cb.src_cfun = cfun;
    1023        65512 :       ctx->cb.copy_decl = omp_copy_decl;
    1024        65512 :       ctx->cb.eh_lp_nr = 0;
    1025        65512 :       ctx->cb.transform_call_graph_edges = CB_CGE_MOVE;
    1026        65512 :       ctx->cb.adjust_array_error_bounds = true;
    1027        65512 :       ctx->cb.dont_remap_vla_if_no_change = true;
    1028        65512 :       ctx->depth = 1;
    1029              :     }
    1030              : 
    1031       144018 :   ctx->cb.decl_map = new hash_map<tree, tree>;
    1032              : 
    1033       144018 :   return ctx;
    1034              : }
    1035              : 
    1036              : static gimple_seq maybe_catch_exception (gimple_seq);
    1037              : 
    1038              : /* Finalize task copyfn.  */
    1039              : 
    1040              : static void
    1041          514 : finalize_task_copyfn (gomp_task *task_stmt)
    1042              : {
    1043          514 :   struct function *child_cfun;
    1044          514 :   tree child_fn;
    1045          514 :   gimple_seq seq = NULL, new_seq;
    1046          514 :   gbind *bind;
    1047              : 
    1048          514 :   child_fn = gimple_omp_task_copy_fn (task_stmt);
    1049          514 :   if (child_fn == NULL_TREE)
    1050            0 :     return;
    1051              : 
    1052          514 :   child_cfun = DECL_STRUCT_FUNCTION (child_fn);
    1053          514 :   DECL_STRUCT_FUNCTION (child_fn)->curr_properties = cfun->curr_properties;
    1054              : 
    1055          514 :   push_cfun (child_cfun);
    1056          514 :   bind = gimplify_body (child_fn, false);
    1057          514 :   gimple_seq_add_stmt (&seq, bind);
    1058          514 :   new_seq = maybe_catch_exception (seq);
    1059          514 :   if (new_seq != seq)
    1060              :     {
    1061          341 :       bind = gimple_build_bind (NULL, new_seq, NULL);
    1062          341 :       seq = NULL;
    1063          341 :       gimple_seq_add_stmt (&seq, bind);
    1064              :     }
    1065          514 :   gimple_set_body (child_fn, seq);
    1066          514 :   pop_cfun ();
    1067              : 
    1068              :   /* Inform the callgraph about the new function.  */
    1069          514 :   cgraph_node *node = cgraph_node::get_create (child_fn);
    1070          514 :   node->parallelized_function = 1;
    1071          514 :   cgraph_node::add_new_function (child_fn, false);
    1072              : }
    1073              : 
    1074              : /* Destroy a omp_context data structures.  Called through the splay tree
    1075              :    value delete callback.  */
    1076              : 
    1077              : static void
    1078       144012 : delete_omp_context (splay_tree_value value)
    1079              : {
    1080       144012 :   omp_context *ctx = (omp_context *) value;
    1081              : 
    1082       288024 :   delete ctx->cb.decl_map;
    1083              : 
    1084       144012 :   if (ctx->field_map)
    1085        71996 :     splay_tree_delete (ctx->field_map);
    1086       144012 :   if (ctx->sfield_map)
    1087          551 :     splay_tree_delete (ctx->sfield_map);
    1088              : 
    1089              :   /* We hijacked DECL_ABSTRACT_ORIGIN earlier.  We need to clear it before
    1090              :      it produces corrupt debug information.  */
    1091       144012 :   if (ctx->record_type)
    1092              :     {
    1093        55758 :       tree t;
    1094       231921 :       for (t = TYPE_FIELDS (ctx->record_type); t ; t = DECL_CHAIN (t))
    1095       176163 :         DECL_ABSTRACT_ORIGIN (t) = NULL;
    1096              :     }
    1097       144012 :   if (ctx->srecord_type)
    1098              :     {
    1099          551 :       tree t;
    1100         3113 :       for (t = TYPE_FIELDS (ctx->srecord_type); t ; t = DECL_CHAIN (t))
    1101         2562 :         DECL_ABSTRACT_ORIGIN (t) = NULL;
    1102              :     }
    1103              : 
    1104       144012 :   if (ctx->task_reduction_map)
    1105              :     {
    1106         1176 :       ctx->task_reductions.release ();
    1107         2352 :       delete ctx->task_reduction_map;
    1108              :     }
    1109              : 
    1110       144313 :   delete ctx->lastprivate_conditional_map;
    1111       144012 :   delete ctx->lastprivate_iv_map;
    1112       145520 :   delete ctx->allocate_map;
    1113              : 
    1114       144012 :   XDELETE (ctx);
    1115       144012 : }
    1116              : 
    1117              : /* Fix up RECEIVER_DECL with a type that has been remapped to the child
    1118              :    context.  */
    1119              : 
    1120              : static void
    1121        38678 : fixup_child_record_type (omp_context *ctx)
    1122              : {
    1123        38678 :   tree f, type = ctx->record_type;
    1124              : 
    1125        38678 :   if (!ctx->receiver_decl)
    1126              :     return;
    1127              :   /* ??? It isn't sufficient to just call remap_type here, because
    1128              :      variably_modified_type_p doesn't work the way we expect for
    1129              :      record types.  Testing each field for whether it needs remapping
    1130              :      and creating a new record by hand works, however.  */
    1131       168936 :   for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
    1132       131550 :     if (variably_modified_type_p (TREE_TYPE (f), ctx->cb.src_fn))
    1133              :       break;
    1134        38678 :   if (f)
    1135              :     {
    1136         1292 :       tree name, new_fields = NULL;
    1137              : 
    1138         1292 :       type = lang_hooks.types.make_type (RECORD_TYPE);
    1139         1292 :       name = DECL_NAME (TYPE_NAME (ctx->record_type));
    1140         1292 :       name = build_decl (DECL_SOURCE_LOCATION (ctx->receiver_decl),
    1141              :                          TYPE_DECL, name, type);
    1142         1292 :       TYPE_NAME (type) = name;
    1143              : 
    1144        13546 :       for (f = TYPE_FIELDS (ctx->record_type); f ; f = DECL_CHAIN (f))
    1145              :         {
    1146        12254 :           tree new_f = copy_node (f);
    1147        12254 :           DECL_CONTEXT (new_f) = type;
    1148        12254 :           TREE_TYPE (new_f) = remap_type (TREE_TYPE (f), &ctx->cb);
    1149        12254 :           DECL_CHAIN (new_f) = new_fields;
    1150        12254 :           walk_tree (&DECL_SIZE (new_f), copy_tree_body_r, &ctx->cb, NULL);
    1151        12254 :           walk_tree (&DECL_SIZE_UNIT (new_f), copy_tree_body_r,
    1152              :                      &ctx->cb, NULL);
    1153        12254 :           walk_tree (&DECL_FIELD_OFFSET (new_f), copy_tree_body_r,
    1154              :                      &ctx->cb, NULL);
    1155        12254 :           new_fields = new_f;
    1156              : 
    1157              :           /* Arrange to be able to look up the receiver field
    1158              :              given the sender field.  */
    1159        12254 :           splay_tree_insert (ctx->field_map, (splay_tree_key) f,
    1160              :                              (splay_tree_value) new_f);
    1161              :         }
    1162         1292 :       TYPE_FIELDS (type) = nreverse (new_fields);
    1163         1292 :       layout_type (type);
    1164              :     }
    1165              : 
    1166              :   /* In a target region we never modify any of the pointers in *.omp_data_i,
    1167              :      so attempt to help the optimizers.  */
    1168        38678 :   if (is_gimple_omp_offloaded (ctx->stmt))
    1169        18507 :     type = build_qualified_type (type, TYPE_QUAL_CONST);
    1170              : 
    1171        38678 :   TREE_TYPE (ctx->receiver_decl)
    1172       116034 :     = build_qualified_type (flexible_array_type_p (type)
    1173          108 :                             ? build_pointer_type (type)
    1174        38570 :                             : build_reference_type (type), TYPE_QUAL_RESTRICT);
    1175              : }
    1176              : 
    1177              : /* Instantiate decls as necessary in CTX to satisfy the data sharing
    1178              :    specified by CLAUSES.  */
    1179              : 
    1180              : static void
    1181       136095 : scan_sharing_clauses (tree clauses, omp_context *ctx)
    1182              : {
    1183       136095 :   tree c, decl;
    1184       136095 :   bool scan_array_reductions = false;
    1185       136095 :   bool flex_array_ptr = false;
    1186              : 
    1187       580301 :   for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    1188       444206 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
    1189       444206 :         && (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
    1190              :             /* omp_default_mem_alloc is 1 */
    1191         1297 :             || !integer_onep (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
    1192          572 :             || OMP_CLAUSE_ALLOCATE_ALIGN (c) != NULL_TREE))
    1193              :       {
    1194              :         /* The allocate clauses that appear on a target construct or on
    1195              :            constructs in a target region must specify an allocator expression
    1196              :            unless a requires directive with the dynamic_allocators clause
    1197              :            is present in the same compilation unit.  */
    1198         1985 :         if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
    1199         1216 :             && ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS) == 0)
    1200         3189 :             && omp_maybe_offloaded_ctx (ctx))
    1201           14 :           error_at (OMP_CLAUSE_LOCATION (c), "%<allocate%> clause must"
    1202              :                     " specify an allocator here");
    1203         1985 :         if ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS) == 0
    1204         1973 :             && OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) != NULL_TREE
    1205          769 :             && DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
    1206         2494 :             && !DECL_ARTIFICIAL (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
    1207              :           {
    1208          279 :             tree alloc2 = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
    1209          279 :             if (TREE_CODE (alloc2) == MEM_REF
    1210          279 :                 || TREE_CODE (alloc2) == INDIRECT_REF)
    1211            0 :               alloc2 = TREE_OPERAND (alloc2, 0);
    1212          279 :             omp_context *ctx2 = ctx;
    1213          740 :             for (; ctx2; ctx2 = ctx2->outer)
    1214          480 :               if (is_gimple_omp_offloaded (ctx2->stmt))
    1215              :                 break;
    1216          279 :             if (ctx2 != NULL)
    1217              :               {
    1218           19 :                 tree c2 = gimple_omp_target_clauses (ctx2->stmt);
    1219          108 :                 for (; c2; c2 = OMP_CLAUSE_CHAIN (c2))
    1220          106 :                   if (OMP_CLAUSE_CODE (c2) == OMP_CLAUSE_USES_ALLOCATORS
    1221          123 :                       && operand_equal_p (
    1222           17 :                            alloc2, OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c2)))
    1223              :                     break;
    1224           19 :                 if (c2 == NULL_TREE)
    1225            4 :                   error_at (EXPR_LOC_OR_LOC (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
    1226              :                                              OMP_CLAUSE_LOCATION (c)),
    1227              :                             "allocator %qE in %<allocate%> clause inside a "
    1228              :                             "target region must be specified in an "
    1229              :                             "%<uses_allocators%> clause on the %<target%> "
    1230              :                             "directive", alloc2);
    1231              :               }
    1232              :           }
    1233         1985 :         if (ctx->allocate_map == NULL)
    1234         1508 :           ctx->allocate_map = new hash_map<tree, tree>;
    1235         1985 :         tree val = integer_zero_node;
    1236         1985 :         if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
    1237          769 :           val = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
    1238         1985 :         if (OMP_CLAUSE_ALLOCATE_ALIGN (c))
    1239          295 :           val = build_tree_list (val, OMP_CLAUSE_ALLOCATE_ALIGN (c));
    1240         1985 :         ctx->allocate_map->put (OMP_CLAUSE_DECL (c), val);
    1241              :       }
    1242              : 
    1243       580301 :   for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    1244              :     {
    1245       444206 :       bool by_ref;
    1246              : 
    1247       444206 :       switch (OMP_CLAUSE_CODE (c))
    1248              :         {
    1249        79735 :         case OMP_CLAUSE_PRIVATE:
    1250        79735 :           decl = OMP_CLAUSE_DECL (c);
    1251        79735 :           if (OMP_CLAUSE_PRIVATE_OUTER_REF (c))
    1252          165 :             goto do_private;
    1253        79570 :           else if (!is_variable_sized (decl))
    1254        79206 :             install_var_local (decl, ctx);
    1255              :           break;
    1256              : 
    1257        49076 :         case OMP_CLAUSE_SHARED:
    1258        49076 :           decl = OMP_CLAUSE_DECL (c);
    1259        49076 :           if (ctx->allocate_map && ctx->allocate_map->get (decl))
    1260           37 :             ctx->allocate_map->remove (decl);
    1261              :           /* Ignore shared directives in teams construct inside of
    1262              :              target construct.  */
    1263        49076 :           if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
    1264        49076 :               && !is_host_teams_ctx (ctx))
    1265              :             {
    1266              :               /* Global variables don't need to be copied,
    1267              :                  the receiver side will use them directly.  */
    1268        12841 :               tree odecl = maybe_lookup_decl_in_outer_ctx (decl, ctx);
    1269        12841 :               if (is_global_var (odecl))
    1270              :                 break;
    1271         8730 :               insert_decl_map (&ctx->cb, decl, odecl);
    1272         8730 :               break;
    1273              :             }
    1274        36235 :           gcc_assert (is_taskreg_ctx (ctx));
    1275        36235 :           gcc_assert (!COMPLETE_TYPE_P (TREE_TYPE (decl))
    1276              :                       || !is_variable_sized (decl));
    1277              :           /* Global variables don't need to be copied,
    1278              :              the receiver side will use them directly.  */
    1279        36235 :           if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
    1280              :             break;
    1281        29985 :           if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
    1282              :             {
    1283           86 :               use_pointer_for_field (decl, ctx);
    1284           86 :               break;
    1285              :             }
    1286        29899 :           by_ref = use_pointer_for_field (decl, NULL);
    1287        57240 :           if ((! TREE_READONLY (decl) && !OMP_CLAUSE_SHARED_READONLY (c))
    1288        19732 :               || TREE_ADDRESSABLE (decl)
    1289        19374 :               || by_ref
    1290        49273 :               || omp_privatize_by_reference (decl))
    1291              :             {
    1292        12728 :               by_ref = use_pointer_for_field (decl, ctx);
    1293        12728 :               install_var_field (decl, by_ref, 3, ctx);
    1294        12728 :               install_var_local (decl, ctx);
    1295        12728 :               break;
    1296              :             }
    1297              :           /* We don't need to copy const scalar vars back.  */
    1298        17171 :           OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_FIRSTPRIVATE);
    1299        17171 :           goto do_private;
    1300              : 
    1301        15370 :         case OMP_CLAUSE_REDUCTION:
    1302              :           /* Collect 'reduction' clauses on OpenACC compute construct.  */
    1303        15370 :           if (is_gimple_omp_oacc (ctx->stmt)
    1304        15370 :               && is_gimple_omp_offloaded (ctx->stmt))
    1305              :             {
    1306              :               /* No 'reduction' clauses on OpenACC 'kernels'.  */
    1307          790 :               gcc_checking_assert (!is_oacc_kernels (ctx));
    1308              :               /* Likewise, on OpenACC 'kernels' decomposed parts.  */
    1309          790 :               gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
    1310              : 
    1311          790 :               ctx->local_reduction_clauses
    1312          790 :                 = tree_cons (NULL, c, ctx->local_reduction_clauses);
    1313              :             }
    1314              :           /* FALLTHRU */
    1315              : 
    1316        17469 :         case OMP_CLAUSE_IN_REDUCTION:
    1317        17469 :           decl = OMP_CLAUSE_DECL (c);
    1318        17469 :           if (ctx->allocate_map
    1319        17469 :               && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
    1320          473 :                    && (OMP_CLAUSE_REDUCTION_INSCAN (c)
    1321          473 :                        || OMP_CLAUSE_REDUCTION_TASK (c)))
    1322          481 :                   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
    1323          473 :                   || is_task_ctx (ctx)))
    1324              :             {
    1325              :               /* For now.  */
    1326           17 :               if (ctx->allocate_map->get (decl))
    1327           17 :                 ctx->allocate_map->remove (decl);
    1328              :             }
    1329        17469 :           if (TREE_CODE (decl) == MEM_REF)
    1330              :             {
    1331         2453 :               tree t = TREE_OPERAND (decl, 0);
    1332         2453 :               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
    1333          436 :                 t = TREE_OPERAND (t, 0);
    1334         2453 :               if (INDIRECT_REF_P (t)
    1335         2341 :                   || TREE_CODE (t) == ADDR_EXPR)
    1336         1426 :                 t = TREE_OPERAND (t, 0);
    1337         2453 :               if (is_omp_target (ctx->stmt))
    1338              :                 {
    1339           96 :                   if (is_variable_sized (t))
    1340              :                     {
    1341           32 :                       gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
    1342           32 :                       t = DECL_VALUE_EXPR (t);
    1343           32 :                       gcc_assert (INDIRECT_REF_P (t));
    1344           32 :                       t = TREE_OPERAND (t, 0);
    1345           32 :                       gcc_assert (DECL_P (t));
    1346              :                     }
    1347           96 :                   tree at = t;
    1348           96 :                   if (ctx->outer)
    1349           96 :                     scan_omp_op (&at, ctx->outer);
    1350           96 :                   tree nt = omp_copy_decl_1 (at, ctx->outer);
    1351          192 :                   splay_tree_insert (ctx->field_map,
    1352           96 :                                      (splay_tree_key) &DECL_CONTEXT (t),
    1353              :                                      (splay_tree_value) nt);
    1354           96 :                   if (at != t)
    1355           96 :                     splay_tree_insert (ctx->field_map,
    1356           48 :                                        (splay_tree_key) &DECL_CONTEXT (at),
    1357              :                                        (splay_tree_value) nt);
    1358           96 :                   break;
    1359              :                 }
    1360         2357 :               install_var_local (t, ctx);
    1361         2357 :               if (is_taskreg_ctx (ctx)
    1362         1901 :                   && (!is_global_var (maybe_lookup_decl_in_outer_ctx (t, ctx))
    1363          387 :                       || (is_task_ctx (ctx)
    1364          310 :                           && (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
    1365          234 :                               || (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
    1366            0 :                                   && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
    1367              :                                       == POINTER_TYPE)))))
    1368         1590 :                   && !is_variable_sized (t)
    1369         3888 :                   && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
    1370          696 :                       || (!OMP_CLAUSE_REDUCTION_TASK (c)
    1371          657 :                           && !is_task_ctx (ctx))))
    1372              :                 {
    1373         1414 :                   by_ref = use_pointer_for_field (t, NULL);
    1374         1414 :                   if (is_task_ctx (ctx)
    1375          835 :                       && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
    1376         1574 :                       && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == POINTER_TYPE)
    1377              :                     {
    1378           32 :                       install_var_field (t, false, 1, ctx);
    1379           32 :                       install_var_field (t, by_ref, 2, ctx);
    1380              :                     }
    1381              :                   else
    1382         1382 :                     install_var_field (t, by_ref, 3, ctx);
    1383              :                 }
    1384              :               break;
    1385              :             }
    1386        15016 :           if (is_omp_target (ctx->stmt))
    1387              :             {
    1388          335 :               tree at = decl;
    1389          335 :               if (ctx->outer)
    1390           54 :                 scan_omp_op (&at, ctx->outer);
    1391          335 :               tree nt = omp_copy_decl_1 (at, ctx->outer);
    1392          670 :               splay_tree_insert (ctx->field_map,
    1393          335 :                                  (splay_tree_key) &DECL_CONTEXT (decl),
    1394              :                                  (splay_tree_value) nt);
    1395          335 :               if (at != decl)
    1396           24 :                 splay_tree_insert (ctx->field_map,
    1397           12 :                                    (splay_tree_key) &DECL_CONTEXT (at),
    1398              :                                    (splay_tree_value) nt);
    1399          335 :               break;
    1400              :             }
    1401        14681 :           if (is_task_ctx (ctx)
    1402        28267 :               || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
    1403        13586 :                   && OMP_CLAUSE_REDUCTION_TASK (c)
    1404          390 :                   && is_parallel_ctx (ctx)))
    1405              :             {
    1406              :               /* Global variables don't need to be copied,
    1407              :                  the receiver side will use them directly.  */
    1408         1183 :               if (!is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
    1409              :                 {
    1410          487 :                   by_ref = use_pointer_for_field (decl, ctx);
    1411          487 :                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
    1412          325 :                     install_var_field (decl, by_ref, 3, ctx);
    1413              :                 }
    1414         1183 :               install_var_local (decl, ctx);
    1415         1183 :               break;
    1416              :             }
    1417        13498 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
    1418        13498 :               && OMP_CLAUSE_REDUCTION_TASK (c))
    1419              :             {
    1420          302 :               install_var_local (decl, ctx);
    1421          302 :               break;
    1422              :             }
    1423        13196 :           goto do_private;
    1424              : 
    1425        24312 :         case OMP_CLAUSE_LASTPRIVATE:
    1426              :           /* Let the corresponding firstprivate clause create
    1427              :              the variable.  */
    1428        24312 :           if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
    1429              :             break;
    1430              :           /* FALLTHRU */
    1431              : 
    1432        62019 :         case OMP_CLAUSE_FIRSTPRIVATE:
    1433        62019 :         case OMP_CLAUSE_LINEAR:
    1434        62019 :           decl = OMP_CLAUSE_DECL (c);
    1435        92940 :         do_private:
    1436        92940 :           if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
    1437              :                || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR
    1438              :                || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
    1439        47019 :               && is_gimple_omp_offloaded (ctx->stmt))
    1440              :             {
    1441        16864 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
    1442        16864 :                   || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
    1443          240 :                       && lang_hooks.decls.omp_array_data (decl, true)))
    1444              :                 {
    1445              :                   /* OpenACC firstprivate clauses are later processed with same
    1446              :                      code path as map clauses in lower_omp_target, so follow
    1447              :                      the same convention of using the whole clause expression
    1448              :                      as splay-tree key.  */
    1449        16494 :                   tree k = (is_oacc_parallel_or_serial (ctx) ? c : NULL_TREE);
    1450        16494 :                   by_ref = !omp_privatize_by_reference (decl);
    1451        16494 :                   install_var_field (decl, by_ref, 3, ctx, k);
    1452              :                 }
    1453          370 :               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
    1454              :                 {
    1455          221 :                   if (INDIRECT_REF_P (decl))
    1456            0 :                     decl = TREE_OPERAND (decl, 0);
    1457          221 :                   install_var_field (decl, true, 3, ctx);
    1458              :                 }
    1459          149 :               else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
    1460            8 :                   install_var_field (decl, true, 3, ctx);
    1461              :               else
    1462          141 :                 install_var_field (decl, false, 3, ctx);
    1463              :             }
    1464        92940 :           if (is_variable_sized (decl))
    1465              :             {
    1466           68 :               if (is_task_ctx (ctx))
    1467              :                 {
    1468           14 :                   if (ctx->allocate_map
    1469           14 :                       && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
    1470              :                     {
    1471              :                       /* For now.  */
    1472            0 :                       if (ctx->allocate_map->get (decl))
    1473            0 :                         ctx->allocate_map->remove (decl);
    1474              :                     }
    1475           14 :                   install_var_field (decl, false, 1, ctx);
    1476              :                 }
    1477              :               break;
    1478              :             }
    1479        92872 :           else if (is_taskreg_ctx (ctx))
    1480              :             {
    1481        39433 :               bool global
    1482        39433 :                 = is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx));
    1483        39433 :               by_ref = use_pointer_for_field (decl, NULL);
    1484              : 
    1485        39433 :               if (is_task_ctx (ctx)
    1486        39433 :                   && (global || by_ref || omp_privatize_by_reference (decl)))
    1487              :                 {
    1488          626 :                   if (ctx->allocate_map
    1489          626 :                       && ctx->allocate_map->get (decl))
    1490           59 :                     install_var_field (decl, by_ref, 32 | 1, ctx);
    1491              :                   else
    1492          567 :                     install_var_field (decl, false, 1, ctx);
    1493          626 :                   if (!global)
    1494          362 :                     install_var_field (decl, by_ref, 2, ctx);
    1495              :                 }
    1496        38807 :               else if (!global)
    1497        37526 :                 install_var_field (decl, by_ref, 3, ctx);
    1498              :             }
    1499        92872 :           install_var_local (decl, ctx);
    1500              :           /* For descr arrays on target: firstprivatize data + attach ptr.  */
    1501        92872 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
    1502        46575 :               && is_gimple_omp_offloaded (ctx->stmt)
    1503        16465 :               && !is_gimple_omp_oacc (ctx->stmt)
    1504        12217 :               && lang_hooks.decls.omp_array_data (decl, true)
    1505        92914 :               && lang_hooks.decls.omp_array_data_privatize (decl))
    1506              :             {
    1507           26 :               install_var_field (decl, false, 16 | 3, ctx);
    1508           26 :               install_var_field (decl, true, 8 | 3, ctx);
    1509              :             }
    1510              :           break;
    1511              : 
    1512         2118 :         case OMP_CLAUSE_USE_DEVICE_PTR:
    1513         2118 :         case OMP_CLAUSE_USE_DEVICE_ADDR:
    1514         2118 :           decl = OMP_CLAUSE_DECL (c);
    1515              : 
    1516              :           /* Fortran array descriptors.  */
    1517         2118 :           if (lang_hooks.decls.omp_array_data (decl, true))
    1518          601 :             install_var_field (decl, false, 19, ctx);
    1519         1517 :           else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
    1520         1312 :                     && !omp_privatize_by_reference (decl)
    1521          513 :                     && !omp_is_allocatable_or_ptr (decl))
    1522         2454 :                    || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
    1523          407 :             install_var_field (decl, true, 11, ctx);
    1524              :           else
    1525         1110 :             install_var_field (decl, false, 11, ctx);
    1526         2118 :           if (DECL_SIZE (decl)
    1527         2118 :               && !poly_int_tree_p (DECL_SIZE (decl)))
    1528              :             {
    1529            6 :               tree decl2 = DECL_VALUE_EXPR (decl);
    1530            6 :               gcc_assert (INDIRECT_REF_P (decl2));
    1531            6 :               decl2 = TREE_OPERAND (decl2, 0);
    1532            6 :               gcc_assert (DECL_P (decl2));
    1533            6 :               install_var_local (decl2, ctx);
    1534              :             }
    1535         2118 :           install_var_local (decl, ctx);
    1536         2118 :           break;
    1537              : 
    1538          240 :         case OMP_CLAUSE_HAS_DEVICE_ADDR:
    1539          240 :           decl = OMP_CLAUSE_DECL (c);
    1540          240 :           while (INDIRECT_REF_P (decl)
    1541          257 :                  || TREE_CODE (decl) == ARRAY_REF)
    1542           17 :             decl = TREE_OPERAND (decl, 0);
    1543          240 :           goto do_private;
    1544              : 
    1545          149 :         case OMP_CLAUSE_IS_DEVICE_PTR:
    1546          149 :           decl = OMP_CLAUSE_DECL (c);
    1547          149 :           goto do_private;
    1548              : 
    1549        21761 :         case OMP_CLAUSE__LOOPTEMP_:
    1550        21761 :         case OMP_CLAUSE__REDUCTEMP_:
    1551        21761 :           gcc_assert (is_taskreg_ctx (ctx));
    1552        21761 :           decl = OMP_CLAUSE_DECL (c);
    1553        21761 :           install_var_field (decl, false, 3, ctx);
    1554        21761 :           install_var_local (decl, ctx);
    1555        21761 :           break;
    1556              : 
    1557          899 :         case OMP_CLAUSE_COPYPRIVATE:
    1558          899 :         case OMP_CLAUSE_COPYIN:
    1559          899 :           decl = OMP_CLAUSE_DECL (c);
    1560          899 :           by_ref = use_pointer_for_field (decl, NULL);
    1561          899 :           install_var_field (decl, by_ref, 3, ctx);
    1562          899 :           break;
    1563              : 
    1564        57710 :         case OMP_CLAUSE_FINAL:
    1565        57710 :         case OMP_CLAUSE_IF:
    1566        57710 :         case OMP_CLAUSE_SELF:
    1567        57710 :         case OMP_CLAUSE_NUM_THREADS:
    1568        57710 :         case OMP_CLAUSE_NUM_TEAMS:
    1569        57710 :         case OMP_CLAUSE_THREAD_LIMIT:
    1570        57710 :         case OMP_CLAUSE_DEVICE:
    1571        57710 :         case OMP_CLAUSE_SCHEDULE:
    1572        57710 :         case OMP_CLAUSE_DIST_SCHEDULE:
    1573        57710 :         case OMP_CLAUSE_DEPEND:
    1574        57710 :         case OMP_CLAUSE_PRIORITY:
    1575        57710 :         case OMP_CLAUSE_GRAINSIZE:
    1576        57710 :         case OMP_CLAUSE_NUM_TASKS:
    1577        57710 :         case OMP_CLAUSE_NUM_GANGS:
    1578        57710 :         case OMP_CLAUSE_NUM_WORKERS:
    1579        57710 :         case OMP_CLAUSE_VECTOR_LENGTH:
    1580        57710 :         case OMP_CLAUSE_DETACH:
    1581        57710 :         case OMP_CLAUSE_FILTER:
    1582        57710 :           if (ctx->outer)
    1583        17732 :             scan_omp_op (&OMP_CLAUSE_OPERAND (c, 0), ctx->outer);
    1584              :           break;
    1585              : 
    1586        92135 :         case OMP_CLAUSE_TO:
    1587        92135 :         case OMP_CLAUSE_FROM:
    1588        92135 :         case OMP_CLAUSE_MAP:
    1589        92135 :           if (ctx->outer)
    1590        15339 :             scan_omp_op (&OMP_CLAUSE_SIZE (c), ctx->outer);
    1591        92135 :           decl = OMP_CLAUSE_DECL (c);
    1592              :           /* If requested, make 'decl' addressable.  */
    1593        92135 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
    1594        92135 :               && OMP_CLAUSE_MAP_DECL_MAKE_ADDRESSABLE (c))
    1595              :             {
    1596          433 :               gcc_checking_assert (DECL_P (decl));
    1597              : 
    1598          433 :               bool decl_addressable = TREE_ADDRESSABLE (decl);
    1599          433 :               if (!decl_addressable)
    1600              :                 {
    1601          219 :                   if (!make_addressable_vars)
    1602          131 :                     make_addressable_vars = BITMAP_ALLOC (NULL);
    1603          219 :                   bitmap_set_bit (make_addressable_vars, DECL_UID (decl));
    1604          219 :                   TREE_ADDRESSABLE (decl) = 1;
    1605              :                 }
    1606              : 
    1607          433 :               if (dump_enabled_p ())
    1608              :                 {
    1609          421 :                   location_t loc = OMP_CLAUSE_LOCATION (c);
    1610          421 :                   const dump_user_location_t d_u_loc
    1611          421 :                     = dump_user_location_t::from_location_t (loc);
    1612              :                   /* PR100695 "Format decoder, quoting in 'dump_printf' etc." */
    1613              : #if __GNUC__ >= 10
    1614          421 : # pragma GCC diagnostic push
    1615          421 : # pragma GCC diagnostic ignored "-Wformat"
    1616              : #endif
    1617          421 :                   if (!decl_addressable)
    1618          207 :                     dump_printf_loc (MSG_NOTE, d_u_loc,
    1619              :                                      "variable %<%T%>"
    1620              :                                      " made addressable\n",
    1621              :                                      decl);
    1622              :                   else
    1623          214 :                     dump_printf_loc (MSG_NOTE, d_u_loc,
    1624              :                                      "variable %<%T%>"
    1625              :                                      " already made addressable\n",
    1626              :                                      decl);
    1627              : #if __GNUC__ >= 10
    1628          421 : # pragma GCC diagnostic pop
    1629              : #endif
    1630              :                 }
    1631              : 
    1632              :               /* Done.  */
    1633          433 :               OMP_CLAUSE_MAP_DECL_MAKE_ADDRESSABLE (c) = 0;
    1634              :             }
    1635        92135 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
    1636        92135 :               && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
    1637              :             {
    1638              :               /* Ignore GOMP_MAP_POINTER kind for arrays in regions that are
    1639              :                  not offloaded; there is nothing to map for those.  */
    1640        10486 :               if (!is_gimple_omp_offloaded (ctx->stmt)
    1641         4981 :                   && !POINTER_TYPE_P (TREE_TYPE (decl))
    1642        10750 :                   && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c))
    1643              :                 break;
    1644              :             }
    1645        91871 :           if (!flex_array_ptr)
    1646        91643 :             flex_array_ptr = lang_hooks.decls.omp_deep_mapping_p (ctx->stmt, c);
    1647              : 
    1648              :           /* Global variables with "omp declare target" attribute
    1649              :              don't need to be copied, the receiver side will use them
    1650              :              directly.  However, global variables with "omp declare target link"
    1651              :              attribute need to be copied.  Or when ALWAYS modifier is used.  */
    1652        91871 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
    1653        82529 :               && DECL_P (decl)
    1654        46164 :               && ((OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
    1655        42287 :                    && (OMP_CLAUSE_MAP_KIND (c)
    1656              :                        != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
    1657        41858 :                    && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
    1658        41620 :                    && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)
    1659         4609 :                   || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
    1660        42764 :               && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_TO
    1661        42627 :               && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_FROM
    1662        42594 :               && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_TOFROM
    1663        42101 :               && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_PRESENT_TO
    1664        42071 :               && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_PRESENT_FROM
    1665        42049 :               && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_PRESENT_TOFROM
    1666        42035 :               && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_TO_PSET
    1667        38033 :               && is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx))
    1668         9028 :               && varpool_node::get_create (decl)->offloadable
    1669        98089 :               && !lookup_attribute ("omp declare target link",
    1670         6218 :                                     DECL_ATTRIBUTES (decl)))
    1671              :             break;
    1672        85694 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
    1673        76352 :               && DECL_P (decl)
    1674        39987 :               && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
    1675        39749 :                   || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
    1676        85997 :               && is_omp_target (ctx->stmt))
    1677              :             {
    1678              :               /* If this is an offloaded region, an attach operation should
    1679              :                  only exist when the pointer variable is mapped in a prior
    1680              :                  clause.  An exception is if we have a reference (to pointer):
    1681              :                  in that case we should have mapped "*decl" in a previous
    1682              :                  mapping instead of "decl".  Skip the assertion in that case.
    1683              :                  If we had an error, we may not have attempted to sort clauses
    1684              :                  properly, so avoid the test.  */
    1685          265 :               if (TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
    1686          233 :                   && is_gimple_omp_offloaded (ctx->stmt)
    1687          335 :                   && !seen_error ())
    1688           62 :                 gcc_assert
    1689              :                   (maybe_lookup_decl (decl, ctx)
    1690              :                    || (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx))
    1691              :                        && lookup_attribute ("omp declare target",
    1692              :                                             DECL_ATTRIBUTES (decl))));
    1693              : 
    1694              :               /* By itself, attach/detach is generated as part of pointer
    1695              :                  variable mapping and should not create new variables in the
    1696              :                  offloaded region, however sender refs for it must be created
    1697              :                  for its address to be passed to the runtime.  */
    1698          265 :               tree field
    1699          265 :                 = build_decl (OMP_CLAUSE_LOCATION (c),
    1700              :                               FIELD_DECL, NULL_TREE, ptr_type_node);
    1701          265 :               SET_DECL_ALIGN (field, TYPE_ALIGN (ptr_type_node));
    1702          265 :               insert_field_into_struct (ctx->record_type, field);
    1703              :               /* To not clash with a map of the pointer variable itself,
    1704              :                  attach/detach maps have their field looked up by the *clause*
    1705              :                  tree expression, not the decl.  */
    1706          265 :               gcc_assert (!splay_tree_lookup (ctx->field_map,
    1707              :                                               (splay_tree_key) c));
    1708          265 :               splay_tree_insert (ctx->field_map, (splay_tree_key) c,
    1709              :                                  (splay_tree_value) field);
    1710          265 :               break;
    1711              :             }
    1712        85429 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
    1713        85429 :               && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
    1714        72212 :                   || (OMP_CLAUSE_MAP_KIND (c)
    1715              :                       == GOMP_MAP_FIRSTPRIVATE_REFERENCE)))
    1716              :             {
    1717         4304 :               if (TREE_CODE (decl) == COMPONENT_REF
    1718         4304 :                   || (INDIRECT_REF_P (decl)
    1719            0 :                       && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
    1720            0 :                       && (((TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
    1721              :                             == REFERENCE_TYPE)
    1722            0 :                            || (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
    1723              :                                == POINTER_TYPE)))))
    1724              :                 break;
    1725         4304 :               if (DECL_SIZE (decl)
    1726         4304 :                   && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
    1727              :                 {
    1728          712 :                   tree decl2 = DECL_VALUE_EXPR (decl);
    1729          712 :                   gcc_assert (INDIRECT_REF_P (decl2));
    1730          712 :                   decl2 = TREE_OPERAND (decl2, 0);
    1731          712 :                   gcc_assert (DECL_P (decl2));
    1732          712 :                   install_var_local (decl2, ctx);
    1733              :                 }
    1734         4304 :               install_var_local (decl, ctx);
    1735         4304 :               break;
    1736              :             }
    1737        81125 :           if (DECL_P (decl))
    1738              :             {
    1739        43119 :               if (DECL_SIZE (decl)
    1740        43119 :                   && !poly_int_tree_p (DECL_SIZE (decl)))
    1741              :                 {
    1742            0 :                   tree decl2 = DECL_VALUE_EXPR (decl);
    1743            0 :                   gcc_assert (INDIRECT_REF_P (decl2));
    1744            0 :                   decl2 = TREE_OPERAND (decl2, 0);
    1745            0 :                   gcc_assert (DECL_P (decl2));
    1746            0 :                   install_var_field (decl2, true, 3, ctx, c);
    1747            0 :                   install_var_local (decl2, ctx);
    1748            0 :                   install_var_local (decl, ctx);
    1749              :                 }
    1750              :               else
    1751              :                 {
    1752        43119 :                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
    1753        35418 :                       && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
    1754         5183 :                       && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
    1755        48302 :                       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
    1756          425 :                     install_var_field (decl, true, 7, ctx, c);
    1757              :                   else
    1758        42694 :                     install_var_field (decl, true, 3, ctx, c);
    1759        43119 :                   if (is_gimple_omp_offloaded (ctx->stmt)
    1760        43119 :                       && !(is_gimple_omp_oacc (ctx->stmt)
    1761        13857 :                            && OMP_CLAUSE_MAP_IN_REDUCTION (c)))
    1762        24846 :                     install_var_local (decl, ctx);
    1763              :                 }
    1764              :             }
    1765              :           else
    1766              :             {
    1767        38006 :               tree base = get_base_address (decl);
    1768        38006 :               tree nc = OMP_CLAUSE_CHAIN (c);
    1769        38006 :               if (DECL_P (base)
    1770         9378 :                   && nc != NULL_TREE
    1771         6753 :                   && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
    1772         5589 :                   && OMP_CLAUSE_DECL (nc) == base
    1773         1188 :                   && OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_POINTER
    1774        38674 :                   && integer_zerop (OMP_CLAUSE_SIZE (nc)))
    1775              :                 {
    1776            0 :                   OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c) = 1;
    1777            0 :                   OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (nc) = 1;
    1778              :                 }
    1779              :               else
    1780              :                 {
    1781        38006 :                   if (ctx->outer)
    1782              :                     {
    1783         7152 :                       scan_omp_op (&OMP_CLAUSE_DECL (c), ctx->outer);
    1784         7152 :                       decl = OMP_CLAUSE_DECL (c);
    1785              :                     }
    1786        38006 :                   gcc_assert (!splay_tree_lookup (ctx->field_map,
    1787              :                                                   (splay_tree_key) decl));
    1788        38006 :                   tree field
    1789        38006 :                     = build_decl (OMP_CLAUSE_LOCATION (c),
    1790              :                                   FIELD_DECL, NULL_TREE, ptr_type_node);
    1791        38006 :                   SET_DECL_ALIGN (field, TYPE_ALIGN (ptr_type_node));
    1792        38006 :                   insert_field_into_struct (ctx->record_type, field);
    1793        38006 :                   splay_tree_insert (ctx->field_map, (splay_tree_key) c,
    1794              :                                      (splay_tree_value) field);
    1795              :                 }
    1796              :             }
    1797              :           break;
    1798              : 
    1799         4001 :         case OMP_CLAUSE_ORDER:
    1800         4001 :           ctx->order_concurrent = true;
    1801         4001 :           break;
    1802              : 
    1803         1935 :         case OMP_CLAUSE_BIND:
    1804         1935 :           ctx->loop_p = true;
    1805         1935 :           break;
    1806              : 
    1807              :         case OMP_CLAUSE_NOWAIT:
    1808              :         case OMP_CLAUSE_ORDERED:
    1809              :         case OMP_CLAUSE_COLLAPSE:
    1810              :         case OMP_CLAUSE_UNTIED:
    1811              :         case OMP_CLAUSE_MERGEABLE:
    1812              :         case OMP_CLAUSE_PROC_BIND:
    1813              :         case OMP_CLAUSE_SAFELEN:
    1814              :         case OMP_CLAUSE_SIMDLEN:
    1815              :         case OMP_CLAUSE_THREADS:
    1816              :         case OMP_CLAUSE_SIMD:
    1817              :         case OMP_CLAUSE_NOGROUP:
    1818              :         case OMP_CLAUSE_DEFAULTMAP:
    1819              :         case OMP_CLAUSE_ASYNC:
    1820              :         case OMP_CLAUSE_WAIT:
    1821              :         case OMP_CLAUSE_GANG:
    1822              :         case OMP_CLAUSE_WORKER:
    1823              :         case OMP_CLAUSE_VECTOR:
    1824              :         case OMP_CLAUSE_INDEPENDENT:
    1825              :         case OMP_CLAUSE_AUTO:
    1826              :         case OMP_CLAUSE_SEQ:
    1827              :         case OMP_CLAUSE_TILE:
    1828              :         case OMP_CLAUSE__SIMT_:
    1829              :         case OMP_CLAUSE_DEFAULT:
    1830              :         case OMP_CLAUSE_NONTEMPORAL:
    1831              :         case OMP_CLAUSE_IF_PRESENT:
    1832              :         case OMP_CLAUSE_FINALIZE:
    1833              :         case OMP_CLAUSE_TASK_REDUCTION:
    1834              :         case OMP_CLAUSE_ALLOCATE:
    1835              :         case OMP_CLAUSE_DEVICE_TYPE:
    1836              :           break;
    1837              : 
    1838          112 :         case OMP_CLAUSE_USES_ALLOCATORS:
    1839          112 :           decl = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
    1840          112 :           gcc_assert (DECL_P (decl));
    1841          112 :           gcc_assert (is_gimple_omp_offloaded (ctx->stmt));
    1842          112 :           install_var_field (decl, false, 3, ctx);
    1843          112 :           install_var_local (decl, ctx);
    1844          112 :           break;
    1845              : 
    1846          162 :         case OMP_CLAUSE_ALIGNED:
    1847          162 :           decl = OMP_CLAUSE_DECL (c);
    1848          162 :           if (is_global_var (decl)
    1849          162 :               && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
    1850           86 :             install_var_local (decl, ctx);
    1851              :           break;
    1852              : 
    1853          393 :         case OMP_CLAUSE__CONDTEMP_:
    1854          393 :           decl = OMP_CLAUSE_DECL (c);
    1855          393 :           if (is_parallel_ctx (ctx))
    1856              :             {
    1857          122 :               install_var_field (decl, false, 3, ctx);
    1858          122 :               install_var_local (decl, ctx);
    1859              :             }
    1860          271 :           else if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
    1861          271 :                    && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD
    1862          420 :                    && !OMP_CLAUSE__CONDTEMP__ITER (c))
    1863          149 :             install_var_local (decl, ctx);
    1864              :           break;
    1865              : 
    1866              :         case OMP_CLAUSE_INIT:
    1867              :         case OMP_CLAUSE_USE:
    1868              :         case OMP_CLAUSE_DESTROY:
    1869              :         case OMP_CLAUSE_MESSAGE:
    1870              :           break;
    1871              : 
    1872            0 :         case OMP_CLAUSE__CACHE_:
    1873            0 :         case OMP_CLAUSE_NOHOST:
    1874            0 :         default:
    1875            0 :           gcc_unreachable ();
    1876              :         }
    1877              :     }
    1878              : 
    1879       580301 :   for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    1880              :     {
    1881       444206 :       switch (OMP_CLAUSE_CODE (c))
    1882              :         {
    1883        24312 :         case OMP_CLAUSE_LASTPRIVATE:
    1884              :           /* Let the corresponding firstprivate clause create
    1885              :              the variable.  */
    1886        24312 :           if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c))
    1887         8015 :             scan_array_reductions = true;
    1888        24312 :           if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
    1889              :             break;
    1890              :           /* FALLTHRU */
    1891              : 
    1892       159314 :         case OMP_CLAUSE_FIRSTPRIVATE:
    1893       159314 :         case OMP_CLAUSE_PRIVATE:
    1894       159314 :         case OMP_CLAUSE_LINEAR:
    1895       159314 :         case OMP_CLAUSE_HAS_DEVICE_ADDR:
    1896       159314 :         case OMP_CLAUSE_IS_DEVICE_PTR:
    1897       159314 :           decl = OMP_CLAUSE_DECL (c);
    1898       159314 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
    1899              :             {
    1900          257 :               while (INDIRECT_REF_P (decl)
    1901          257 :                      || TREE_CODE (decl) == ARRAY_REF)
    1902           17 :                 decl = TREE_OPERAND (decl, 0);
    1903              :             }
    1904              : 
    1905       159314 :           if (is_variable_sized (decl))
    1906              :             {
    1907          432 :               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
    1908              :                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR
    1909              :                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
    1910           61 :                   && is_gimple_omp_offloaded (ctx->stmt))
    1911              :                 {
    1912           16 :                   tree decl2 = DECL_VALUE_EXPR (decl);
    1913           16 :                   gcc_assert (INDIRECT_REF_P (decl2));
    1914           16 :                   decl2 = TREE_OPERAND (decl2, 0);
    1915           16 :                   gcc_assert (DECL_P (decl2));
    1916           16 :                   install_var_local (decl2, ctx);
    1917           16 :                   fixup_remapped_decl (decl2, ctx, false);
    1918              :                 }
    1919          432 :               install_var_local (decl, ctx);
    1920              :             }
    1921       318628 :           fixup_remapped_decl (decl, ctx,
    1922       159314 :                                OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
    1923        79735 :                                && OMP_CLAUSE_PRIVATE_DEBUG (c));
    1924       159314 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
    1925       159314 :               && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c))
    1926              :             scan_array_reductions = true;
    1927              :           break;
    1928              : 
    1929        17469 :         case OMP_CLAUSE_REDUCTION:
    1930        17469 :         case OMP_CLAUSE_IN_REDUCTION:
    1931        17469 :           decl = OMP_CLAUSE_DECL (c);
    1932        17469 :           if (TREE_CODE (decl) != MEM_REF && !is_omp_target (ctx->stmt))
    1933              :             {
    1934        14681 :               if (is_variable_sized (decl))
    1935            0 :                 install_var_local (decl, ctx);
    1936        14681 :               fixup_remapped_decl (decl, ctx, false);
    1937              :             }
    1938        17469 :           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    1939       444206 :             scan_array_reductions = true;
    1940              :           break;
    1941              : 
    1942          530 :         case OMP_CLAUSE_TASK_REDUCTION:
    1943          530 :           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    1944       444206 :             scan_array_reductions = true;
    1945              :           break;
    1946              : 
    1947        31905 :         case OMP_CLAUSE_SHARED:
    1948              :           /* Ignore shared directives in teams construct inside of
    1949              :              target construct.  */
    1950        31905 :           if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
    1951        31905 :               && !is_host_teams_ctx (ctx))
    1952              :             break;
    1953        19064 :           decl = OMP_CLAUSE_DECL (c);
    1954        19064 :           if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
    1955              :             break;
    1956        12814 :           if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
    1957              :             {
    1958           86 :               if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl,
    1959              :                                                                  ctx->outer)))
    1960              :                 break;
    1961           79 :               bool by_ref = use_pointer_for_field (decl, ctx);
    1962           79 :               install_var_field (decl, by_ref, 11, ctx);
    1963           79 :               break;
    1964              :             }
    1965        12728 :           fixup_remapped_decl (decl, ctx, false);
    1966        12728 :           break;
    1967              : 
    1968        82793 :         case OMP_CLAUSE_MAP:
    1969        82793 :           if (!is_gimple_omp_offloaded (ctx->stmt))
    1970              :             break;
    1971        55961 :           decl = OMP_CLAUSE_DECL (c);
    1972        55961 :           if (DECL_P (decl)
    1973        35904 :               && ((OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
    1974        32195 :                    && (OMP_CLAUSE_MAP_KIND (c)
    1975              :                        != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
    1976         4138 :                   || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
    1977        32975 :               && is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx))
    1978        64813 :               && varpool_node::get_create (decl)->offloadable)
    1979              :             break;
    1980        49326 :           if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
    1981        47114 :                || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
    1982         2212 :               && is_omp_target (ctx->stmt)
    1983        51329 :               && !is_gimple_omp_offloaded (ctx->stmt))
    1984              :             break;
    1985        49326 :           if (DECL_P (decl))
    1986              :             {
    1987        29269 :               if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
    1988        26238 :                    || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
    1989         6738 :                   && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
    1990        30900 :                   && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
    1991              :                 {
    1992           24 :                   tree new_decl = lookup_decl (decl, ctx);
    1993           24 :                   TREE_TYPE (new_decl)
    1994           48 :                     = remap_type (TREE_TYPE (decl), &ctx->cb);
    1995              :                 }
    1996        29245 :               else if (DECL_SIZE (decl)
    1997        29245 :                        && !poly_int_tree_p (DECL_SIZE (decl)))
    1998              :                 {
    1999          712 :                   tree decl2 = DECL_VALUE_EXPR (decl);
    2000          712 :                   gcc_assert (INDIRECT_REF_P (decl2));
    2001          712 :                   decl2 = TREE_OPERAND (decl2, 0);
    2002          712 :                   gcc_assert (DECL_P (decl2));
    2003          712 :                   fixup_remapped_decl (decl2, ctx, false);
    2004          712 :                   fixup_remapped_decl (decl, ctx, true);
    2005              :                 }
    2006              :               else
    2007        28533 :                 fixup_remapped_decl (decl, ctx, false);
    2008              :             }
    2009              :           break;
    2010              : 
    2011              :         case OMP_CLAUSE_COPYPRIVATE:
    2012              :         case OMP_CLAUSE_COPYIN:
    2013              :         case OMP_CLAUSE_DEFAULT:
    2014              :         case OMP_CLAUSE_IF:
    2015              :         case OMP_CLAUSE_SELF:
    2016              :         case OMP_CLAUSE_NUM_THREADS:
    2017              :         case OMP_CLAUSE_NUM_TEAMS:
    2018              :         case OMP_CLAUSE_THREAD_LIMIT:
    2019              :         case OMP_CLAUSE_DEVICE:
    2020              :         case OMP_CLAUSE_SCHEDULE:
    2021              :         case OMP_CLAUSE_DIST_SCHEDULE:
    2022              :         case OMP_CLAUSE_NOWAIT:
    2023              :         case OMP_CLAUSE_ORDERED:
    2024              :         case OMP_CLAUSE_COLLAPSE:
    2025              :         case OMP_CLAUSE_UNTIED:
    2026              :         case OMP_CLAUSE_FINAL:
    2027              :         case OMP_CLAUSE_MERGEABLE:
    2028              :         case OMP_CLAUSE_PROC_BIND:
    2029              :         case OMP_CLAUSE_SAFELEN:
    2030              :         case OMP_CLAUSE_SIMDLEN:
    2031              :         case OMP_CLAUSE_ALIGNED:
    2032              :         case OMP_CLAUSE_DEPEND:
    2033              :         case OMP_CLAUSE_DETACH:
    2034              :         case OMP_CLAUSE_ALLOCATE:
    2035              :         case OMP_CLAUSE__LOOPTEMP_:
    2036              :         case OMP_CLAUSE__REDUCTEMP_:
    2037              :         case OMP_CLAUSE_TO:
    2038              :         case OMP_CLAUSE_FROM:
    2039              :         case OMP_CLAUSE_PRIORITY:
    2040              :         case OMP_CLAUSE_GRAINSIZE:
    2041              :         case OMP_CLAUSE_NUM_TASKS:
    2042              :         case OMP_CLAUSE_THREADS:
    2043              :         case OMP_CLAUSE_SIMD:
    2044              :         case OMP_CLAUSE_NOGROUP:
    2045              :         case OMP_CLAUSE_DEFAULTMAP:
    2046              :         case OMP_CLAUSE_ORDER:
    2047              :         case OMP_CLAUSE_BIND:
    2048              :         case OMP_CLAUSE_USE_DEVICE_PTR:
    2049              :         case OMP_CLAUSE_USE_DEVICE_ADDR:
    2050              :         case OMP_CLAUSE_NONTEMPORAL:
    2051              :         case OMP_CLAUSE_ASYNC:
    2052              :         case OMP_CLAUSE_WAIT:
    2053              :         case OMP_CLAUSE_NUM_GANGS:
    2054              :         case OMP_CLAUSE_NUM_WORKERS:
    2055              :         case OMP_CLAUSE_VECTOR_LENGTH:
    2056              :         case OMP_CLAUSE_GANG:
    2057              :         case OMP_CLAUSE_WORKER:
    2058              :         case OMP_CLAUSE_VECTOR:
    2059              :         case OMP_CLAUSE_INDEPENDENT:
    2060              :         case OMP_CLAUSE_AUTO:
    2061              :         case OMP_CLAUSE_SEQ:
    2062              :         case OMP_CLAUSE_TILE:
    2063              :         case OMP_CLAUSE__SIMT_:
    2064              :         case OMP_CLAUSE_IF_PRESENT:
    2065              :         case OMP_CLAUSE_FINALIZE:
    2066              :         case OMP_CLAUSE_FILTER:
    2067              :         case OMP_CLAUSE__CONDTEMP_:
    2068              :         case OMP_CLAUSE_INIT:
    2069              :         case OMP_CLAUSE_USE:
    2070              :         case OMP_CLAUSE_DESTROY:
    2071              :         case OMP_CLAUSE_DEVICE_TYPE:
    2072              :         case OMP_CLAUSE_USES_ALLOCATORS:
    2073              :         case OMP_CLAUSE_MESSAGE:
    2074              :           break;
    2075              : 
    2076            0 :         case OMP_CLAUSE__CACHE_:
    2077            0 :         case OMP_CLAUSE_NOHOST:
    2078            0 :         default:
    2079            0 :           gcc_unreachable ();
    2080              :         }
    2081              :     }
    2082              : 
    2083       136095 :   gcc_checking_assert (!scan_array_reductions
    2084              :                        || !is_gimple_omp_oacc (ctx->stmt));
    2085              :   if (scan_array_reductions)
    2086              :     {
    2087        37069 :       for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    2088        31495 :         if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
    2089        29076 :              || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
    2090        28428 :              || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
    2091        32223 :             && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    2092              :           {
    2093         2241 :             omp_context *rctx = ctx;
    2094         2241 :             if (is_omp_target (ctx->stmt))
    2095           60 :               rctx = ctx->outer;
    2096         2241 :             scan_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c), rctx);
    2097         2241 :             scan_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), rctx);
    2098              :           }
    2099        29254 :         else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    2100        29254 :                  && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c))
    2101         8015 :           scan_omp (&OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c), ctx);
    2102        21239 :         else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
    2103        21239 :                  && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c))
    2104          686 :           scan_omp (&OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c), ctx);
    2105              :     }
    2106       136095 :   if (flex_array_ptr)
    2107              :     {
    2108          145 :       tree field = build_range_type (size_type_node,
    2109              :                                      build_int_cstu (size_type_node, 0),
    2110              :                                      NULL_TREE);
    2111          145 :       field = build_array_type (ptr_type_node, field);
    2112          145 :       field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, field);
    2113          145 :       SET_DECL_ALIGN (field, TYPE_ALIGN (ptr_type_node));
    2114          145 :       DECL_CONTEXT (field) = ctx->record_type;
    2115          145 :       DECL_CHAIN (field) = TYPE_FIELDS (ctx->record_type);
    2116          145 :       TYPE_FIELDS (ctx->record_type) = field;
    2117              :     }
    2118       136095 : }
    2119              : 
    2120              : /* Create a new name for omp child function.  Returns an identifier. */
    2121              : 
    2122              : static tree
    2123        53804 : create_omp_child_function_name (bool task_copy)
    2124              : {
    2125       107057 :   return clone_function_name_numbered (current_function_decl,
    2126        53804 :                                        task_copy ? "_omp_cpyfn" : "_omp_fn");
    2127              : }
    2128              : 
    2129              : /* Return true if CTX may belong to offloaded code: either if current function
    2130              :    is offloaded, or any enclosing context corresponds to a target region.  */
    2131              : 
    2132              : static bool
    2133       129868 : omp_maybe_offloaded_ctx (omp_context *ctx)
    2134              : {
    2135       129868 :   if (cgraph_node::get (current_function_decl)->offloadable)
    2136              :     return true;
    2137       218189 :   for (; ctx; ctx = ctx->outer)
    2138       131350 :     if (is_gimple_omp_offloaded (ctx->stmt))
    2139              :       return true;
    2140              :   return false;
    2141              : }
    2142              : 
    2143              : /* Build a decl for the omp child function.  It'll not contain a body
    2144              :    yet, just the bare decl. If HOST_ONLY, do not create a device version.  */
    2145              : 
    2146              : static void
    2147        53804 : create_omp_child_function (omp_context *ctx, bool task_copy, bool host_only)
    2148              : {
    2149        53804 :   tree decl, type, name, t;
    2150              : 
    2151        53804 :   name = create_omp_child_function_name (task_copy);
    2152        53804 :   if (task_copy)
    2153          551 :     type = build_function_type_list (void_type_node, ptr_type_node,
    2154              :                                      ptr_type_node, NULL_TREE);
    2155              :   else
    2156        53253 :     type = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
    2157              : 
    2158        53804 :   decl = build_decl (gimple_location (ctx->stmt), FUNCTION_DECL, name, type);
    2159              : 
    2160        53804 :   gcc_checking_assert (!is_gimple_omp_oacc (ctx->stmt)
    2161              :                        || !task_copy);
    2162        42418 :   if (!task_copy)
    2163        53253 :     ctx->cb.dst_fn = decl;
    2164              :   else
    2165          551 :     gimple_omp_task_set_copy_fn (ctx->stmt, decl);
    2166              : 
    2167        53804 :   TREE_STATIC (decl) = 1;
    2168        53804 :   TREE_USED (decl) = 1;
    2169        53804 :   DECL_ARTIFICIAL (decl) = 1;
    2170        53804 :   DECL_IGNORED_P (decl) = 0;
    2171        53804 :   TREE_PUBLIC (decl) = 0;
    2172        53804 :   DECL_UNINLINABLE (decl) = 1;
    2173        53804 :   DECL_EXTERNAL (decl) = 0;
    2174        53804 :   DECL_CONTEXT (decl) = NULL_TREE;
    2175        53804 :   DECL_INITIAL (decl) = make_node (BLOCK);
    2176        53804 :   BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
    2177        53804 :   DECL_ATTRIBUTES (decl) = DECL_ATTRIBUTES (current_function_decl);
    2178              :   /* Remove omp declare simd attribute from the new attributes.  */
    2179        53804 :   if (tree a = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (decl)))
    2180              :     {
    2181            9 :       while (tree a2 = lookup_attribute ("omp declare simd", TREE_CHAIN (a)))
    2182              :         a = a2;
    2183            9 :       a = TREE_CHAIN (a);
    2184           22 :       for (tree *p = &DECL_ATTRIBUTES (decl); *p != a;)
    2185           13 :         if (is_attribute_p ("omp declare simd", get_attribute_name (*p)))
    2186            9 :           *p = TREE_CHAIN (*p);
    2187              :         else
    2188              :           {
    2189            4 :             tree chain = TREE_CHAIN (*p);
    2190            4 :             *p = copy_node (*p);
    2191            4 :             p = &TREE_CHAIN (*p);
    2192            4 :             *p = chain;
    2193              :           }
    2194              :     }
    2195       107608 :   DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
    2196        53804 :     = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (current_function_decl);
    2197       107608 :   DECL_FUNCTION_SPECIFIC_TARGET (decl)
    2198        53804 :     = DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl);
    2199       107608 :   DECL_FUNCTION_VERSIONED (decl)
    2200        53804 :     = DECL_FUNCTION_VERSIONED (current_function_decl);
    2201              : 
    2202        53804 :   if (omp_maybe_offloaded_ctx (ctx) && !host_only)
    2203              :     {
    2204        32806 :       cgraph_node::get_create (decl)->offloadable = 1;
    2205        32806 :       if (ENABLE_OFFLOADING)
    2206              :         g->have_offload = true;
    2207              :     }
    2208              : 
    2209        53804 :   if (cgraph_node::get_create (decl)->offloadable)
    2210              :     {
    2211        32806 :       const char *target_attr = (is_gimple_omp_offloaded (ctx->stmt)
    2212        32806 :                                  ? "omp target entrypoint"
    2213         6884 :                                  : "omp declare target");
    2214        32806 :       if (lookup_attribute ("omp declare target",
    2215        32806 :                             DECL_ATTRIBUTES (current_function_decl)))
    2216              :         {
    2217         3034 :           if (is_gimple_omp_offloaded (ctx->stmt))
    2218         1620 :             DECL_ATTRIBUTES (decl)
    2219         1620 :               = remove_attribute ("omp declare target",
    2220         1620 :                                   copy_list (DECL_ATTRIBUTES (decl)));
    2221              :           else
    2222              :             target_attr = NULL;
    2223              :         }
    2224         1620 :       if (target_attr
    2225        31392 :           && is_gimple_omp_offloaded (ctx->stmt)
    2226        25922 :           && lookup_attribute ("noclone", DECL_ATTRIBUTES (decl)) == NULL_TREE)
    2227        20415 :         DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("noclone"),
    2228        20415 :                                            NULL_TREE, DECL_ATTRIBUTES (decl));
    2229        32806 :       if (target_attr)
    2230        31392 :         DECL_ATTRIBUTES (decl)
    2231        62784 :           = tree_cons (get_identifier (target_attr),
    2232        31392 :                        NULL_TREE, DECL_ATTRIBUTES (decl));
    2233              :     }
    2234              : 
    2235        53804 :   t = build_decl (DECL_SOURCE_LOCATION (decl),
    2236              :                   RESULT_DECL, NULL_TREE, void_type_node);
    2237        53804 :   DECL_ARTIFICIAL (t) = 1;
    2238        53804 :   DECL_IGNORED_P (t) = 1;
    2239        53804 :   DECL_CONTEXT (t) = decl;
    2240        53804 :   DECL_RESULT (decl) = t;
    2241              : 
    2242        53804 :   tree data_name = get_identifier (".omp_data_i");
    2243        53804 :   t = build_decl (DECL_SOURCE_LOCATION (decl), PARM_DECL, data_name,
    2244              :                   ptr_type_node);
    2245        53804 :   DECL_ARTIFICIAL (t) = 1;
    2246        53804 :   DECL_NAMELESS (t) = 1;
    2247        53804 :   DECL_ARG_TYPE (t) = ptr_type_node;
    2248        53804 :   DECL_CONTEXT (t) = current_function_decl;
    2249        53804 :   TREE_USED (t) = 1;
    2250        53804 :   TREE_READONLY (t) = 1;
    2251        53804 :   DECL_ARGUMENTS (decl) = t;
    2252        53804 :   if (!task_copy)
    2253        53253 :     ctx->receiver_decl = t;
    2254              :   else
    2255              :     {
    2256          551 :       t = build_decl (DECL_SOURCE_LOCATION (decl),
    2257              :                       PARM_DECL, get_identifier (".omp_data_o"),
    2258              :                       ptr_type_node);
    2259          551 :       DECL_ARTIFICIAL (t) = 1;
    2260          551 :       DECL_NAMELESS (t) = 1;
    2261          551 :       DECL_ARG_TYPE (t) = ptr_type_node;
    2262          551 :       DECL_CONTEXT (t) = current_function_decl;
    2263          551 :       TREE_USED (t) = 1;
    2264          551 :       TREE_ADDRESSABLE (t) = 1;
    2265          551 :       DECL_CHAIN (t) = DECL_ARGUMENTS (decl);
    2266          551 :       DECL_ARGUMENTS (decl) = t;
    2267              :     }
    2268              : 
    2269              :   /* Allocate memory for the function structure.  The call to
    2270              :      allocate_struct_function clobbers CFUN, so we need to restore
    2271              :      it afterward.  */
    2272        53804 :   push_struct_function (decl);
    2273        53804 :   cfun->function_end_locus = gimple_location (ctx->stmt);
    2274        53804 :   init_tree_ssa (cfun);
    2275        53804 :   pop_cfun ();
    2276        53804 : }
    2277              : 
    2278              : /* Callback for walk_gimple_seq.  Check if combined parallel
    2279              :    contains gimple_omp_for_combined_into_p OMP_FOR.  */
    2280              : 
    2281              : tree
    2282        37397 : omp_find_combined_for (gimple_stmt_iterator *gsi_p,
    2283              :                        bool *handled_ops_p,
    2284              :                        struct walk_stmt_info *wi)
    2285              : {
    2286        37397 :   gimple *stmt = gsi_stmt (*gsi_p);
    2287              : 
    2288        37397 :   *handled_ops_p = true;
    2289        37397 :   switch (gimple_code (stmt))
    2290              :     {
    2291        20840 :     WALK_SUBSTMTS;
    2292              : 
    2293        14266 :     case GIMPLE_OMP_FOR:
    2294        14266 :       if (gimple_omp_for_combined_into_p (stmt)
    2295        14266 :           && gimple_omp_for_kind (stmt)
    2296         8630 :              == *(const enum gf_mask *) (wi->info))
    2297              :         {
    2298         8630 :           wi->info = stmt;
    2299         8630 :           return integer_zero_node;
    2300              :         }
    2301              :       break;
    2302              :     default:
    2303              :       break;
    2304              :     }
    2305              :   return NULL;
    2306              : }
    2307              : 
    2308              : /* Add _LOOPTEMP_/_REDUCTEMP_ clauses on OpenMP parallel or task.  */
    2309              : 
    2310              : static void
    2311        14851 : add_taskreg_looptemp_clauses (enum gf_mask msk, gimple *stmt,
    2312              :                               omp_context *outer_ctx)
    2313              : {
    2314        14851 :   struct walk_stmt_info wi;
    2315              : 
    2316        14851 :   memset (&wi, 0, sizeof (wi));
    2317        14851 :   wi.val_only = true;
    2318        14851 :   wi.info = (void *) &msk;
    2319        14851 :   walk_gimple_seq (gimple_omp_body (stmt), omp_find_combined_for, NULL, &wi);
    2320        14851 :   if (wi.info != (void *) &msk)
    2321              :     {
    2322         8630 :       gomp_for *for_stmt = as_a <gomp_for *> ((gimple *) wi.info);
    2323         8630 :       struct omp_for_data fd;
    2324         8630 :       omp_extract_for_data (for_stmt, &fd, NULL);
    2325              :       /* We need two temporaries with fd.loop.v type (istart/iend)
    2326              :          and then (fd.collapse - 1) temporaries with the same
    2327              :          type for count2 ... countN-1 vars if not constant.  */
    2328         8630 :       size_t count = 2, i;
    2329         8630 :       tree type = fd.iter_type;
    2330         8630 :       if (fd.collapse > 1
    2331         3263 :           && TREE_CODE (fd.loop.n2) != INTEGER_CST)
    2332              :         {
    2333         1634 :           count += fd.collapse - 1;
    2334              :           /* If there are lastprivate clauses on the inner
    2335              :              GIMPLE_OMP_FOR, add one more temporaries for the total number
    2336              :              of iterations (product of count1 ... countN-1).  */
    2337         1634 :           if (omp_find_clause (gimple_omp_for_clauses (for_stmt),
    2338              :                                OMP_CLAUSE_LASTPRIVATE)
    2339         1634 :               || (msk == GF_OMP_FOR_KIND_FOR
    2340         1477 :                   && omp_find_clause (gimple_omp_parallel_clauses (stmt),
    2341              :                                       OMP_CLAUSE_LASTPRIVATE)))
    2342              :             {
    2343          850 :               tree temp = create_tmp_var (type);
    2344          850 :               tree c = build_omp_clause (UNKNOWN_LOCATION,
    2345              :                                          OMP_CLAUSE__LOOPTEMP_);
    2346          850 :               insert_decl_map (&outer_ctx->cb, temp, temp);
    2347          850 :               OMP_CLAUSE_DECL (c) = temp;
    2348          850 :               OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
    2349          850 :               gimple_omp_taskreg_set_clauses (stmt, c);
    2350              :             }
    2351         1634 :           if (fd.non_rect
    2352           24 :               && fd.last_nonrect == fd.first_nonrect + 1)
    2353           12 :             if (tree v = gimple_omp_for_index (for_stmt, fd.last_nonrect))
    2354           12 :               if (!TYPE_UNSIGNED (TREE_TYPE (v)))
    2355              :                 {
    2356           12 :                   v = gimple_omp_for_index (for_stmt, fd.first_nonrect);
    2357           12 :                   tree type2 = TREE_TYPE (v);
    2358           12 :                   count++;
    2359           48 :                   for (i = 0; i < 3; i++)
    2360              :                     {
    2361           36 :                       tree temp = create_tmp_var (type2);
    2362           36 :                       tree c = build_omp_clause (UNKNOWN_LOCATION,
    2363              :                                                  OMP_CLAUSE__LOOPTEMP_);
    2364           36 :                       insert_decl_map (&outer_ctx->cb, temp, temp);
    2365           36 :                       OMP_CLAUSE_DECL (c) = temp;
    2366           36 :                       OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
    2367           36 :                       gimple_omp_taskreg_set_clauses (stmt, c);
    2368              :                     }
    2369              :                 }
    2370              :         }
    2371        28919 :       for (i = 0; i < count; i++)
    2372              :         {
    2373        20289 :           tree temp = create_tmp_var (type);
    2374        20289 :           tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__LOOPTEMP_);
    2375        20289 :           insert_decl_map (&outer_ctx->cb, temp, temp);
    2376        20289 :           OMP_CLAUSE_DECL (c) = temp;
    2377        20289 :           OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
    2378        20289 :           gimple_omp_taskreg_set_clauses (stmt, c);
    2379              :         }
    2380              :     }
    2381        14851 :   if (msk == GF_OMP_FOR_KIND_TASKLOOP
    2382        16419 :       && omp_find_clause (gimple_omp_task_clauses (stmt),
    2383              :                           OMP_CLAUSE_REDUCTION))
    2384              :     {
    2385          517 :       tree type = build_pointer_type (pointer_sized_int_node);
    2386          517 :       tree temp = create_tmp_var (type);
    2387          517 :       tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
    2388          517 :       insert_decl_map (&outer_ctx->cb, temp, temp);
    2389          517 :       OMP_CLAUSE_DECL (c) = temp;
    2390          517 :       OMP_CLAUSE_CHAIN (c) = gimple_omp_task_clauses (stmt);
    2391          517 :       gimple_omp_task_set_clauses (stmt, c);
    2392              :     }
    2393        14851 : }
    2394              : 
    2395              : /* Scan an OpenMP parallel directive.  */
    2396              : 
    2397              : static void
    2398        19105 : scan_omp_parallel (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
    2399              : {
    2400        19105 :   omp_context *ctx;
    2401        19105 :   tree name;
    2402        19105 :   gomp_parallel *stmt = as_a <gomp_parallel *> (gsi_stmt (*gsi));
    2403              : 
    2404              :   /* Ignore parallel directives with empty bodies, unless there
    2405              :      are copyin clauses.  */
    2406        19105 :   if (optimize > 0
    2407        13291 :       && empty_body_p (gimple_omp_body (stmt))
    2408        19152 :       && omp_find_clause (gimple_omp_parallel_clauses (stmt),
    2409              :                           OMP_CLAUSE_COPYIN) == NULL)
    2410              :     {
    2411           41 :       gsi_replace (gsi, gimple_build_nop (), false);
    2412           41 :       return;
    2413              :     }
    2414              : 
    2415        19064 :   if (gimple_omp_parallel_combined_p (stmt))
    2416        13283 :     add_taskreg_looptemp_clauses (GF_OMP_FOR_KIND_FOR, stmt, outer_ctx);
    2417        19064 :   for (tree c = omp_find_clause (gimple_omp_parallel_clauses (stmt),
    2418              :                                  OMP_CLAUSE_REDUCTION);
    2419        21945 :        c; c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_REDUCTION))
    2420         4212 :     if (OMP_CLAUSE_REDUCTION_TASK (c))
    2421              :       {
    2422           69 :         tree type = build_pointer_type (pointer_sized_int_node);
    2423           69 :         tree temp = create_tmp_var (type);
    2424           69 :         tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
    2425           69 :         if (outer_ctx)
    2426           18 :           insert_decl_map (&outer_ctx->cb, temp, temp);
    2427           69 :         OMP_CLAUSE_DECL (c) = temp;
    2428           69 :         OMP_CLAUSE_CHAIN (c) = gimple_omp_parallel_clauses (stmt);
    2429           69 :         gimple_omp_parallel_set_clauses (stmt, c);
    2430           69 :         break;
    2431              :       }
    2432         4143 :     else if (OMP_CLAUSE_CHAIN (c) == NULL_TREE)
    2433              :       break;
    2434              : 
    2435        19064 :   ctx = new_omp_context (stmt, outer_ctx);
    2436        19064 :   taskreg_contexts.safe_push (ctx);
    2437        19064 :   if (taskreg_nesting_level > 1)
    2438         6927 :     ctx->is_nested = true;
    2439        19064 :   ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
    2440        19064 :   ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
    2441        19064 :   name = create_tmp_var_name (".omp_data_s");
    2442        19064 :   name = build_decl (gimple_location (stmt),
    2443              :                      TYPE_DECL, name, ctx->record_type);
    2444        19064 :   DECL_ARTIFICIAL (name) = 1;
    2445        19064 :   DECL_NAMELESS (name) = 1;
    2446        19064 :   TYPE_NAME (ctx->record_type) = name;
    2447        19064 :   TYPE_ARTIFICIAL (ctx->record_type) = 1;
    2448        19064 :   create_omp_child_function (ctx, false, false);
    2449        19064 :   gimple_omp_parallel_set_child_fn (stmt, ctx->cb.dst_fn);
    2450              : 
    2451        19064 :   scan_sharing_clauses (gimple_omp_parallel_clauses (stmt), ctx);
    2452        19064 :   scan_omp (gimple_omp_body_ptr (stmt), ctx);
    2453              : 
    2454        19064 :   if (TYPE_FIELDS (ctx->record_type) == NULL)
    2455         3617 :     ctx->record_type = ctx->receiver_decl = NULL;
    2456              : }
    2457              : 
    2458              : /* Scan an OpenMP task directive.  */
    2459              : 
    2460              : static void
    2461         5653 : scan_omp_task (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
    2462              : {
    2463         5653 :   omp_context *ctx;
    2464         5653 :   tree name, t;
    2465         5653 :   gomp_task *stmt = as_a <gomp_task *> (gsi_stmt (*gsi));
    2466              : 
    2467              :   /* Ignore task directives with empty bodies, unless they have depend
    2468              :      clause.  */
    2469         5653 :   if (optimize > 0
    2470         2577 :       && gimple_omp_body (stmt)
    2471         2534 :       && empty_body_p (gimple_omp_body (stmt))
    2472         5709 :       && !omp_find_clause (gimple_omp_task_clauses (stmt), OMP_CLAUSE_DEPEND))
    2473              :     {
    2474           21 :       gsi_replace (gsi, gimple_build_nop (), false);
    2475          135 :       return;
    2476              :     }
    2477              : 
    2478         5632 :   if (gimple_omp_task_taskloop_p (stmt))
    2479         1568 :     add_taskreg_looptemp_clauses (GF_OMP_FOR_KIND_TASKLOOP, stmt, outer_ctx);
    2480              : 
    2481         5632 :   ctx = new_omp_context (stmt, outer_ctx);
    2482              : 
    2483         5632 :   if (gimple_omp_task_taskwait_p (stmt))
    2484              :     {
    2485           93 :       scan_sharing_clauses (gimple_omp_task_clauses (stmt), ctx);
    2486           93 :       return;
    2487              :     }
    2488              : 
    2489         5539 :   taskreg_contexts.safe_push (ctx);
    2490         5539 :   if (taskreg_nesting_level > 1)
    2491         2342 :     ctx->is_nested = true;
    2492         5539 :   ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
    2493         5539 :   ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
    2494         5539 :   name = create_tmp_var_name (".omp_data_s");
    2495         5539 :   name = build_decl (gimple_location (stmt),
    2496              :                      TYPE_DECL, name, ctx->record_type);
    2497         5539 :   DECL_ARTIFICIAL (name) = 1;
    2498         5539 :   DECL_NAMELESS (name) = 1;
    2499         5539 :   TYPE_NAME (ctx->record_type) = name;
    2500         5539 :   TYPE_ARTIFICIAL (ctx->record_type) = 1;
    2501         5539 :   create_omp_child_function (ctx, false, false);
    2502         5539 :   gimple_omp_task_set_child_fn (stmt, ctx->cb.dst_fn);
    2503              : 
    2504         5539 :   scan_sharing_clauses (gimple_omp_task_clauses (stmt), ctx);
    2505              : 
    2506         5539 :   if (ctx->srecord_type)
    2507              :     {
    2508          551 :       name = create_tmp_var_name (".omp_data_a");
    2509          551 :       name = build_decl (gimple_location (stmt),
    2510              :                          TYPE_DECL, name, ctx->srecord_type);
    2511          551 :       DECL_ARTIFICIAL (name) = 1;
    2512          551 :       DECL_NAMELESS (name) = 1;
    2513          551 :       TYPE_NAME (ctx->srecord_type) = name;
    2514          551 :       TYPE_ARTIFICIAL (ctx->srecord_type) = 1;
    2515          551 :       create_omp_child_function (ctx, true, false);
    2516              :     }
    2517              : 
    2518         5539 :   scan_omp (gimple_omp_body_ptr (stmt), ctx);
    2519              : 
    2520         5539 :   if (TYPE_FIELDS (ctx->record_type) == NULL)
    2521              :     {
    2522         2082 :       ctx->record_type = ctx->receiver_decl = NULL;
    2523         2082 :       t = build_int_cst (long_integer_type_node, 0);
    2524         2082 :       gimple_omp_task_set_arg_size (stmt, t);
    2525         2082 :       t = build_int_cst (long_integer_type_node, 1);
    2526         2082 :       gimple_omp_task_set_arg_align (stmt, t);
    2527              :     }
    2528              : }
    2529              : 
    2530              : /* Helper function for finish_taskreg_scan, called through walk_tree.
    2531              :    If maybe_lookup_decl_in_outer_context returns non-NULL for some
    2532              :    tree, replace it in the expression.  */
    2533              : 
    2534              : static tree
    2535          276 : finish_taskreg_remap (tree *tp, int *walk_subtrees, void *data)
    2536              : {
    2537          276 :   if (VAR_P (*tp))
    2538              :     {
    2539           96 :       omp_context *ctx = (omp_context *) data;
    2540           96 :       tree t = maybe_lookup_decl_in_outer_ctx (*tp, ctx);
    2541           96 :       if (t != *tp)
    2542              :         {
    2543            9 :           if (DECL_HAS_VALUE_EXPR_P (t))
    2544            0 :             t = unshare_expr (DECL_VALUE_EXPR (t));
    2545            9 :           *tp = t;
    2546              :         }
    2547           96 :       *walk_subtrees = 0;
    2548              :     }
    2549          180 :   else if (IS_TYPE_OR_DECL_P (*tp))
    2550            0 :     *walk_subtrees = 0;
    2551          276 :   return NULL_TREE;
    2552              : }
    2553              : 
    2554              : /* If any decls have been made addressable during scan_omp,
    2555              :    adjust their fields if needed, and layout record types
    2556              :    of parallel/task constructs.  */
    2557              : 
    2558              : static void
    2559        27325 : finish_taskreg_scan (omp_context *ctx)
    2560              : {
    2561        27325 :   if (ctx->record_type == NULL_TREE)
    2562              :     return;
    2563              : 
    2564              :   /* If any make_addressable_vars were needed, verify all
    2565              :      OMP_CLAUSE_SHARED clauses on GIMPLE_OMP_{PARALLEL,TASK,TEAMS}
    2566              :      statements if use_pointer_for_field hasn't changed
    2567              :      because of that.  If it did, update field types now.  */
    2568        20171 :   if (make_addressable_vars)
    2569              :     {
    2570         2216 :       tree c;
    2571              : 
    2572        12369 :       for (c = gimple_omp_taskreg_clauses (ctx->stmt);
    2573        12369 :            c; c = OMP_CLAUSE_CHAIN (c))
    2574        10153 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
    2575        10153 :             && !OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
    2576              :           {
    2577         2617 :             tree decl = OMP_CLAUSE_DECL (c);
    2578              : 
    2579              :             /* Global variables don't need to be copied,
    2580              :                the receiver side will use them directly.  */
    2581         2617 :             if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
    2582          282 :               continue;
    2583         2335 :             if (!bitmap_bit_p (make_addressable_vars, DECL_UID (decl))
    2584         2335 :                 || !use_pointer_for_field (decl, ctx))
    2585         1784 :               continue;
    2586          551 :             tree field = lookup_field (decl, ctx);
    2587          551 :             if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE
    2588          551 :                 && TREE_TYPE (TREE_TYPE (field)) == TREE_TYPE (decl))
    2589          497 :               continue;
    2590           54 :             TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
    2591           54 :             TREE_THIS_VOLATILE (field) = 0;
    2592           54 :             DECL_USER_ALIGN (field) = 0;
    2593           54 :             SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
    2594           54 :             if (TYPE_ALIGN (ctx->record_type) < DECL_ALIGN (field))
    2595           16 :               SET_TYPE_ALIGN (ctx->record_type, DECL_ALIGN (field));
    2596           54 :             if (ctx->srecord_type)
    2597              :               {
    2598            0 :                 tree sfield = lookup_sfield (decl, ctx);
    2599            0 :                 TREE_TYPE (sfield) = TREE_TYPE (field);
    2600            0 :                 TREE_THIS_VOLATILE (sfield) = 0;
    2601            0 :                 DECL_USER_ALIGN (sfield) = 0;
    2602            0 :                 SET_DECL_ALIGN (sfield, DECL_ALIGN (field));
    2603            0 :                 if (TYPE_ALIGN (ctx->srecord_type) < DECL_ALIGN (sfield))
    2604            0 :                   SET_TYPE_ALIGN (ctx->srecord_type, DECL_ALIGN (sfield));
    2605              :               }
    2606              :           }
    2607              :     }
    2608              : 
    2609        20171 :   if (gimple_code (ctx->stmt) == GIMPLE_OMP_PARALLEL)
    2610              :     {
    2611        15447 :       tree clauses = gimple_omp_parallel_clauses (ctx->stmt);
    2612        15447 :       tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
    2613        15447 :       if (c)
    2614              :         {
    2615              :           /* Move the _reductemp_ clause first.  GOMP_parallel_reductions
    2616              :              expects to find it at the start of data.  */
    2617           69 :           tree f = lookup_field (OMP_CLAUSE_DECL (c), ctx);
    2618           69 :           tree *p = &TYPE_FIELDS (ctx->record_type);
    2619          155 :           while (*p)
    2620          155 :             if (*p == f)
    2621              :               {
    2622           69 :                 *p = DECL_CHAIN (*p);
    2623           69 :                 break;
    2624              :               }
    2625              :             else
    2626           86 :               p = &DECL_CHAIN (*p);
    2627           69 :           DECL_CHAIN (f) = TYPE_FIELDS (ctx->record_type);
    2628           69 :           TYPE_FIELDS (ctx->record_type) = f;
    2629              :         }
    2630        15447 :       layout_type (ctx->record_type);
    2631        15447 :       fixup_child_record_type (ctx);
    2632              :     }
    2633         4724 :   else if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS)
    2634              :     {
    2635         1267 :       layout_type (ctx->record_type);
    2636         1267 :       fixup_child_record_type (ctx);
    2637              :     }
    2638              :   else
    2639              :     {
    2640         3457 :       location_t loc = gimple_location (ctx->stmt);
    2641         3457 :       tree *p, vla_fields = NULL_TREE, *q = &vla_fields;
    2642         3457 :       tree detach_clause
    2643         3457 :         = omp_find_clause (gimple_omp_task_clauses (ctx->stmt),
    2644              :                            OMP_CLAUSE_DETACH);
    2645              :       /* Move VLA fields to the end.  */
    2646         3457 :       p = &TYPE_FIELDS (ctx->record_type);
    2647        15417 :       while (*p)
    2648        11960 :         if (!TYPE_SIZE_UNIT (TREE_TYPE (*p))
    2649        11960 :             || ! TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (*p))))
    2650              :           {
    2651           96 :             *q = *p;
    2652           96 :             *p = TREE_CHAIN (*p);
    2653           96 :             TREE_CHAIN (*q) = NULL_TREE;
    2654           96 :             q = &TREE_CHAIN (*q);
    2655              :           }
    2656              :         else
    2657        11864 :           p = &DECL_CHAIN (*p);
    2658         3457 :       *p = vla_fields;
    2659         3457 :       if (gimple_omp_task_taskloop_p (ctx->stmt))
    2660              :         {
    2661              :           /* Move fields corresponding to first and second _looptemp_
    2662              :              clause first.  There are filled by GOMP_taskloop
    2663              :              and thus need to be in specific positions.  */
    2664         1568 :           tree clauses = gimple_omp_task_clauses (ctx->stmt);
    2665         1568 :           tree c1 = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
    2666         1568 :           tree c2 = omp_find_clause (OMP_CLAUSE_CHAIN (c1),
    2667              :                                      OMP_CLAUSE__LOOPTEMP_);
    2668         1568 :           tree c3 = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
    2669         1568 :           tree f1 = lookup_field (OMP_CLAUSE_DECL (c1), ctx);
    2670         1568 :           tree f2 = lookup_field (OMP_CLAUSE_DECL (c2), ctx);
    2671         2085 :           tree f3 = c3 ? lookup_field (OMP_CLAUSE_DECL (c3), ctx) : NULL_TREE;
    2672         1568 :           p = &TYPE_FIELDS (ctx->record_type);
    2673         8878 :           while (*p)
    2674         7310 :             if (*p == f1 || *p == f2 || *p == f3)
    2675         3653 :               *p = DECL_CHAIN (*p);
    2676              :             else
    2677         3657 :               p = &DECL_CHAIN (*p);
    2678         1568 :           DECL_CHAIN (f1) = f2;
    2679         1568 :           if (c3)
    2680              :             {
    2681          517 :               DECL_CHAIN (f2) = f3;
    2682          517 :               DECL_CHAIN (f3) = TYPE_FIELDS (ctx->record_type);
    2683              :             }
    2684              :           else
    2685         1051 :             DECL_CHAIN (f2) = TYPE_FIELDS (ctx->record_type);
    2686         1568 :           TYPE_FIELDS (ctx->record_type) = f1;
    2687         1568 :           if (ctx->srecord_type)
    2688              :             {
    2689          390 :               f1 = lookup_sfield (OMP_CLAUSE_DECL (c1), ctx);
    2690          390 :               f2 = lookup_sfield (OMP_CLAUSE_DECL (c2), ctx);
    2691          390 :               if (c3)
    2692          186 :                 f3 = lookup_sfield (OMP_CLAUSE_DECL (c3), ctx);
    2693          390 :               p = &TYPE_FIELDS (ctx->srecord_type);
    2694         2104 :               while (*p)
    2695         1714 :                 if (*p == f1 || *p == f2 || *p == f3)
    2696          966 :                   *p = DECL_CHAIN (*p);
    2697              :                 else
    2698          748 :                   p = &DECL_CHAIN (*p);
    2699          390 :               DECL_CHAIN (f1) = f2;
    2700          390 :               DECL_CHAIN (f2) = TYPE_FIELDS (ctx->srecord_type);
    2701          390 :               if (c3)
    2702              :                 {
    2703          186 :                   DECL_CHAIN (f2) = f3;
    2704          186 :                   DECL_CHAIN (f3) = TYPE_FIELDS (ctx->srecord_type);
    2705              :                 }
    2706              :               else
    2707          204 :                 DECL_CHAIN (f2) = TYPE_FIELDS (ctx->srecord_type);
    2708          390 :               TYPE_FIELDS (ctx->srecord_type) = f1;
    2709              :             }
    2710              :         }
    2711         3457 :       if (detach_clause)
    2712              :         {
    2713          189 :           tree c, field;
    2714              : 
    2715              :           /* Look for a firstprivate clause with the detach event handle.  */
    2716          540 :           for (c = gimple_omp_taskreg_clauses (ctx->stmt);
    2717          540 :                c; c = OMP_CLAUSE_CHAIN (c))
    2718              :             {
    2719          540 :               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
    2720          347 :                 continue;
    2721          193 :               if (maybe_lookup_decl_in_outer_ctx (OMP_CLAUSE_DECL (c), ctx)
    2722          193 :                   == OMP_CLAUSE_DECL (detach_clause))
    2723              :                 break;
    2724              :             }
    2725              : 
    2726          189 :           gcc_assert (c);
    2727          189 :           field = lookup_field (OMP_CLAUSE_DECL (c), ctx);
    2728              : 
    2729              :           /* Move field corresponding to the detach clause first.
    2730              :              This is filled by GOMP_task and needs to be in a
    2731              :              specific position.  */
    2732          189 :           p = &TYPE_FIELDS (ctx->record_type);
    2733          518 :           while (*p)
    2734          329 :             if (*p == field)
    2735          189 :               *p = DECL_CHAIN (*p);
    2736              :             else
    2737          140 :               p = &DECL_CHAIN (*p);
    2738          189 :           DECL_CHAIN (field) = TYPE_FIELDS (ctx->record_type);
    2739          189 :           TYPE_FIELDS (ctx->record_type) = field;
    2740          189 :           if (ctx->srecord_type)
    2741              :             {
    2742            3 :               field = lookup_sfield (OMP_CLAUSE_DECL (c), ctx);
    2743            3 :               p = &TYPE_FIELDS (ctx->srecord_type);
    2744            9 :               while (*p)
    2745            6 :                 if (*p == field)
    2746            3 :                   *p = DECL_CHAIN (*p);
    2747              :                 else
    2748            3 :                   p = &DECL_CHAIN (*p);
    2749            3 :               DECL_CHAIN (field) = TYPE_FIELDS (ctx->srecord_type);
    2750            3 :               TYPE_FIELDS (ctx->srecord_type) = field;
    2751              :             }
    2752              :         }
    2753         3457 :       layout_type (ctx->record_type);
    2754         3457 :       fixup_child_record_type (ctx);
    2755         3457 :       if (ctx->srecord_type)
    2756          551 :         layout_type (ctx->srecord_type);
    2757         3457 :       tree t = fold_convert_loc (loc, long_integer_type_node,
    2758         3457 :                                  TYPE_SIZE_UNIT (ctx->record_type));
    2759         3457 :       if (TREE_CODE (t) != INTEGER_CST)
    2760              :         {
    2761           21 :           t = unshare_expr (t);
    2762           21 :           walk_tree (&t, finish_taskreg_remap, ctx, NULL);
    2763              :         }
    2764         3457 :       gimple_omp_task_set_arg_size (ctx->stmt, t);
    2765         3457 :       t = build_int_cst (long_integer_type_node,
    2766         3457 :                          TYPE_ALIGN_UNIT (ctx->record_type));
    2767         3457 :       gimple_omp_task_set_arg_align (ctx->stmt, t);
    2768              :     }
    2769              : }
    2770              : 
    2771              : /* Find the enclosing offload context.  */
    2772              : 
    2773              : static omp_context *
    2774         9665 : enclosing_target_ctx (omp_context *ctx)
    2775              : {
    2776        43359 :   for (; ctx; ctx = ctx->outer)
    2777        41745 :     if (gimple_code (ctx->stmt) == GIMPLE_OMP_TARGET)
    2778              :       break;
    2779              : 
    2780        22111 :   return ctx;
    2781              : }
    2782              : 
    2783              : /* Return whether CTX's parent compute construct is an OpenACC 'kernels'
    2784              :    construct.
    2785              :    (This doesn't include OpenACC 'kernels' decomposed parts.)  */
    2786              : 
    2787              : static bool
    2788        11325 : ctx_in_oacc_kernels_region (omp_context *ctx)
    2789              : {
    2790        39137 :   for (;ctx != NULL; ctx = ctx->outer)
    2791              :     {
    2792        29472 :       gimple *stmt = ctx->stmt;
    2793        29472 :       if (gimple_code (stmt) == GIMPLE_OMP_TARGET
    2794        29472 :           && gimple_omp_target_kind (stmt) == GF_OMP_TARGET_KIND_OACC_KERNELS)
    2795              :         return true;
    2796              :     }
    2797              : 
    2798              :   return false;
    2799              : }
    2800              : 
    2801              : /* Check the parallelism clauses inside a OpenACC 'kernels' region.
    2802              :    (This doesn't include OpenACC 'kernels' decomposed parts.)
    2803              :    Until kernels handling moves to use the same loop indirection
    2804              :    scheme as parallel, we need to do this checking early.  */
    2805              : 
    2806              : static unsigned
    2807         7151 : check_oacc_kernel_gwv (gomp_for *stmt, omp_context *ctx)
    2808              : {
    2809         7151 :   bool checking = true;
    2810         7151 :   unsigned outer_mask = 0;
    2811         7151 :   unsigned this_mask = 0;
    2812         7151 :   bool has_seq = false, has_auto = false;
    2813              : 
    2814         7151 :   if (ctx->outer)
    2815         4819 :     outer_mask = check_oacc_kernel_gwv (NULL,  ctx->outer);
    2816         7151 :   if (!stmt)
    2817              :     {
    2818         4819 :       checking = false;
    2819         4819 :       if (gimple_code (ctx->stmt) != GIMPLE_OMP_FOR)
    2820              :         return outer_mask;
    2821         1730 :       stmt = as_a <gomp_for *> (ctx->stmt);
    2822              :     }
    2823              : 
    2824        11330 :   for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
    2825              :     {
    2826         7268 :       switch (OMP_CLAUSE_CODE (c))
    2827              :         {
    2828          645 :         case OMP_CLAUSE_GANG:
    2829          645 :           this_mask |= GOMP_DIM_MASK (GOMP_DIM_GANG);
    2830          645 :           break;
    2831          439 :         case OMP_CLAUSE_WORKER:
    2832          439 :           this_mask |= GOMP_DIM_MASK (GOMP_DIM_WORKER);
    2833          439 :           break;
    2834          342 :         case OMP_CLAUSE_VECTOR:
    2835          342 :           this_mask |= GOMP_DIM_MASK (GOMP_DIM_VECTOR);
    2836          342 :           break;
    2837           63 :         case OMP_CLAUSE_SEQ:
    2838           63 :           has_seq = true;
    2839           63 :           break;
    2840           94 :         case OMP_CLAUSE_AUTO:
    2841           94 :           has_auto = true;
    2842           94 :           break;
    2843              :         default:
    2844              :           break;
    2845              :         }
    2846              :     }
    2847              : 
    2848         4062 :   if (checking)
    2849              :     {
    2850         2332 :       if (has_seq && (this_mask || has_auto))
    2851           36 :         error_at (gimple_location (stmt), "%<seq%> overrides other"
    2852              :                   " OpenACC loop specifiers");
    2853         2296 :       else if (has_auto && this_mask)
    2854           30 :         error_at (gimple_location (stmt), "%<auto%> conflicts with other"
    2855              :                   " OpenACC loop specifiers");
    2856              : 
    2857         2332 :       if (this_mask & outer_mask)
    2858           15 :         error_at (gimple_location (stmt), "inner loop uses same"
    2859              :                   " OpenACC parallelism as containing loop");
    2860              :     }
    2861              : 
    2862         4062 :   return outer_mask | this_mask;
    2863              : }
    2864              : 
    2865              : /* If DECL is the iteration variable of loop K in the collapsed loop nest
    2866              :    STMT, meaning that the gimplifier put "DECL = V" at the start of the body,
    2867              :    return K.  Otherwise return -1.  */
    2868              : 
    2869              : static int
    2870            0 : omp_find_iv_index (const gomp_for *stmt, tree decl)
    2871              : {
    2872            0 :   gimple_seq body = gimple_omp_body (stmt);
    2873            0 :   for (gimple_stmt_iterator gsi = gsi_start (body); !gsi_end_p (gsi);
    2874            0 :        gsi_next (&gsi))
    2875              :     {
    2876            0 :       gimple *s = gsi_stmt (gsi);
    2877            0 :       if (!gimple_assign_single_p (s))
    2878              :         break;
    2879            0 :       if (gimple_assign_lhs (s) == decl)
    2880            0 :         for (size_t k = 0; k < gimple_omp_for_collapse (stmt); k++)
    2881            0 :           if (gimple_assign_rhs1 (s) == gimple_omp_for_index (stmt, k))
    2882            0 :             return k;
    2883              :     }
    2884              :   return -1;
    2885              : }
    2886              : 
    2887              : /* Scan a GIMPLE_OMP_FOR.  */
    2888              : 
    2889              : static omp_context *
    2890        55355 : scan_omp_for (gomp_for *stmt, omp_context *outer_ctx)
    2891              : {
    2892        55355 :   omp_context *ctx;
    2893        55355 :   size_t i;
    2894        55355 :   tree clauses = gimple_omp_for_clauses (stmt);
    2895              : 
    2896        55355 :   ctx = new_omp_context (stmt, outer_ctx);
    2897              : 
    2898        55355 :   if (is_gimple_omp_oacc (stmt))
    2899              :     {
    2900        12446 :       omp_context *tgt = enclosing_target_ctx (outer_ctx);
    2901              : 
    2902        12446 :       if (!(tgt && is_oacc_kernels (tgt)))
    2903        32251 :         for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    2904              :           {
    2905        22137 :             tree c_op0;
    2906        22137 :             switch (OMP_CLAUSE_CODE (c))
    2907              :               {
    2908         1952 :               case OMP_CLAUSE_GANG:
    2909         1952 :                 c_op0 = OMP_CLAUSE_GANG_EXPR (c);
    2910         1952 :                 break;
    2911              : 
    2912         1426 :               case OMP_CLAUSE_WORKER:
    2913         1426 :                 c_op0 = OMP_CLAUSE_WORKER_EXPR (c);
    2914         1426 :                 break;
    2915              : 
    2916         1627 :               case OMP_CLAUSE_VECTOR:
    2917         1627 :                 c_op0 = OMP_CLAUSE_VECTOR_EXPR (c);
    2918         1627 :                 break;
    2919              : 
    2920        17132 :               default:
    2921        17132 :                 continue;
    2922              :               }
    2923              : 
    2924         5005 :             if (c_op0)
    2925              :               {
    2926              :                 /* By construction, this is impossible for OpenACC 'kernels'
    2927              :                    decomposed parts.  */
    2928          210 :                 gcc_assert (!(tgt && is_oacc_kernels_decomposed_part (tgt)));
    2929              : 
    2930          210 :                 error_at (OMP_CLAUSE_LOCATION (c),
    2931              :                           "argument not permitted on %qs clause",
    2932          210 :                           omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
    2933          210 :                 if (tgt)
    2934          180 :                   inform (gimple_location (tgt->stmt),
    2935              :                           "enclosing parent compute construct");
    2936           30 :                 else if (oacc_get_fn_attrib (current_function_decl))
    2937           30 :                   inform (DECL_SOURCE_LOCATION (current_function_decl),
    2938              :                           "enclosing routine");
    2939              :                 else
    2940            0 :                   gcc_unreachable ();
    2941              :               }
    2942              :           }
    2943              : 
    2944        12446 :       if (tgt && is_oacc_kernels (tgt))
    2945         2332 :         check_oacc_kernel_gwv (stmt, ctx);
    2946              : 
    2947              :       /* Collect all variables named in reductions on this loop.  Ensure
    2948              :          that, if this loop has a reduction on some variable v, and there is
    2949              :          a reduction on v somewhere in an outer context, then there is a
    2950              :          reduction on v on all intervening loops as well.  */
    2951        12446 :       tree local_reduction_clauses = NULL;
    2952        39374 :       for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
    2953              :         {
    2954        26928 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
    2955         4712 :             local_reduction_clauses
    2956         4712 :               = tree_cons (NULL, c, local_reduction_clauses);
    2957              :         }
    2958        12446 :       if (ctx->outer_reduction_clauses == NULL && ctx->outer != NULL)
    2959        11920 :         ctx->outer_reduction_clauses
    2960        11920 :           = chainon (unshare_expr (ctx->outer->local_reduction_clauses),
    2961              :                      ctx->outer->outer_reduction_clauses);
    2962        12446 :       tree outer_reduction_clauses = ctx->outer_reduction_clauses;
    2963        12446 :       tree local_iter = local_reduction_clauses;
    2964        17158 :       for (; local_iter; local_iter = TREE_CHAIN (local_iter))
    2965              :         {
    2966         4712 :           tree local_clause = TREE_VALUE (local_iter);
    2967         4712 :           tree local_var = OMP_CLAUSE_DECL (local_clause);
    2968         4712 :           tree_code local_op = OMP_CLAUSE_REDUCTION_CODE (local_clause);
    2969         4712 :           bool have_outer_reduction = false;
    2970         4712 :           tree ctx_iter = outer_reduction_clauses;
    2971         5826 :           for (; ctx_iter; ctx_iter = TREE_CHAIN (ctx_iter))
    2972              :             {
    2973         3325 :               tree outer_clause = TREE_VALUE (ctx_iter);
    2974         3325 :               tree outer_var = OMP_CLAUSE_DECL (outer_clause);
    2975         3325 :               tree_code outer_op = OMP_CLAUSE_REDUCTION_CODE (outer_clause);
    2976         3325 :               if (outer_var == local_var && outer_op != local_op)
    2977              :                 {
    2978          535 :                   if (warning_at (OMP_CLAUSE_LOCATION (local_clause),
    2979          535 :                                   OPT_Wopenmp, "conflicting reduction "
    2980              :                                                "operations for %qE",
    2981              :                                   local_var))
    2982          535 :                     inform (OMP_CLAUSE_LOCATION (outer_clause),
    2983              :                             "location of the previous reduction for %qE",
    2984              :                             outer_var);
    2985              :                 }
    2986         3325 :               if (outer_var == local_var)
    2987              :                 {
    2988              :                   have_outer_reduction = true;
    2989              :                   break;
    2990              :                 }
    2991              :             }
    2992         4712 :           if (have_outer_reduction)
    2993              :             {
    2994              :               /* There is a reduction on outer_var both on this loop and on
    2995              :                  some enclosing loop.  Walk up the context tree until such a
    2996              :                  loop with a reduction on outer_var is found, and complain
    2997              :                  about all intervening loops that do not have such a
    2998              :                  reduction.  */
    2999         2211 :               struct omp_context *curr_loop = ctx->outer;
    3000         2211 :               bool found = false;
    3001         2826 :               while (curr_loop != NULL)
    3002              :                 {
    3003         2826 :                   tree curr_iter = curr_loop->local_reduction_clauses;
    3004         3460 :                   for (; curr_iter; curr_iter = TREE_CHAIN (curr_iter))
    3005              :                     {
    3006         2845 :                       tree curr_clause = TREE_VALUE (curr_iter);
    3007         2845 :                       tree curr_var = OMP_CLAUSE_DECL (curr_clause);
    3008         2845 :                       if (curr_var == local_var)
    3009              :                         {
    3010              :                           found = true;
    3011              :                           break;
    3012              :                         }
    3013              :                     }
    3014         2826 :                   if (!found)
    3015          615 :                     warning_at (gimple_location (curr_loop->stmt), OPT_Wopenmp,
    3016              :                                 "nested loop in reduction needs "
    3017              :                                 "reduction clause for %qE",
    3018              :                                 local_var);
    3019              :                   else
    3020              :                     break;
    3021          615 :                   curr_loop = curr_loop->outer;
    3022              :                 }
    3023              :             }
    3024              :         }
    3025        12446 :       ctx->local_reduction_clauses = local_reduction_clauses;
    3026        12446 :       ctx->outer_reduction_clauses
    3027        12446 :         = chainon (unshare_expr (ctx->local_reduction_clauses),
    3028              :                    ctx->outer_reduction_clauses);
    3029              : 
    3030        12446 :       if (tgt && is_oacc_kernels (tgt))
    3031              :         {
    3032              :           /* Strip out reductions, as they are not handled yet.  */
    3033              :           tree *prev_ptr = &clauses;
    3034              : 
    3035         7123 :           while (tree probe = *prev_ptr)
    3036              :             {
    3037         4791 :               tree *next_ptr = &OMP_CLAUSE_CHAIN (probe);
    3038              : 
    3039         4791 :               if (OMP_CLAUSE_CODE (probe) == OMP_CLAUSE_REDUCTION)
    3040          718 :                 *prev_ptr = *next_ptr;
    3041              :               else
    3042              :                 prev_ptr = next_ptr;
    3043              :             }
    3044              : 
    3045         2332 :           gimple_omp_for_set_clauses (stmt, clauses);
    3046              :         }
    3047              :     }
    3048              : 
    3049        55355 :   scan_sharing_clauses (clauses, ctx);
    3050              : 
    3051              :   /* For SIMT, record which lastprivate clauses reference iteration
    3052              :      variables.  */
    3053        55355 :   if (gimple_omp_for_collapse (stmt) > 1
    3054        14291 :       && gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_SIMD
    3055        59186 :       && omp_find_clause (clauses, OMP_CLAUSE__SIMT_))
    3056            0 :     for (tree clause = clauses; clause; clause = OMP_CLAUSE_CHAIN (clause))
    3057            0 :       if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_LASTPRIVATE)
    3058              :         {
    3059            0 :           tree decl = OMP_CLAUSE_DECL (clause);
    3060            0 :           int k = omp_find_iv_index (stmt, decl);
    3061            0 :           if (k != -1)
    3062              :             {
    3063            0 :               if (ctx->lastprivate_iv_map == NULL)
    3064            0 :                 ctx->lastprivate_iv_map = new hash_map<tree, int>;
    3065            0 :               ctx->lastprivate_iv_map->put (decl, k);
    3066              :             }
    3067              :         }
    3068              : 
    3069        55355 :   scan_omp (gimple_omp_for_pre_body_ptr (stmt), ctx);
    3070       190574 :   for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
    3071              :     {
    3072        79864 :       scan_omp_op (gimple_omp_for_index_ptr (stmt, i), ctx);
    3073        79864 :       scan_omp_op (gimple_omp_for_initial_ptr (stmt, i), ctx);
    3074        79864 :       scan_omp_op (gimple_omp_for_final_ptr (stmt, i), ctx);
    3075        79864 :       scan_omp_op (gimple_omp_for_incr_ptr (stmt, i), ctx);
    3076              :     }
    3077        55355 :   scan_omp (gimple_omp_body_ptr (stmt), ctx);
    3078        55355 :   return ctx;
    3079              : }
    3080              : 
    3081              : /* Duplicate #pragma omp simd, one for SIMT, another one for SIMD.  */
    3082              : 
    3083              : static void
    3084            0 : scan_omp_simd (gimple_stmt_iterator *gsi, gomp_for *stmt,
    3085              :                omp_context *outer_ctx)
    3086              : {
    3087            0 :   gbind *bind = gimple_build_bind (NULL, NULL, NULL);
    3088            0 :   gsi_replace (gsi, bind, false);
    3089            0 :   gimple_seq seq = NULL;
    3090            0 :   gimple *g = gimple_build_call_internal (IFN_GOMP_USE_SIMT, 0);
    3091            0 :   tree cond = create_tmp_var_raw (integer_type_node);
    3092            0 :   DECL_CONTEXT (cond) = current_function_decl;
    3093            0 :   DECL_SEEN_IN_BIND_EXPR_P (cond) = 1;
    3094            0 :   gimple_bind_set_vars (bind, cond);
    3095            0 :   gimple_call_set_lhs (g, cond);
    3096            0 :   gimple_seq_add_stmt (&seq, g);
    3097            0 :   tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
    3098            0 :   tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
    3099            0 :   tree lab3 = create_artificial_label (UNKNOWN_LOCATION);
    3100            0 :   g = gimple_build_cond (NE_EXPR, cond, integer_zero_node, lab1, lab2);
    3101            0 :   gimple_seq_add_stmt (&seq, g);
    3102            0 :   g = gimple_build_label (lab1);
    3103            0 :   gimple_seq_add_stmt (&seq, g);
    3104            0 :   gimple_seq new_seq = copy_gimple_seq_and_replace_locals (stmt);
    3105            0 :   gomp_for *new_stmt = as_a <gomp_for *> (new_seq);
    3106            0 :   tree clause = build_omp_clause (gimple_location (stmt), OMP_CLAUSE__SIMT_);
    3107            0 :   OMP_CLAUSE_CHAIN (clause) = gimple_omp_for_clauses (new_stmt);
    3108            0 :   gimple_omp_for_set_clauses (new_stmt, clause);
    3109            0 :   gimple_seq_add_stmt (&seq, new_stmt);
    3110            0 :   g = gimple_build_goto (lab3);
    3111            0 :   gimple_seq_add_stmt (&seq, g);
    3112            0 :   g = gimple_build_label (lab2);
    3113            0 :   gimple_seq_add_stmt (&seq, g);
    3114            0 :   gimple_seq_add_stmt (&seq, stmt);
    3115            0 :   g = gimple_build_label (lab3);
    3116            0 :   gimple_seq_add_stmt (&seq, g);
    3117            0 :   gimple_bind_set_body (bind, seq);
    3118            0 :   update_stmt (bind);
    3119            0 :   scan_omp_for (new_stmt, outer_ctx);
    3120            0 :   scan_omp_for (stmt, outer_ctx)->simt_stmt = new_stmt;
    3121            0 : }
    3122              : 
    3123              : static tree omp_find_scan (gimple_stmt_iterator *, bool *,
    3124              :                            struct walk_stmt_info *);
    3125              : static omp_context *maybe_lookup_ctx (gimple *);
    3126              : 
    3127              : /* Duplicate #pragma omp simd, one for the scan input phase loop and one
    3128              :    for scan phase loop.  */
    3129              : 
    3130              : static void
    3131           93 : scan_omp_simd_scan (gimple_stmt_iterator *gsi, gomp_for *stmt,
    3132              :                     omp_context *outer_ctx)
    3133              : {
    3134              :   /* The only change between inclusive and exclusive scan will be
    3135              :      within the first simd loop, so just use inclusive in the
    3136              :      worksharing loop.  */
    3137           93 :   outer_ctx->scan_inclusive = true;
    3138           93 :   tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_INCLUSIVE);
    3139           93 :   OMP_CLAUSE_DECL (c) = integer_zero_node;
    3140              : 
    3141           93 :   gomp_scan *input_stmt = gimple_build_omp_scan (NULL, NULL_TREE);
    3142           93 :   gomp_scan *scan_stmt = gimple_build_omp_scan (NULL, c);
    3143           93 :   gsi_replace (gsi, input_stmt, false);
    3144           93 :   gimple_seq input_body = NULL;
    3145           93 :   gimple_seq_add_stmt (&input_body, stmt);
    3146           93 :   gsi_insert_after (gsi, scan_stmt, GSI_NEW_STMT);
    3147              : 
    3148           93 :   gimple_stmt_iterator input1_gsi = gsi_none ();
    3149           93 :   struct walk_stmt_info wi;
    3150           93 :   memset (&wi, 0, sizeof (wi));
    3151           93 :   wi.val_only = true;
    3152           93 :   wi.info = (void *) &input1_gsi;
    3153           93 :   walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), omp_find_scan, NULL, &wi);
    3154           93 :   gcc_assert (!gsi_end_p (input1_gsi));
    3155              : 
    3156           93 :   gimple *input_stmt1 = gsi_stmt (input1_gsi);
    3157           93 :   gsi_next (&input1_gsi);
    3158           93 :   gimple *scan_stmt1 = gsi_stmt (input1_gsi);
    3159           93 :   gcc_assert (scan_stmt1 && gimple_code (scan_stmt1) == GIMPLE_OMP_SCAN);
    3160           93 :   c = gimple_omp_scan_clauses (as_a <gomp_scan *> (scan_stmt1));
    3161          186 :   if (c && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_EXCLUSIVE)
    3162              :     std::swap (input_stmt1, scan_stmt1);
    3163              : 
    3164           93 :   gimple_seq input_body1 = gimple_omp_body (input_stmt1);
    3165           93 :   gimple_omp_set_body (input_stmt1, NULL);
    3166              : 
    3167           93 :   gimple_seq scan_body = copy_gimple_seq_and_replace_locals (stmt);
    3168           93 :   gomp_for *new_stmt = as_a <gomp_for *> (scan_body);
    3169              : 
    3170           93 :   gimple_omp_set_body (input_stmt1, input_body1);
    3171           93 :   gimple_omp_set_body (scan_stmt1, NULL);
    3172              : 
    3173           93 :   gimple_stmt_iterator input2_gsi = gsi_none ();
    3174           93 :   memset (&wi, 0, sizeof (wi));
    3175           93 :   wi.val_only = true;
    3176           93 :   wi.info = (void *) &input2_gsi;
    3177           93 :   walk_gimple_seq_mod (gimple_omp_body_ptr (new_stmt), omp_find_scan,
    3178              :                        NULL, &wi);
    3179           93 :   gcc_assert (!gsi_end_p (input2_gsi));
    3180              : 
    3181           93 :   gimple *input_stmt2 = gsi_stmt (input2_gsi);
    3182           93 :   gsi_next (&input2_gsi);
    3183           93 :   gimple *scan_stmt2 = gsi_stmt (input2_gsi);
    3184           93 :   gcc_assert (scan_stmt2 && gimple_code (scan_stmt2) == GIMPLE_OMP_SCAN);
    3185          186 :   if (c && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_EXCLUSIVE)
    3186              :     std::swap (input_stmt2, scan_stmt2);
    3187              : 
    3188           93 :   gimple_omp_set_body (input_stmt2, NULL);
    3189              : 
    3190           93 :   gimple_omp_set_body (input_stmt, input_body);
    3191           93 :   gimple_omp_set_body (scan_stmt, scan_body);
    3192              : 
    3193           93 :   omp_context *ctx = new_omp_context (input_stmt, outer_ctx);
    3194           93 :   scan_omp (gimple_omp_body_ptr (input_stmt), ctx);
    3195              : 
    3196           93 :   ctx = new_omp_context (scan_stmt, outer_ctx);
    3197           93 :   scan_omp (gimple_omp_body_ptr (scan_stmt), ctx);
    3198              : 
    3199           93 :   maybe_lookup_ctx (new_stmt)->for_simd_scan_phase = true;
    3200           93 : }
    3201              : 
    3202              : /* Scan an OpenMP sections directive.  */
    3203              : 
    3204              : static void
    3205          589 : scan_omp_sections (gomp_sections *stmt, omp_context *outer_ctx)
    3206              : {
    3207          589 :   omp_context *ctx;
    3208              : 
    3209          589 :   ctx = new_omp_context (stmt, outer_ctx);
    3210          589 :   scan_sharing_clauses (gimple_omp_sections_clauses (stmt), ctx);
    3211          589 :   scan_omp (gimple_omp_body_ptr (stmt), ctx);
    3212          589 : }
    3213              : 
    3214              : /* Scan an OpenMP single directive.  */
    3215              : 
    3216              : static void
    3217         1258 : scan_omp_single (gomp_single *stmt, omp_context *outer_ctx)
    3218              : {
    3219         1258 :   omp_context *ctx;
    3220         1258 :   tree name;
    3221              : 
    3222         1258 :   ctx = new_omp_context (stmt, outer_ctx);
    3223         1258 :   ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
    3224         1258 :   ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
    3225         1258 :   name = create_tmp_var_name (".omp_copy_s");
    3226         1258 :   name = build_decl (gimple_location (stmt),
    3227              :                      TYPE_DECL, name, ctx->record_type);
    3228         1258 :   TYPE_NAME (ctx->record_type) = name;
    3229              : 
    3230         1258 :   scan_sharing_clauses (gimple_omp_single_clauses (stmt), ctx);
    3231         1258 :   scan_omp (gimple_omp_body_ptr (stmt), ctx);
    3232              : 
    3233         1258 :   if (TYPE_FIELDS (ctx->record_type) == NULL)
    3234         1108 :     ctx->record_type = NULL;
    3235              :   else
    3236          150 :     layout_type (ctx->record_type);
    3237         1258 : }
    3238              : 
    3239              : /* Scan a GIMPLE_OMP_TARGET.  */
    3240              : 
    3241              : static void
    3242        43419 : scan_omp_target (gomp_target *stmt, omp_context *outer_ctx)
    3243              : {
    3244        43419 :   omp_context *ctx;
    3245        43419 :   tree name;
    3246        43419 :   bool offloaded = is_gimple_omp_offloaded (stmt);
    3247        43419 :   tree clauses = gimple_omp_target_clauses (stmt);
    3248              : 
    3249        43419 :   ctx = new_omp_context (stmt, outer_ctx);
    3250        43419 :   ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
    3251        43419 :   ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
    3252        43419 :   name = create_tmp_var_name (".omp_data_t");
    3253        43419 :   name = build_decl (gimple_location (stmt),
    3254              :                      TYPE_DECL, name, ctx->record_type);
    3255        43419 :   DECL_ARTIFICIAL (name) = 1;
    3256        43419 :   DECL_NAMELESS (name) = 1;
    3257        43419 :   TYPE_NAME (ctx->record_type) = name;
    3258        43419 :   TYPE_ARTIFICIAL (ctx->record_type) = 1;
    3259              : 
    3260        43419 :   if (offloaded)
    3261              :     {
    3262        25928 :       tree c = omp_find_clause (clauses, OMP_CLAUSE_DEVICE_TYPE);
    3263        25928 :       bool host_only
    3264        25928 :         = c && OMP_CLAUSE_DEVICE_TYPE_KIND (c) == OMP_CLAUSE_DEVICE_TYPE_HOST;
    3265        25928 :       create_omp_child_function (ctx, false, host_only);
    3266        25928 :       gimple_omp_target_set_child_fn (stmt, ctx->cb.dst_fn);
    3267              :     }
    3268              : 
    3269        43419 :   scan_sharing_clauses (clauses, ctx);
    3270        43419 :   scan_omp (gimple_omp_body_ptr (stmt), ctx);
    3271              : 
    3272        43419 :   if (TYPE_FIELDS (ctx->record_type) == NULL)
    3273         7976 :     ctx->record_type = ctx->receiver_decl = NULL;
    3274              :   else
    3275              :     {
    3276        35443 :       TYPE_FIELDS (ctx->record_type)
    3277        35443 :         = nreverse (TYPE_FIELDS (ctx->record_type));
    3278        35443 :       if (flag_checking)
    3279              :         {
    3280        35443 :           unsigned int align = DECL_ALIGN (TYPE_FIELDS (ctx->record_type));
    3281        35443 :           for (tree field = TYPE_FIELDS (ctx->record_type);
    3282       136124 :                field;
    3283       100681 :                field = DECL_CHAIN (field))
    3284       100681 :             gcc_assert (DECL_ALIGN (field) == align);
    3285              :         }
    3286        35443 :       layout_type (ctx->record_type);
    3287        35443 :       if (offloaded)
    3288        18507 :         fixup_child_record_type (ctx);
    3289              :     }
    3290              : 
    3291        43419 :   if (ctx->teams_nested_p && ctx->nonteams_nested_p)
    3292              :     {
    3293            4 :       error_at (gimple_location (stmt),
    3294              :                 "%<target%> construct with nested %<teams%> construct "
    3295              :                 "contains directives outside of the %<teams%> construct");
    3296            4 :       gimple_omp_set_body (stmt, gimple_build_bind (NULL, NULL, NULL));
    3297              :     }
    3298        43419 : }
    3299              : 
    3300              : /* Scan an OpenMP teams directive.  */
    3301              : 
    3302              : static void
    3303         9499 : scan_omp_teams (gomp_teams *stmt, omp_context *outer_ctx)
    3304              : {
    3305         9499 :   omp_context *ctx = new_omp_context (stmt, outer_ctx);
    3306              : 
    3307         9499 :   if (!gimple_omp_teams_host (stmt))
    3308              :     {
    3309         6777 :       scan_sharing_clauses (gimple_omp_teams_clauses (stmt), ctx);
    3310         6777 :       scan_omp (gimple_omp_body_ptr (stmt), ctx);
    3311         6777 :       return;
    3312              :     }
    3313         2722 :   taskreg_contexts.safe_push (ctx);
    3314         2722 :   gcc_assert (taskreg_nesting_level == 1);
    3315         2722 :   ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
    3316         2722 :   ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
    3317         2722 :   tree name = create_tmp_var_name (".omp_data_s");
    3318         2722 :   name = build_decl (gimple_location (stmt),
    3319              :                      TYPE_DECL, name, ctx->record_type);
    3320         2722 :   DECL_ARTIFICIAL (name) = 1;
    3321         2722 :   DECL_NAMELESS (name) = 1;
    3322         2722 :   TYPE_NAME (ctx->record_type) = name;
    3323         2722 :   TYPE_ARTIFICIAL (ctx->record_type) = 1;
    3324         2722 :   create_omp_child_function (ctx, false, false);
    3325         2722 :   gimple_omp_teams_set_child_fn (stmt, ctx->cb.dst_fn);
    3326              : 
    3327         2722 :   scan_sharing_clauses (gimple_omp_teams_clauses (stmt), ctx);
    3328         2722 :   scan_omp (gimple_omp_body_ptr (stmt), ctx);
    3329              : 
    3330         2722 :   if (TYPE_FIELDS (ctx->record_type) == NULL)
    3331         1455 :     ctx->record_type = ctx->receiver_decl = NULL;
    3332              : }
    3333              : 
    3334              : /* Check nesting restrictions.  */
    3335              : static bool
    3336       163484 : check_omp_nesting_restrictions (gimple *stmt, omp_context *ctx)
    3337              : {
    3338       163484 :   tree c;
    3339              : 
    3340              :   /* No nesting of non-OpenACC STMT (that is, an OpenMP one, or a GOMP builtin)
    3341              :      inside an OpenACC CTX.  */
    3342       163484 :   if (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD
    3343       163484 :       || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE)
    3344              :     /* ..., except for the atomic codes that OpenACC shares with OpenMP.  */
    3345              :     ;
    3346       294761 :   else if (!(is_gimple_omp (stmt)
    3347       145251 :              && is_gimple_omp_oacc (stmt)))
    3348              :     {
    3349       119246 :       if (oacc_get_fn_attrib (cfun->decl) != NULL)
    3350              :         {
    3351           48 :           error_at (gimple_location (stmt),
    3352              :                     "non-OpenACC construct inside of OpenACC routine");
    3353           48 :           return false;
    3354              :         }
    3355              :       else
    3356       253514 :         for (omp_context *octx = ctx; octx != NULL; octx = octx->outer)
    3357       268808 :           if (is_gimple_omp (octx->stmt)
    3358       134492 :               && is_gimple_omp_oacc (octx->stmt))
    3359              :             {
    3360          176 :               error_at (gimple_location (stmt),
    3361              :                         "non-OpenACC construct inside of OpenACC region");
    3362          176 :               return false;
    3363              :             }
    3364              :     }
    3365              : 
    3366       163260 :   if (ctx != NULL)
    3367              :     {
    3368        88312 :       if (gimple_code (ctx->stmt) == GIMPLE_OMP_TARGET
    3369        88312 :           && gimple_omp_target_kind (ctx->stmt) == GF_OMP_TARGET_KIND_REGION)
    3370              :         {
    3371         9320 :           c = omp_find_clause (gimple_omp_target_clauses (ctx->stmt),
    3372              :                                OMP_CLAUSE_DEVICE);
    3373         9320 :           if (c && OMP_CLAUSE_DEVICE_ANCESTOR (c))
    3374              :             {
    3375            9 :               error_at (gimple_location (stmt),
    3376              :                         "OpenMP constructs are not allowed in target region "
    3377              :                         "with %<ancestor%>");
    3378            9 :               return false;
    3379              :             }
    3380              : 
    3381         9311 :           if (gimple_code (stmt) == GIMPLE_OMP_TEAMS && !ctx->teams_nested_p)
    3382         6773 :             ctx->teams_nested_p = true;
    3383              :           else
    3384         2538 :             ctx->nonteams_nested_p = true;
    3385              :         }
    3386        88303 :       if (gimple_code (ctx->stmt) == GIMPLE_OMP_SCAN
    3387          192 :           && ctx->outer
    3388        88495 :           && gimple_code (ctx->outer->stmt) == GIMPLE_OMP_FOR)
    3389              :         ctx = ctx->outer;
    3390        88303 :       if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
    3391        29786 :           && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD
    3392        90154 :           && !ctx->loop_p)
    3393              :         {
    3394         1631 :           c = NULL_TREE;
    3395         1631 :           if (ctx->order_concurrent
    3396         1631 :               && (gimple_code (stmt) == GIMPLE_OMP_ORDERED
    3397          280 :                   || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD
    3398          190 :                   || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE))
    3399              :             {
    3400          210 :               error_at (gimple_location (stmt),
    3401              :                         "OpenMP constructs other than %<parallel%>, %<loop%>"
    3402              :                         " or %<simd%> may not be nested inside a region with"
    3403              :                         " the %<order(concurrent)%> clause");
    3404          210 :               return false;
    3405              :             }
    3406         1421 :           if (gimple_code (stmt) == GIMPLE_OMP_ORDERED)
    3407              :             {
    3408          133 :               c = gimple_omp_ordered_clauses (as_a <gomp_ordered *> (stmt));
    3409          133 :               if (omp_find_clause (c, OMP_CLAUSE_SIMD))
    3410              :                 {
    3411          133 :                   if (omp_find_clause (c, OMP_CLAUSE_THREADS)
    3412          133 :                       && (ctx->outer == NULL
    3413           29 :                           || !gimple_omp_for_combined_into_p (ctx->stmt)
    3414           25 :                           || gimple_code (ctx->outer->stmt) != GIMPLE_OMP_FOR
    3415           25 :                           || (gimple_omp_for_kind (ctx->outer->stmt)
    3416              :                               != GF_OMP_FOR_KIND_FOR)
    3417           25 :                           || !gimple_omp_for_combined_p (ctx->outer->stmt)))
    3418              :                     {
    3419            8 :                       error_at (gimple_location (stmt),
    3420              :                                 "%<ordered simd threads%> must be closely "
    3421              :                                 "nested inside of %<%s simd%> region",
    3422            8 :                                 lang_GNU_Fortran () ? "do" : "for");
    3423            8 :                       return false;
    3424              :                     }
    3425              :                   return true;
    3426              :                 }
    3427              :             }
    3428              :           else if (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD
    3429              :                    || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE
    3430              :                    || gimple_code (stmt) == GIMPLE_OMP_SCAN
    3431              :                    || gimple_code (stmt) == GIMPLE_OMP_STRUCTURED_BLOCK)
    3432              :             return true;
    3433              :           else if (gimple_code (stmt) == GIMPLE_OMP_FOR
    3434              :                    && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
    3435              :             return true;
    3436           60 :           error_at (gimple_location (stmt),
    3437              :                     "OpenMP constructs other than "
    3438              :                     "%<ordered simd%>, %<simd%>, %<loop%> or %<atomic%> may "
    3439              :                     "not be nested inside %<simd%> region");
    3440           60 :           return false;
    3441              :         }
    3442        86672 :       else if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS)
    3443              :         {
    3444         6559 :           if ((gimple_code (stmt) != GIMPLE_OMP_FOR
    3445         6235 :                || (gimple_omp_for_kind (stmt) != GF_OMP_FOR_KIND_DISTRIBUTE
    3446           25 :                    && omp_find_clause (gimple_omp_for_clauses (stmt),
    3447              :                                        OMP_CLAUSE_BIND) == NULL_TREE))
    3448         6575 :               && gimple_code (stmt) != GIMPLE_OMP_PARALLEL)
    3449              :             {
    3450          132 :               error_at (gimple_location (stmt),
    3451              :                         "only %<distribute%>, %<parallel%> or %<loop%> "
    3452              :                         "regions are allowed to be strictly nested inside "
    3453              :                         "%<teams%> region");
    3454          132 :               return false;
    3455              :             }
    3456              :         }
    3457        80113 :       else if (ctx->order_concurrent
    3458         1953 :                && gimple_code (stmt) != GIMPLE_OMP_PARALLEL
    3459         1470 :                && (gimple_code (stmt) != GIMPLE_OMP_FOR
    3460         1196 :                    || gimple_omp_for_kind (stmt) != GF_OMP_FOR_KIND_SIMD)
    3461          289 :                && gimple_code (stmt) != GIMPLE_OMP_SCAN
    3462        80402 :                && gimple_code (stmt) != GIMPLE_OMP_STRUCTURED_BLOCK)
    3463              :         {
    3464          285 :           if (ctx->loop_p)
    3465          135 :             error_at (gimple_location (stmt),
    3466              :                       "OpenMP constructs other than %<parallel%>, %<loop%> or "
    3467              :                       "%<simd%> may not be nested inside a %<loop%> region");
    3468              :           else
    3469          150 :             error_at (gimple_location (stmt),
    3470              :                       "OpenMP constructs other than %<parallel%>, %<loop%> or "
    3471              :                       "%<simd%> may not be nested inside a region with "
    3472              :                       "the %<order(concurrent)%> clause");
    3473              :           return false;
    3474              :         }
    3475              :     }
    3476       161203 :   switch (gimple_code (stmt))
    3477              :     {
    3478        55482 :     case GIMPLE_OMP_FOR:
    3479        55482 :       if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_SIMD)
    3480              :         return true;
    3481        43534 :       if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
    3482              :         {
    3483         8927 :           if (ctx != NULL && gimple_code (ctx->stmt) != GIMPLE_OMP_TEAMS)
    3484              :             {
    3485            0 :               error_at (gimple_location (stmt),
    3486              :                         "%<distribute%> region must be strictly nested "
    3487              :                         "inside %<teams%> construct");
    3488            0 :               return false;
    3489              :             }
    3490              :           return true;
    3491              :         }
    3492              :       /* We split taskloop into task and nested taskloop in it.  */
    3493        34607 :       if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_TASKLOOP)
    3494              :         return true;
    3495              :       /* For now, hope this will change and loop bind(parallel) will not
    3496              :          be allowed in lots of contexts.  */
    3497        31471 :       if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR
    3498        50431 :           && omp_find_clause (gimple_omp_for_clauses (stmt), OMP_CLAUSE_BIND))
    3499              :         return true;
    3500        30837 :       if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_OACC_LOOP)
    3501              :         {
    3502        12511 :           bool ok = false;
    3503              : 
    3504        12511 :           if (ctx)
    3505        11964 :             switch (gimple_code (ctx->stmt))
    3506              :               {
    3507         4049 :               case GIMPLE_OMP_FOR:
    3508         4049 :                 ok = (gimple_omp_for_kind (ctx->stmt)
    3509              :                       == GF_OMP_FOR_KIND_OACC_LOOP);
    3510         4049 :                 break;
    3511              : 
    3512         7887 :               case GIMPLE_OMP_TARGET:
    3513         7887 :                 switch (gimple_omp_target_kind (ctx->stmt))
    3514              :                   {
    3515              :                   case GF_OMP_TARGET_KIND_OACC_PARALLEL:
    3516              :                   case GF_OMP_TARGET_KIND_OACC_KERNELS:
    3517              :                   case GF_OMP_TARGET_KIND_OACC_SERIAL:
    3518              :                   case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
    3519              :                   case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
    3520              :                     ok = true;
    3521              :                     break;
    3522              : 
    3523              :                   default:
    3524              :                     break;
    3525              :                   }
    3526              : 
    3527              :               default:
    3528              :                 break;
    3529              :               }
    3530          547 :           else if (oacc_get_fn_attrib (current_function_decl))
    3531              :             ok = true;
    3532         4049 :           if (!ok)
    3533              :             {
    3534           65 :               error_at (gimple_location (stmt),
    3535              :                         "OpenACC loop directive must be associated with"
    3536              :                         " an OpenACC compute region");
    3537           65 :               return false;
    3538              :             }
    3539              :         }
    3540              :       /* FALLTHRU */
    3541        34971 :     case GIMPLE_CALL:
    3542        34971 :       if (is_gimple_call (stmt)
    3543        34971 :           && (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
    3544              :               == BUILT_IN_GOMP_CANCEL
    3545         3082 :               || DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
    3546              :                  == BUILT_IN_GOMP_CANCELLATION_POINT))
    3547              :         {
    3548         1925 :           const char *bad = NULL;
    3549         1925 :           const char *kind = NULL;
    3550         1925 :           const char *construct
    3551         1925 :             = (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
    3552              :                == BUILT_IN_GOMP_CANCEL)
    3553         1925 :               ? "cancel"
    3554          808 :               : "cancellation point";
    3555         1925 :           if (ctx == NULL)
    3556              :             {
    3557           41 :               error_at (gimple_location (stmt), "orphaned %qs construct",
    3558              :                         construct);
    3559           41 :               return false;
    3560              :             }
    3561         1884 :           switch (tree_fits_shwi_p (gimple_call_arg (stmt, 0))
    3562         1884 :                   ? tree_to_shwi (gimple_call_arg (stmt, 0))
    3563              :                   : 0)
    3564              :             {
    3565          546 :             case 1:
    3566          546 :               if (gimple_code (ctx->stmt) != GIMPLE_OMP_PARALLEL)
    3567              :                 bad = "parallel";
    3568          226 :               else if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
    3569              :                        == BUILT_IN_GOMP_CANCEL
    3570          226 :                        && !integer_zerop (gimple_call_arg (stmt, 1)))
    3571          177 :                 ctx->cancellable = true;
    3572              :               kind = "parallel";
    3573              :               break;
    3574          427 :             case 2:
    3575          427 :               if (gimple_code (ctx->stmt) != GIMPLE_OMP_FOR
    3576          427 :                   || gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR)
    3577              :                 bad = "for";
    3578          117 :               else if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
    3579              :                        == BUILT_IN_GOMP_CANCEL
    3580          117 :                        && !integer_zerop (gimple_call_arg (stmt, 1)))
    3581              :                 {
    3582          106 :                   ctx->cancellable = true;
    3583          106 :                   if (omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
    3584              :                                        OMP_CLAUSE_NOWAIT))
    3585            5 :                     warning_at (gimple_location (stmt), OPT_Wopenmp,
    3586              :                                 "%<cancel for%> inside "
    3587              :                                 "%<nowait%> for construct");
    3588          106 :                   if (omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
    3589              :                                        OMP_CLAUSE_ORDERED))
    3590            5 :                     warning_at (gimple_location (stmt), OPT_Wopenmp,
    3591              :                                 "%<cancel for%> inside "
    3592              :                                 "%<ordered%> for construct");
    3593              :                 }
    3594              :               kind = "for";
    3595              :               break;
    3596          383 :             case 4:
    3597          383 :               if (gimple_code (ctx->stmt) != GIMPLE_OMP_SECTIONS
    3598          383 :                   && gimple_code (ctx->stmt) != GIMPLE_OMP_SECTION)
    3599              :                 bad = "sections";
    3600           93 :               else if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
    3601              :                        == BUILT_IN_GOMP_CANCEL
    3602           93 :                        && !integer_zerop (gimple_call_arg (stmt, 1)))
    3603              :                 {
    3604           73 :                   if (gimple_code (ctx->stmt) == GIMPLE_OMP_SECTIONS)
    3605              :                     {
    3606            0 :                       ctx->cancellable = true;
    3607            0 :                       if (omp_find_clause (gimple_omp_sections_clauses
    3608            0 :                                                                 (ctx->stmt),
    3609              :                                            OMP_CLAUSE_NOWAIT))
    3610            0 :                         warning_at (gimple_location (stmt), OPT_Wopenmp,
    3611              :                                     "%<cancel sections%> inside "
    3612              :                                     "%<nowait%> sections construct");
    3613              :                     }
    3614              :                   else
    3615              :                     {
    3616           73 :                       gcc_assert (ctx->outer
    3617              :                                   && gimple_code (ctx->outer->stmt)
    3618              :                                      == GIMPLE_OMP_SECTIONS);
    3619           73 :                       ctx->outer->cancellable = true;
    3620           73 :                       if (omp_find_clause (gimple_omp_sections_clauses
    3621           73 :                                                         (ctx->outer->stmt),
    3622              :                                            OMP_CLAUSE_NOWAIT))
    3623           10 :                         warning_at (gimple_location (stmt), OPT_Wopenmp,
    3624              :                                     "%<cancel sections%> inside "
    3625              :                                     "%<nowait%> sections construct");
    3626              :                     }
    3627              :                 }
    3628              :               kind = "sections";
    3629              :               break;
    3630          528 :             case 8:
    3631          528 :               if (!is_task_ctx (ctx)
    3632          528 :                   && (!is_taskloop_ctx (ctx)
    3633           54 :                       || ctx->outer == NULL
    3634           54 :                       || !is_task_ctx (ctx->outer)))
    3635              :                 bad = "task";
    3636              :               else
    3637              :                 {
    3638          248 :                   for (omp_context *octx = ctx->outer;
    3639          439 :                        octx; octx = octx->outer)
    3640              :                     {
    3641          425 :                       switch (gimple_code (octx->stmt))
    3642              :                         {
    3643              :                         case GIMPLE_OMP_TASKGROUP:
    3644              :                           break;
    3645           40 :                         case GIMPLE_OMP_TARGET:
    3646           40 :                           if (gimple_omp_target_kind (octx->stmt)
    3647              :                               != GF_OMP_TARGET_KIND_REGION)
    3648           20 :                             continue;
    3649              :                           /* FALLTHRU */
    3650           99 :                         case GIMPLE_OMP_PARALLEL:
    3651           99 :                         case GIMPLE_OMP_TEAMS:
    3652           99 :                           error_at (gimple_location (stmt),
    3653              :                                     "%<%s taskgroup%> construct not closely "
    3654              :                                     "nested inside of %<taskgroup%> region",
    3655              :                                     construct);
    3656           99 :                           return false;
    3657          104 :                         case GIMPLE_OMP_TASK:
    3658          104 :                           if (gimple_omp_task_taskloop_p (octx->stmt)
    3659           94 :                               && octx->outer
    3660          198 :                               && is_taskloop_ctx (octx->outer))
    3661              :                             {
    3662           94 :                               tree clauses
    3663           94 :                                 = gimple_omp_for_clauses (octx->outer->stmt);
    3664           94 :                               if (!omp_find_clause (clauses, OMP_CLAUSE_NOGROUP))
    3665              :                                 break;
    3666              :                             }
    3667           60 :                           continue;
    3668          111 :                         default:
    3669          111 :                           continue;
    3670          171 :                         }
    3671              :                       break;
    3672              :                     }
    3673          149 :                   ctx->cancellable = true;
    3674              :                 }
    3675              :               kind = "taskgroup";
    3676              :               break;
    3677            0 :             default:
    3678            0 :               error_at (gimple_location (stmt), "invalid arguments");
    3679            0 :               return false;
    3680              :             }
    3681          341 :           if (bad)
    3682              :             {
    3683         1200 :               error_at (gimple_location (stmt),
    3684              :                         "%<%s %s%> construct not closely nested inside of %qs",
    3685              :                         construct, kind, bad);
    3686         1200 :               return false;
    3687              :             }
    3688              :         }
    3689              :       /* FALLTHRU */
    3690              :     case GIMPLE_OMP_SECTIONS:
    3691              :     case GIMPLE_OMP_SINGLE:
    3692        56469 :       for (; ctx != NULL; ctx = ctx->outer)
    3693        38337 :         switch (gimple_code (ctx->stmt))
    3694              :           {
    3695         6873 :           case GIMPLE_OMP_FOR:
    3696         6873 :             if (gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR
    3697         6873 :                 && gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_TASKLOOP)
    3698              :               break;
    3699              :             /* FALLTHRU */
    3700          778 :           case GIMPLE_OMP_SECTIONS:
    3701          778 :           case GIMPLE_OMP_SINGLE:
    3702          778 :           case GIMPLE_OMP_ORDERED:
    3703          778 :           case GIMPLE_OMP_MASTER:
    3704          778 :           case GIMPLE_OMP_MASKED:
    3705          778 :           case GIMPLE_OMP_TASK:
    3706          778 :           case GIMPLE_OMP_CRITICAL:
    3707          778 :             if (is_gimple_call (stmt))
    3708              :               {
    3709          707 :                 if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
    3710              :                     != BUILT_IN_GOMP_BARRIER)
    3711              :                   return true;
    3712           23 :                 error_at (gimple_location (stmt),
    3713              :                           "barrier region may not be closely nested inside "
    3714              :                           "of work-sharing, %<loop%>, %<critical%>, "
    3715              :                           "%<ordered%>, %<master%>, %<masked%>, explicit "
    3716              :                           "%<task%> or %<taskloop%> region");
    3717           23 :                 return false;
    3718              :               }
    3719           71 :             error_at (gimple_location (stmt),
    3720              :                       "work-sharing region may not be closely nested inside "
    3721              :                       "of work-sharing, %<loop%>, %<critical%>, %<ordered%>, "
    3722              :                       "%<master%>, %<masked%>, explicit %<task%> or "
    3723              :                       "%<taskloop%> region");
    3724           71 :             return false;
    3725              :           case GIMPLE_OMP_PARALLEL:
    3726              :           case GIMPLE_OMP_TEAMS:
    3727              :             return true;
    3728        13437 :           case GIMPLE_OMP_TARGET:
    3729        13437 :             if (gimple_omp_target_kind (ctx->stmt)
    3730              :                 == GF_OMP_TARGET_KIND_REGION)
    3731              :               return true;
    3732              :             break;
    3733              :           default:
    3734              :             break;
    3735              :           }
    3736              :       break;
    3737              :     case GIMPLE_OMP_MASTER:
    3738              :     case GIMPLE_OMP_MASKED:
    3739         1526 :       for (; ctx != NULL; ctx = ctx->outer)
    3740         1017 :         switch (gimple_code (ctx->stmt))
    3741              :           {
    3742           14 :           case GIMPLE_OMP_FOR:
    3743           14 :             if (gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR
    3744           14 :                 && gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_TASKLOOP)
    3745              :               break;
    3746              :             /* FALLTHRU */
    3747           30 :           case GIMPLE_OMP_SECTIONS:
    3748           30 :           case GIMPLE_OMP_SINGLE:
    3749           30 :           case GIMPLE_OMP_TASK:
    3750           60 :             error_at (gimple_location (stmt),
    3751              :                       "%qs region may not be closely nested inside "
    3752              :                       "of work-sharing, %<loop%>, explicit %<task%> or "
    3753              :                       "%<taskloop%> region",
    3754           30 :                       gimple_code (stmt) == GIMPLE_OMP_MASTER
    3755              :                       ? "master" : "masked");
    3756           30 :             return false;
    3757              :           case GIMPLE_OMP_PARALLEL:
    3758              :           case GIMPLE_OMP_TEAMS:
    3759              :             return true;
    3760           10 :           case GIMPLE_OMP_TARGET:
    3761           10 :             if (gimple_omp_target_kind (ctx->stmt)
    3762              :                 == GF_OMP_TARGET_KIND_REGION)
    3763              :               return true;
    3764              :             break;
    3765              :           default:
    3766              :             break;
    3767              :           }
    3768              :       break;
    3769              :     case GIMPLE_OMP_SCOPE:
    3770          302 :       for (; ctx != NULL; ctx = ctx->outer)
    3771          183 :         switch (gimple_code (ctx->stmt))
    3772              :           {
    3773            7 :           case GIMPLE_OMP_FOR:
    3774            7 :             if (gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR
    3775            7 :                 && gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_TASKLOOP)
    3776              :               break;
    3777              :             /* FALLTHRU */
    3778           25 :           case GIMPLE_OMP_SECTIONS:
    3779           25 :           case GIMPLE_OMP_SINGLE:
    3780           25 :           case GIMPLE_OMP_TASK:
    3781           25 :           case GIMPLE_OMP_CRITICAL:
    3782           25 :           case GIMPLE_OMP_ORDERED:
    3783           25 :           case GIMPLE_OMP_MASTER:
    3784           25 :           case GIMPLE_OMP_MASKED:
    3785           25 :             error_at (gimple_location (stmt),
    3786              :                       "%<scope%> region may not be closely nested inside "
    3787              :                       "of work-sharing, %<loop%>, explicit %<task%>, "
    3788              :                       "%<taskloop%>, %<critical%>, %<ordered%>, %<master%>, "
    3789              :                       "or %<masked%> region");
    3790           25 :             return false;
    3791              :           case GIMPLE_OMP_PARALLEL:
    3792              :           case GIMPLE_OMP_TEAMS:
    3793              :             return true;
    3794            5 :           case GIMPLE_OMP_TARGET:
    3795            5 :             if (gimple_omp_target_kind (ctx->stmt)
    3796              :                 == GF_OMP_TARGET_KIND_REGION)
    3797              :               return true;
    3798              :             break;
    3799              :           default:
    3800              :             break;
    3801              :           }
    3802              :       break;
    3803         5657 :     case GIMPLE_OMP_TASK:
    3804        23282 :       for (c = gimple_omp_task_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
    3805        17629 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS)
    3806              :           {
    3807            4 :             enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_KIND (c);
    3808            4 :             error_at (OMP_CLAUSE_LOCATION (c),
    3809              :                       "%<%s(%s)%> is only allowed in %<omp ordered%>",
    3810            4 :                       OMP_CLAUSE_DOACROSS_DEPEND (c) ? "depend" : "doacross",
    3811              :                       kind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
    3812            4 :             return false;
    3813              :           }
    3814              :       break;
    3815         1635 :     case GIMPLE_OMP_ORDERED:
    3816         2881 :       for (c = gimple_omp_ordered_clauses (as_a <gomp_ordered *> (stmt));
    3817         2881 :            c; c = OMP_CLAUSE_CHAIN (c))
    3818              :         {
    3819         1262 :           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DOACROSS)
    3820              :             {
    3821          173 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
    3822              :                 {
    3823            4 :                   error_at (OMP_CLAUSE_LOCATION (c),
    3824              :                             "invalid depend kind in omp %<ordered%> %<depend%>");
    3825            4 :                   return false;
    3826              :                 }
    3827          169 :               gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREADS
    3828              :                           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SIMD);
    3829          169 :               continue;
    3830              :             }
    3831              : 
    3832         1089 :           tree oclause;
    3833              :           /* Look for containing ordered(N) loop.  */
    3834         1089 :           if (ctx == NULL
    3835         1077 :               || gimple_code (ctx->stmt) != GIMPLE_OMP_FOR
    3836         1089 :               || (oclause
    3837         1077 :                   = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
    3838              :                                      OMP_CLAUSE_ORDERED)) == NULL_TREE)
    3839              :             {
    3840           12 :               error_at (OMP_CLAUSE_LOCATION (c),
    3841              :                         "%<ordered%> construct with %<depend%> clause "
    3842              :                         "must be closely nested inside an %<ordered%> loop");
    3843           12 :               return false;
    3844              :             }
    3845              :         }
    3846         1619 :       c = gimple_omp_ordered_clauses (as_a <gomp_ordered *> (stmt));
    3847         1619 :       if (omp_find_clause (c, OMP_CLAUSE_SIMD))
    3848              :         {
    3849              :           /* ordered simd must be closely nested inside of simd region,
    3850              :              and simd region must not encounter constructs other than
    3851              :              ordered simd, therefore ordered simd may be either orphaned,
    3852              :              or ctx->stmt must be simd.  The latter case is handled already
    3853              :              earlier.  */
    3854           36 :           if (ctx != NULL)
    3855              :             {
    3856           10 :               error_at (gimple_location (stmt),
    3857              :                         "%<ordered%> %<simd%> must be closely nested inside "
    3858              :                         "%<simd%> region");
    3859           10 :               return false;
    3860              :             }
    3861              :         }
    3862         1609 :       for (; ctx != NULL; ctx = ctx->outer)
    3863         1474 :         switch (gimple_code (ctx->stmt))
    3864              :           {
    3865           38 :           case GIMPLE_OMP_CRITICAL:
    3866           38 :           case GIMPLE_OMP_TASK:
    3867           38 :           case GIMPLE_OMP_ORDERED:
    3868           38 :           ordered_in_taskloop:
    3869           38 :             error_at (gimple_location (stmt),
    3870              :                       "%<ordered%> region may not be closely nested inside "
    3871              :                       "of %<critical%>, %<ordered%>, explicit %<task%> or "
    3872              :                       "%<taskloop%> region");
    3873           38 :             return false;
    3874         1416 :           case GIMPLE_OMP_FOR:
    3875         1416 :             if (gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_TASKLOOP)
    3876           10 :               goto ordered_in_taskloop;
    3877         1406 :             tree o;
    3878         1406 :             o = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
    3879              :                                  OMP_CLAUSE_ORDERED);
    3880         1406 :             if (o == NULL)
    3881              :               {
    3882           22 :                 error_at (gimple_location (stmt),
    3883              :                           "%<ordered%> region must be closely nested inside "
    3884              :                           "a loop region with an %<ordered%> clause");
    3885           22 :                 return false;
    3886              :               }
    3887         1384 :             if (!gimple_omp_ordered_standalone_p (stmt))
    3888              :               {
    3889          375 :                 if (OMP_CLAUSE_ORDERED_DOACROSS (o))
    3890              :                   {
    3891           10 :                     error_at (gimple_location (stmt),
    3892              :                               "%<ordered%> construct without %<doacross%> or "
    3893              :                               "%<depend%> clauses must not have the same "
    3894              :                               "binding region as %<ordered%> construct with "
    3895              :                               "those clauses");
    3896           10 :                     return false;
    3897              :                   }
    3898          365 :                 else if (OMP_CLAUSE_ORDERED_EXPR (o))
    3899              :                   {
    3900           23 :                     tree co
    3901           23 :                       = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
    3902              :                                          OMP_CLAUSE_COLLAPSE);
    3903           23 :                     HOST_WIDE_INT
    3904           23 :                       o_n = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (o));
    3905           23 :                     HOST_WIDE_INT c_n = 1;
    3906           23 :                     if (co)
    3907            5 :                       c_n = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (co));
    3908           23 :                     if (o_n != c_n)
    3909              :                       {
    3910           10 :                         error_at (gimple_location (stmt),
    3911              :                                   "%<ordered%> construct without %<doacross%> "
    3912              :                                   "or %<depend%> clauses binds to loop where "
    3913              :                                   "%<collapse%> argument %wd is different from "
    3914              :                                   "%<ordered%> argument %wd", c_n, o_n);
    3915           10 :                         return false;
    3916              :                       }
    3917              :                   }
    3918              :               }
    3919              :             return true;
    3920           10 :           case GIMPLE_OMP_TARGET:
    3921           10 :             if (gimple_omp_target_kind (ctx->stmt)
    3922              :                 != GF_OMP_TARGET_KIND_REGION)
    3923              :               break;
    3924              :             /* FALLTHRU */
    3925           30 :           case GIMPLE_OMP_PARALLEL:
    3926           30 :           case GIMPLE_OMP_TEAMS:
    3927           30 :             error_at (gimple_location (stmt),
    3928              :                       "%<ordered%> region must be closely nested inside "
    3929              :                       "a loop region with an %<ordered%> clause");
    3930           30 :             return false;
    3931              :           default:
    3932              :             break;
    3933              :           }
    3934              :       break;
    3935          468 :     case GIMPLE_OMP_CRITICAL:
    3936          468 :       {
    3937          468 :         tree this_stmt_name
    3938          468 :           = gimple_omp_critical_name (as_a <gomp_critical *> (stmt));
    3939          905 :         for (; ctx != NULL; ctx = ctx->outer)
    3940          443 :           if (gomp_critical *other_crit
    3941          462 :                 = dyn_cast <gomp_critical *> (ctx->stmt))
    3942           25 :             if (this_stmt_name == gimple_omp_critical_name (other_crit))
    3943              :               {
    3944            6 :                 error_at (gimple_location (stmt),
    3945              :                           "%<critical%> region may not be nested inside "
    3946              :                            "a %<critical%> region with the same name");
    3947            6 :                 return false;
    3948              :               }
    3949              :       }
    3950              :       break;
    3951         9543 :     case GIMPLE_OMP_TEAMS:
    3952         9543 :       if (ctx == NULL)
    3953              :         break;
    3954         6821 :       else if (gimple_code (ctx->stmt) != GIMPLE_OMP_TARGET
    3955         6821 :                || (gimple_omp_target_kind (ctx->stmt)
    3956              :                    != GF_OMP_TARGET_KIND_REGION))
    3957              :         {
    3958              :           /* Teams construct can appear either strictly nested inside of
    3959              :              target construct with no intervening stmts, or can be encountered
    3960              :              only by initial task (so must not appear inside any OpenMP
    3961              :              construct.  */
    3962           44 :           error_at (gimple_location (stmt),
    3963              :                     "%<teams%> construct must be closely nested inside of "
    3964              :                     "%<target%> construct or not nested in any OpenMP "
    3965              :                     "construct");
    3966           44 :           return false;
    3967              :         }
    3968              :       break;
    3969        43802 :     case GIMPLE_OMP_TARGET:
    3970       197594 :       for (c = gimple_omp_target_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
    3971       153796 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS)
    3972              :           {
    3973            4 :             enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_KIND (c);
    3974            4 :             error_at (OMP_CLAUSE_LOCATION (c),
    3975              :                       "%<depend(%s)%> is only allowed in %<omp ordered%>",
    3976              :                       kind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
    3977            4 :             return false;
    3978              :           }
    3979        43798 :       if (is_gimple_omp_offloaded (stmt)
    3980        43798 :           && oacc_get_fn_attrib (cfun->decl) != NULL)
    3981              :         {
    3982            4 :           error_at (gimple_location (stmt),
    3983              :                     "OpenACC region inside of OpenACC routine, nested "
    3984              :                     "parallelism not supported yet");
    3985            4 :           return false;
    3986              :         }
    3987        51210 :       for (; ctx != NULL; ctx = ctx->outer)
    3988              :         {
    3989         7822 :           if (gimple_code (ctx->stmt) != GIMPLE_OMP_TARGET)
    3990              :             {
    3991          768 :               if (is_gimple_omp (stmt)
    3992          768 :                   && is_gimple_omp_oacc (stmt)
    3993          242 :                   && is_gimple_omp (ctx->stmt))
    3994              :                 {
    3995          242 :                   error_at (gimple_location (stmt),
    3996              :                             "OpenACC construct inside of non-OpenACC region");
    3997          242 :                   return false;
    3998              :                 }
    3999          526 :               continue;
    4000              :             }
    4001              : 
    4002         7054 :           const char *stmt_name, *ctx_stmt_name;
    4003         7054 :           switch (gimple_omp_target_kind (stmt))
    4004              :             {
    4005              :             case GF_OMP_TARGET_KIND_REGION: stmt_name = "target"; break;
    4006          399 :             case GF_OMP_TARGET_KIND_DATA: stmt_name = "target data"; break;
    4007         1688 :             case GF_OMP_TARGET_KIND_UPDATE: stmt_name = "target update"; break;
    4008           12 :             case GF_OMP_TARGET_KIND_ENTER_DATA:
    4009           12 :               stmt_name = "target enter data"; break;
    4010           18 :             case GF_OMP_TARGET_KIND_EXIT_DATA:
    4011           18 :               stmt_name = "target exit data"; break;
    4012         1442 :             case GF_OMP_TARGET_KIND_OACC_PARALLEL: stmt_name = "parallel"; break;
    4013         1621 :             case GF_OMP_TARGET_KIND_OACC_KERNELS: stmt_name = "kernels"; break;
    4014          183 :             case GF_OMP_TARGET_KIND_OACC_SERIAL: stmt_name = "serial"; break;
    4015          386 :             case GF_OMP_TARGET_KIND_OACC_DATA: stmt_name = "data"; break;
    4016          256 :             case GF_OMP_TARGET_KIND_OACC_UPDATE: stmt_name = "update"; break;
    4017           94 :             case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
    4018           94 :               stmt_name = "enter data"; break;
    4019           86 :             case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
    4020           86 :               stmt_name = "exit data"; break;
    4021           64 :             case GF_OMP_TARGET_KIND_OACC_DECLARE: stmt_name = "declare"; break;
    4022          128 :             case GF_OMP_TARGET_KIND_OACC_HOST_DATA: stmt_name = "host_data";
    4023          128 :               break;
    4024              :             case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
    4025              :             case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
    4026              :             case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
    4027              :               /* OpenACC 'kernels' decomposed parts.  */
    4028         1621 :               stmt_name = "kernels"; break;
    4029            0 :             default: gcc_unreachable ();
    4030              :             }
    4031         7054 :           switch (gimple_omp_target_kind (ctx->stmt))
    4032              :             {
    4033              :             case GF_OMP_TARGET_KIND_REGION: ctx_stmt_name = "target"; break;
    4034         2714 :             case GF_OMP_TARGET_KIND_DATA: ctx_stmt_name = "target data"; break;
    4035           35 :             case GF_OMP_TARGET_KIND_OACC_PARALLEL:
    4036           35 :               ctx_stmt_name = "parallel"; break;
    4037              :             case GF_OMP_TARGET_KIND_OACC_KERNELS:
    4038         1037 :               ctx_stmt_name = "kernels"; break;
    4039           35 :             case GF_OMP_TARGET_KIND_OACC_SERIAL:
    4040           35 :               ctx_stmt_name = "serial"; break;
    4041         3117 :             case GF_OMP_TARGET_KIND_OACC_DATA: ctx_stmt_name = "data"; break;
    4042            8 :             case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
    4043            8 :               ctx_stmt_name = "host_data"; break;
    4044              :             case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
    4045              :             case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
    4046              :             case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
    4047              :               /* OpenACC 'kernels' decomposed parts.  */
    4048         1037 :               ctx_stmt_name = "kernels"; break;
    4049            0 :             default: gcc_unreachable ();
    4050              :             }
    4051              : 
    4052              :           /* OpenACC/OpenMP mismatch?  */
    4053        14108 :           if (is_gimple_omp_oacc (stmt)
    4054         7054 :               != is_gimple_omp_oacc (ctx->stmt))
    4055              :             {
    4056           56 :               error_at (gimple_location (stmt),
    4057              :                         "%s %qs construct inside of %s %qs region",
    4058              :                         (is_gimple_omp_oacc (stmt)
    4059              :                          ? "OpenACC" : "OpenMP"), stmt_name,
    4060              :                         (is_gimple_omp_oacc (ctx->stmt)
    4061              :                          ? "OpenACC" : "OpenMP"), ctx_stmt_name);
    4062           28 :               return false;
    4063              :             }
    4064         7026 :           if (is_gimple_omp_offloaded (ctx->stmt))
    4065              :             {
    4066              :               /* No GIMPLE_OMP_TARGET inside offloaded OpenACC CTX.  */
    4067          185 :               if (is_gimple_omp_oacc (ctx->stmt))
    4068              :                 {
    4069          105 :                   error_at (gimple_location (stmt),
    4070              :                             "%qs construct inside of %qs region",
    4071              :                             stmt_name, ctx_stmt_name);
    4072          105 :                   return false;
    4073              :                 }
    4074              :               else
    4075              :                 {
    4076           80 :                   if ((gimple_omp_target_kind (ctx->stmt)
    4077              :                        == GF_OMP_TARGET_KIND_REGION)
    4078           80 :                       && (gimple_omp_target_kind (stmt)
    4079              :                           == GF_OMP_TARGET_KIND_REGION))
    4080              :                     {
    4081           58 :                       c = omp_find_clause (gimple_omp_target_clauses (stmt),
    4082              :                                            OMP_CLAUSE_DEVICE);
    4083           58 :                       if (c && OMP_CLAUSE_DEVICE_ANCESTOR (c))
    4084              :                         break;
    4085              :                     }
    4086           49 :                   warning_at (gimple_location (stmt), OPT_Wopenmp,
    4087              :                               "%qs construct inside of %qs region",
    4088              :                               stmt_name, ctx_stmt_name);
    4089              :                 }
    4090              :             }
    4091              :         }
    4092              :       break;
    4093              :     default:
    4094              :       break;
    4095              :     }
    4096              :   return true;
    4097              : }
    4098              : 
    4099              : 
    4100              : /* Helper function scan_omp.
    4101              : 
    4102              :    Callback for walk_tree or operators in walk_gimple_stmt used to
    4103              :    scan for OMP directives in TP.  */
    4104              : 
    4105              : static tree
    4106     10125041 : scan_omp_1_op (tree *tp, int *walk_subtrees, void *data)
    4107              : {
    4108     10125041 :   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
    4109     10125041 :   omp_context *ctx = (omp_context *) wi->info;
    4110     10125041 :   tree t = *tp;
    4111     10125041 :   tree tmp;
    4112              : 
    4113     10125041 :   switch (TREE_CODE (t))
    4114              :     {
    4115      4297963 :     case VAR_DECL:
    4116      4297963 :     case PARM_DECL:
    4117      4297963 :     case LABEL_DECL:
    4118      4297963 :     case RESULT_DECL:
    4119      4297963 :       if (ctx)
    4120              :         {
    4121      2194165 :           tmp = NULL_TREE;
    4122      2194165 :           if (TREE_CODE (t) == VAR_DECL
    4123      3937656 :               && (tmp = lookup_attribute ("omp allocate var",
    4124      1743491 :                                           DECL_ATTRIBUTES (t))) != NULL_TREE)
    4125          105 :             t = TREE_VALUE (TREE_VALUE (tmp));
    4126      2194165 :           tree repl = remap_decl (t, &ctx->cb);
    4127      2194165 :           gcc_checking_assert (TREE_CODE (repl) != ERROR_MARK);
    4128      2194165 :           if (tmp != NULL_TREE  && t != repl)
    4129           51 :             *tp = build_fold_addr_expr (repl);
    4130      2194114 :           else if (tmp == NULL_TREE)
    4131      2194060 :             *tp = repl;
    4132              :         }
    4133              :       break;
    4134              : 
    4135       199224 :     case INDIRECT_REF:
    4136       199224 :     case MEM_REF:
    4137       199224 :       if (ctx
    4138        79387 :           && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
    4139       259055 :           && ((tmp = lookup_attribute ("omp allocate var",
    4140        59831 :                                        DECL_ATTRIBUTES (TREE_OPERAND (t, 0))))
    4141              :                != NULL_TREE))
    4142              :         {
    4143          261 :           tmp = TREE_VALUE (TREE_VALUE (tmp));
    4144          261 :           tree repl = remap_decl (tmp, &ctx->cb);
    4145          261 :           gcc_checking_assert (TREE_CODE (repl) != ERROR_MARK);
    4146          261 :           if (tmp != repl)
    4147          245 :             *tp = repl;
    4148              :           break;
    4149              :         }
    4150      5826817 :       gcc_fallthrough ();
    4151              : 
    4152      5826817 :     default:
    4153      5826817 :       if (ctx && TYPE_P (t))
    4154            5 :         *tp = remap_type (t, &ctx->cb);
    4155      5826812 :       else if (!DECL_P (t))
    4156              :         {
    4157      4839109 :           *walk_subtrees = 1;
    4158      4839109 :           if (ctx)
    4159              :             {
    4160      1469822 :               tree tem = remap_type (TREE_TYPE (t), &ctx->cb);
    4161      1469822 :               if (tem != TREE_TYPE (t))
    4162              :                 {
    4163         9952 :                   if (TREE_CODE (t) == INTEGER_CST)
    4164         4205 :                     *tp = wide_int_to_tree (tem, wi::to_wide (t));
    4165              :                   else
    4166         5747 :                     TREE_TYPE (t) = tem;
    4167              :                 }
    4168              :             }
    4169              :         }
    4170              :       break;
    4171              :     }
    4172              : 
    4173     10125041 :   return NULL_TREE;
    4174              : }
    4175              : 
    4176              : /* Return true if FNDECL is a setjmp or a longjmp.  */
    4177              : 
    4178              : static bool
    4179         3202 : setjmp_or_longjmp_p (const_tree fndecl)
    4180              : {
    4181         3202 :   if (fndecl_built_in_p (fndecl, BUILT_IN_SETJMP, BUILT_IN_LONGJMP))
    4182              :     return true;
    4183              : 
    4184         3202 :   tree declname = DECL_NAME (fndecl);
    4185         3202 :   if (!declname
    4186         3202 :       || (DECL_CONTEXT (fndecl) != NULL_TREE
    4187         2412 :           && TREE_CODE (DECL_CONTEXT (fndecl)) != TRANSLATION_UNIT_DECL)
    4188         6130 :       || !TREE_PUBLIC (fndecl))
    4189              :     return false;
    4190              : 
    4191         2760 :   const char *name = IDENTIFIER_POINTER (declname);
    4192         2760 :   return !strcmp (name, "setjmp") || !strcmp (name, "longjmp");
    4193              : }
    4194              : 
    4195              : /* Helper function for simt_collapse_p.  */
    4196              : 
    4197              : static bool
    4198            0 : simt_collapse_reject (const gimple *stmt, const char *reason)
    4199              : {
    4200            0 :   if (dump_enabled_p ())
    4201            0 :     dump_printf_loc (MSG_MISSED_OPTIMIZATION, stmt,
    4202              :                      "collapsed loop cannot take the SIMT path: %s\n", reason);
    4203            0 :   return false;
    4204              : }
    4205              : 
    4206              : /* Callback for walk_gimple_seq.  Return integer_zero_node if the loop body
    4207              :    prevents the SIMT path, NULL_TREE otherwise.  */
    4208              : 
    4209              : static tree
    4210            0 : simt_collapse_body (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
    4211              :                     struct walk_stmt_info *)
    4212              : {
    4213            0 :   gimple *stmt = gsi_stmt (*gsi_p);
    4214            0 :   *handled_ops_p = false;
    4215              : 
    4216            0 :   switch (gimple_code (stmt))
    4217              :     {
    4218            0 :     case GIMPLE_ASSIGN:
    4219            0 :       switch (gimple_assign_rhs_code (stmt))
    4220              :         {
    4221            0 :         case CEIL_DIV_EXPR:
    4222            0 :         case CEIL_MOD_EXPR:
    4223            0 :         case EXACT_DIV_EXPR:
    4224            0 :         case FLOOR_DIV_EXPR:
    4225            0 :         case FLOOR_MOD_EXPR:
    4226            0 :         case ROUND_DIV_EXPR:
    4227            0 :         case ROUND_MOD_EXPR:
    4228            0 :         case TRUNC_DIV_EXPR:
    4229            0 :         case TRUNC_MOD_EXPR:
    4230            0 :           *handled_ops_p = true;
    4231            0 :           simt_collapse_reject (stmt, "integer division or modulo");
    4232            0 :           return integer_zero_node;
    4233              :         default:
    4234              :           break;
    4235              :         }
    4236              :       break;
    4237            0 :     case GIMPLE_BIND:
    4238            0 :       for (tree var = gimple_bind_vars (as_a <gbind *> (stmt)); var;
    4239            0 :            var = DECL_CHAIN (var))
    4240            0 :         if (TREE_ADDRESSABLE (var))
    4241              :           {
    4242            0 :             *handled_ops_p = true;
    4243            0 :             simt_collapse_reject (stmt, "address-taken local variable");
    4244            0 :             return integer_zero_node;
    4245              :           }
    4246              :       break;
    4247            0 :     case GIMPLE_CALL:
    4248            0 :       if (!gimple_call_internal_p (stmt))
    4249              :         {
    4250            0 :           *handled_ops_p = true;
    4251            0 :           simt_collapse_reject (stmt, "external function call");
    4252            0 :           return integer_zero_node;
    4253              :         }
    4254              :       break;
    4255              :     default:
    4256              :       break;
    4257              :     }
    4258              : 
    4259              :   return NULL_TREE;
    4260              : }
    4261              : 
    4262              : enum simt_collapse_bind_res
    4263              : {
    4264              :   SCBR_DONE,
    4265              :   SCBR_STOP,
    4266              :   SCBR_REJECT
    4267              : };
    4268              : 
    4269              : static enum simt_collapse_bind_res
    4270            0 : simt_collapse_bind (gimple_seq *body, bitmap iv_derived)
    4271              : {
    4272            0 :   for (gimple_stmt_iterator gsi = gsi_start (*body); !gsi_end_p (gsi);
    4273            0 :        gsi_next (&gsi))
    4274              :     {
    4275            0 :       gimple *s = gsi_stmt (gsi);
    4276            0 :       if (gimple_code (s) == GIMPLE_BIND)
    4277              :         {
    4278            0 :           enum simt_collapse_bind_res res
    4279            0 :             = simt_collapse_bind (gimple_bind_body_ptr (as_a <gbind *> (s)),
    4280              :                                   iv_derived);
    4281            0 :           if (res != SCBR_DONE)
    4282              :             return res;
    4283            0 :           continue;
    4284            0 :         }
    4285            0 :       if (is_gimple_debug (s))
    4286            0 :         continue;
    4287            0 :       if (!is_gimple_assign (s))
    4288              :         return SCBR_STOP;
    4289            0 :       tree lhs = gimple_assign_lhs (s);
    4290            0 :       tree rhs1 = gimple_assign_rhs1 (s);
    4291            0 :       if (TREE_CODE (lhs) == MEM_REF && DECL_P (rhs1)
    4292            0 :           && bitmap_bit_p (iv_derived, DECL_UID (rhs1))
    4293            0 :           && !(DECL_P (TREE_OPERAND (lhs, 0))
    4294            0 :                && bitmap_bit_p (iv_derived, DECL_UID (TREE_OPERAND (lhs, 0)))))
    4295              :         return SCBR_REJECT;
    4296            0 :       if (DECL_P (lhs))
    4297            0 :         for (unsigned i = 1; i < gimple_num_ops (s); i++)
    4298            0 :           if (DECL_P (gimple_op (s, i))
    4299            0 :               && bitmap_bit_p (iv_derived, DECL_UID (gimple_op (s, i))))
    4300              :             {
    4301            0 :               bitmap_set_bit (iv_derived, DECL_UID (lhs));
    4302            0 :               break;
    4303              :             }
    4304              :     }
    4305              :   return SCBR_DONE;
    4306              : }
    4307              : 
    4308              : /* Return TRUE if the collapsed loop in STMT can take the SIMT path.  */
    4309              : 
    4310              : static bool
    4311            0 : simt_collapse_p (gimple *stmt)
    4312              : {
    4313            0 :   size_t collapse = gimple_omp_for_collapse (stmt);
    4314            0 :   gcc_assert (collapse > 1);
    4315              : 
    4316              :   /* Non-rectangular loops cannot use the SIMT path.  */
    4317            0 :   for (size_t i = 1; i < collapse; i++)
    4318              :     {
    4319            0 :       tree n1 = gimple_omp_for_initial (stmt, i);
    4320            0 :       tree n2 = gimple_omp_for_final (stmt, i);
    4321            0 :       if (TREE_CODE (n1) == TREE_VEC || TREE_CODE (n2) == TREE_VEC)
    4322            0 :         return simt_collapse_reject (stmt, "non-rectangular loop");
    4323              :     }
    4324              : 
    4325              :   /* Pointer iterators cannot use the SIMT path.  */
    4326            0 :   for (size_t i = 0; i < collapse; i++)
    4327            0 :     if (POINTER_TYPE_P (TREE_TYPE (gimple_omp_for_index (stmt, i))))
    4328            0 :       return simt_collapse_reject (stmt, "pointer iterator");
    4329              : 
    4330              :   /* Non-composite loops cannot take the SIMT path if their innermost variable
    4331              :      is unsigned and counts down.  Loops (whether composite or not) whose
    4332              :      innermost variable is narrower than int cannot either.  */
    4333            0 :   tree innermost_iv = gimple_omp_for_index (stmt, collapse - 1);
    4334            0 :   if (!gimple_omp_for_combined_into_p (stmt))
    4335              :     {
    4336            0 :       enum tree_code cond = gimple_omp_for_cond (stmt, collapse - 1);
    4337            0 :       if (cond == NE_EXPR)
    4338              :         {
    4339            0 :           tree step = omp_get_for_step_from_incr (
    4340              :             gimple_location (stmt), gimple_omp_for_incr (stmt, collapse - 1));
    4341            0 :           gcc_checking_assert (integer_onep (step)
    4342              :                                || integer_minus_onep (step));
    4343            0 :           cond = integer_onep (step) ? LT_EXPR : GT_EXPR;
    4344              :         }
    4345            0 :       if (TYPE_UNSIGNED (TREE_TYPE (innermost_iv))
    4346            0 :           && (cond == GT_EXPR || cond == GE_EXPR))
    4347            0 :         return simt_collapse_reject (
    4348            0 :           stmt, "non-composite loop with unsigned innermost IV counting down");
    4349              :     }
    4350            0 :   if (TYPE_PRECISION (TREE_TYPE (innermost_iv))
    4351            0 :       < TYPE_PRECISION (integer_type_node))
    4352            0 :     return simt_collapse_reject (stmt,
    4353            0 :                                  "loop with innermost IV narrower than int");
    4354              : 
    4355              :   /* Reject a MEM_REF store whose address is loop-invariant and whose value
    4356              :      depends on an iteration variable.  */
    4357            0 :   auto_bitmap iv_derived;
    4358            0 :   for (size_t k = 0; k < collapse; k++)
    4359            0 :     bitmap_set_bit (iv_derived, DECL_UID (gimple_omp_for_index (stmt, k)));
    4360            0 :   enum simt_collapse_bind_res res
    4361            0 :     = simt_collapse_bind (gimple_omp_body_ptr (stmt), iv_derived);
    4362            0 :   if (res == SCBR_REJECT)
    4363            0 :     return simt_collapse_reject (stmt,
    4364            0 :                                  "iteration variable value stored to memory");
    4365              : 
    4366              :   /* Check for OMP clauses that prevent the SIMT path.  */
    4367            0 :   tree clauses = gimple_omp_for_clauses (stmt);
    4368            0 :   for (; clauses; clauses = OMP_CLAUSE_CHAIN (clauses))
    4369              :     {
    4370            0 :       switch (OMP_CLAUSE_CODE (clauses))
    4371              :         {
    4372            0 :         case OMP_CLAUSE_LASTPRIVATE:
    4373            0 :           if (omp_find_iv_index (as_a <const gomp_for *> (stmt),
    4374            0 :                                  OMP_CLAUSE_DECL (clauses))
    4375              :               == -1)
    4376            0 :             return simt_collapse_reject (
    4377            0 :               stmt, "lastprivate clause on a non-iteration variable");
    4378              :           break;
    4379            0 :         case OMP_CLAUSE_LINEAR:
    4380            0 :           return simt_collapse_reject (stmt, "linear clause");
    4381            0 :         case OMP_CLAUSE_REDUCTION:
    4382            0 :           if (OMP_CLAUSE_REDUCTION_INSCAN (clauses))
    4383            0 :             return simt_collapse_reject (stmt, "inscan reduction clause");
    4384            0 :           if (!gimple_omp_for_combined_into_p (stmt))
    4385            0 :             return simt_collapse_reject (
    4386            0 :               stmt, "reduction clause on non-composite loop");
    4387              :           break;
    4388              :         default:
    4389              :           break;
    4390              :         }
    4391              :     }
    4392              : 
    4393              :   /* Walk loop body.  */
    4394            0 :   struct walk_stmt_info wi;
    4395            0 :   memset (&wi, 0, sizeof (wi));
    4396            0 :   walk_gimple_seq (gimple_omp_body (stmt), simt_collapse_body, NULL, &wi);
    4397            0 :   return wi.callback_result == NULL_TREE;
    4398            0 : }
    4399              : 
    4400              : /* Helper function for scan_omp.
    4401              : 
    4402              :    Callback for walk_gimple_stmt used to scan for OMP directives in
    4403              :    the current statement in GSI.  */
    4404              : 
    4405              : static tree
    4406      3534625 : scan_omp_1_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
    4407              :                  struct walk_stmt_info *wi)
    4408              : {
    4409      3534625 :   gimple *stmt = gsi_stmt (*gsi);
    4410      3534625 :   omp_context *ctx = (omp_context *) wi->info;
    4411              : 
    4412      3534625 :   if (gimple_has_location (stmt))
    4413      2881835 :     input_location = gimple_location (stmt);
    4414              : 
    4415              :   /* Check the nesting restrictions.  */
    4416      3534625 :   bool remove = false;
    4417      3534625 :   if (is_gimple_omp (stmt))
    4418       159225 :     remove = !check_omp_nesting_restrictions (stmt, ctx);
    4419      3375400 :   else if (is_gimple_call (stmt))
    4420              :     {
    4421       244334 :       tree fndecl = gimple_call_fndecl (stmt);
    4422       244334 :       if (fndecl)
    4423              :         {
    4424       226918 :           if (ctx
    4425        69062 :               && gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
    4426        14893 :               && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD
    4427         3202 :               && setjmp_or_longjmp_p (fndecl)
    4428       226926 :               && !ctx->loop_p)
    4429              :             {
    4430            4 :               remove = true;
    4431            4 :               error_at (gimple_location (stmt),
    4432              :                         "setjmp/longjmp inside %<simd%> construct");
    4433              :             }
    4434       226914 :           else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
    4435        67860 :             switch (DECL_FUNCTION_CODE (fndecl))
    4436              :               {
    4437         4259 :               case BUILT_IN_GOMP_BARRIER:
    4438         4259 :               case BUILT_IN_GOMP_CANCEL:
    4439         4259 :               case BUILT_IN_GOMP_CANCELLATION_POINT:
    4440         4259 :               case BUILT_IN_GOMP_TASKYIELD:
    4441         4259 :               case BUILT_IN_GOMP_TASKWAIT:
    4442         4259 :               case BUILT_IN_GOMP_TASKGROUP_START:
    4443         4259 :               case BUILT_IN_GOMP_TASKGROUP_END:
    4444         4259 :                 remove = !check_omp_nesting_restrictions (stmt, ctx);
    4445         4259 :                 break;
    4446              :               default:
    4447              :                 break;
    4448              :               }
    4449       159054 :           else if (ctx)
    4450              :             {
    4451        46756 :               omp_context *octx = ctx;
    4452        46756 :               if (gimple_code (ctx->stmt) == GIMPLE_OMP_SCAN && ctx->outer)
    4453        46756 :                 octx = ctx->outer;
    4454        46756 :               if (octx->order_concurrent && omp_runtime_api_call (fndecl))
    4455              :                 {
    4456          240 :                   remove = true;
    4457          240 :                   error_at (gimple_location (stmt),
    4458              :                             "OpenMP runtime API call %qD in a region with "
    4459              :                             "%<order(concurrent)%> clause", fndecl);
    4460              :                 }
    4461        46756 :               if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
    4462         3343 :                   && omp_runtime_api_call (fndecl)
    4463          342 :                   && ((IDENTIFIER_LENGTH (DECL_NAME (fndecl))
    4464              :                        != strlen ("omp_get_num_teams"))
    4465          188 :                       || strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)),
    4466              :                                  "omp_get_num_teams") != 0)
    4467          169 :                   && ((IDENTIFIER_LENGTH (DECL_NAME (fndecl))
    4468              :                        != strlen ("omp_get_num_teams_dim"))
    4469            6 :                       || strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)),
    4470              :                                  "omp_get_num_teams_dim") != 0)
    4471          163 :                   && ((IDENTIFIER_LENGTH (DECL_NAME (fndecl))
    4472              :                        != strlen ("omp_get_team_num"))
    4473          124 :                       || strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)),
    4474              :                                  "omp_get_team_num") != 0)
    4475        46795 :                   && ((IDENTIFIER_LENGTH (DECL_NAME (fndecl))
    4476              :                        != strlen ("omp_get_team_num_dim"))
    4477           12 :                       || strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)),
    4478              :                                  "omp_get_team_num_dim") != 0))
    4479              :                 {
    4480           27 :                   remove = true;
    4481           27 :                   error_at (gimple_location (stmt),
    4482              :                             "OpenMP runtime API call %qD strictly nested in a "
    4483              :                             "%<teams%> region", fndecl);
    4484              :                 }
    4485        46756 :               if (gimple_code (ctx->stmt) == GIMPLE_OMP_TARGET
    4486        13457 :                   && (gimple_omp_target_kind (ctx->stmt)
    4487              :                       == GF_OMP_TARGET_KIND_REGION)
    4488        51826 :                   && omp_runtime_api_call (fndecl))
    4489              :                 {
    4490          396 :                   tree tgt_clauses = gimple_omp_target_clauses (ctx->stmt);
    4491          396 :                   tree c = omp_find_clause (tgt_clauses, OMP_CLAUSE_DEVICE);
    4492          396 :                   if (c && OMP_CLAUSE_DEVICE_ANCESTOR (c))
    4493            5 :                     error_at (gimple_location (stmt),
    4494              :                               "OpenMP runtime API call %qD in a region with "
    4495              :                               "%<device(ancestor)%> clause", fndecl);
    4496              :                 }
    4497              :             }
    4498              :         }
    4499              :     }
    4500       210244 :   if (remove)
    4501              :     {
    4502         3326 :       stmt = gimple_build_nop ();
    4503         3326 :       gsi_replace (gsi, stmt, false);
    4504              :     }
    4505              : 
    4506      3534625 :   *handled_ops_p = true;
    4507              : 
    4508      3534625 :   switch (gimple_code (stmt))
    4509              :     {
    4510        19105 :     case GIMPLE_OMP_PARALLEL:
    4511        19105 :       taskreg_nesting_level++;
    4512        19105 :       scan_omp_parallel (gsi, ctx);
    4513        19105 :       taskreg_nesting_level--;
    4514        19105 :       break;
    4515              : 
    4516         5653 :     case GIMPLE_OMP_TASK:
    4517         5653 :       taskreg_nesting_level++;
    4518         5653 :       scan_omp_task (gsi, ctx);
    4519         5653 :       taskreg_nesting_level--;
    4520         5653 :       break;
    4521              : 
    4522        55448 :     case GIMPLE_OMP_FOR:
    4523        55448 :       if ((gimple_omp_for_kind (as_a <gomp_for *> (stmt))
    4524              :            == GF_OMP_FOR_KIND_SIMD)
    4525        12002 :           && gimple_omp_for_combined_into_p (stmt)
    4526        63885 :           && gimple_code (ctx->stmt) != GIMPLE_OMP_SCAN)
    4527              :         {
    4528         8251 :           tree clauses = gimple_omp_for_clauses (as_a <gomp_for *> (stmt));
    4529         8251 :           tree c = omp_find_clause (clauses, OMP_CLAUSE_REDUCTION);
    4530         8251 :           if (c && OMP_CLAUSE_REDUCTION_INSCAN (c) && !seen_error ())
    4531              :             {
    4532           93 :               scan_omp_simd_scan (gsi, as_a <gomp_for *> (stmt), ctx);
    4533           93 :               break;
    4534              :             }
    4535              :         }
    4536        55355 :       if ((gimple_omp_for_kind (as_a <gomp_for *> (stmt))
    4537              :            == GF_OMP_FOR_KIND_SIMD)
    4538        11909 :           && omp_maybe_offloaded_ctx (ctx)
    4539         4606 :           && omp_max_simt_vf ()
    4540        55355 :           && (gimple_omp_for_collapse (stmt) == 1 || simt_collapse_p (stmt)))
    4541            0 :         scan_omp_simd (gsi, as_a <gomp_for *> (stmt), ctx);
    4542              :       else
    4543        55355 :         scan_omp_for (as_a <gomp_for *> (stmt), ctx);
    4544              :       break;
    4545              : 
    4546          212 :     case GIMPLE_OMP_SCOPE:
    4547          212 :       ctx = new_omp_context (stmt, ctx);
    4548          212 :       scan_sharing_clauses (gimple_omp_scope_clauses (stmt), ctx);
    4549          212 :       scan_omp (gimple_omp_body_ptr (stmt), ctx);
    4550          212 :       break;
    4551              : 
    4552          852 :     case GIMPLE_OMP_DISPATCH:
    4553          852 :       ctx = new_omp_context (stmt, ctx);
    4554          852 :       scan_omp (gimple_omp_body_ptr (stmt), ctx);
    4555          852 :       break;
    4556              : 
    4557          612 :     case GIMPLE_OMP_INTEROP:
    4558          612 :       ctx = new_omp_context (stmt, ctx);
    4559          612 :       break;
    4560              : 
    4561          589 :     case GIMPLE_OMP_SECTIONS:
    4562          589 :       scan_omp_sections (as_a <gomp_sections *> (stmt), ctx);
    4563          589 :       break;
    4564              : 
    4565         1258 :     case GIMPLE_OMP_SINGLE:
    4566         1258 :       scan_omp_single (as_a <gomp_single *> (stmt), ctx);
    4567         1258 :       break;
    4568              : 
    4569         1332 :     case GIMPLE_OMP_SCAN:
    4570         1332 :       if (tree clauses = gimple_omp_scan_clauses (as_a <gomp_scan *> (stmt)))
    4571              :         {
    4572          630 :           if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_INCLUSIVE)
    4573          328 :             ctx->scan_inclusive = true;
    4574          302 :           else if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_EXCLUSIVE)
    4575          302 :             ctx->scan_exclusive = true;
    4576              :         }
    4577              :       /* FALLTHRU */
    4578         6273 :     case GIMPLE_OMP_SECTION:
    4579         6273 :     case GIMPLE_OMP_STRUCTURED_BLOCK:
    4580         6273 :     case GIMPLE_OMP_MASTER:
    4581         6273 :     case GIMPLE_OMP_ORDERED:
    4582         6273 :     case GIMPLE_OMP_CRITICAL:
    4583         6273 :       ctx = new_omp_context (stmt, ctx);
    4584         6273 :       scan_omp (gimple_omp_body_ptr (stmt), ctx);
    4585         6273 :       break;
    4586              : 
    4587          460 :     case GIMPLE_OMP_MASKED:
    4588          460 :       ctx = new_omp_context (stmt, ctx);
    4589          460 :       scan_sharing_clauses (gimple_omp_masked_clauses (stmt), ctx);
    4590          460 :       scan_omp (gimple_omp_body_ptr (stmt), ctx);
    4591          460 :       break;
    4592              : 
    4593          607 :     case GIMPLE_OMP_TASKGROUP:
    4594          607 :       ctx = new_omp_context (stmt, ctx);
    4595          607 :       scan_sharing_clauses (gimple_omp_taskgroup_clauses (stmt), ctx);
    4596          607 :       scan_omp (gimple_omp_body_ptr (stmt), ctx);
    4597          607 :       break;
    4598              : 
    4599        43419 :     case GIMPLE_OMP_TARGET:
    4600        43419 :       if (is_gimple_omp_offloaded (stmt))
    4601              :         {
    4602        25928 :           taskreg_nesting_level++;
    4603        25928 :           scan_omp_target (as_a <gomp_target *> (stmt), ctx);
    4604        25928 :           taskreg_nesting_level--;
    4605              :         }
    4606              :       else
    4607        17491 :         scan_omp_target (as_a <gomp_target *> (stmt), ctx);
    4608              :       break;
    4609              : 
    4610         9499 :     case GIMPLE_OMP_TEAMS:
    4611         9499 :       if (gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
    4612              :         {
    4613         2722 :           taskreg_nesting_level++;
    4614         2722 :           scan_omp_teams (as_a <gomp_teams *> (stmt), ctx);
    4615         2722 :           taskreg_nesting_level--;
    4616              :         }
    4617              :       else
    4618         6777 :         scan_omp_teams (as_a <gomp_teams *> (stmt), ctx);
    4619              :       break;
    4620              : 
    4621       297532 :     case GIMPLE_BIND:
    4622       297532 :       {
    4623       297532 :         tree var;
    4624              : 
    4625       297532 :         *handled_ops_p = false;
    4626       297532 :         if (ctx)
    4627       160906 :           for (var = gimple_bind_vars (as_a <gbind *> (stmt));
    4628       605760 :                var ;
    4629       444854 :                var = DECL_CHAIN (var))
    4630       444854 :             insert_decl_map (&ctx->cb, var, var);
    4631              :       }
    4632              :       break;
    4633      3093106 :     default:
    4634      3093106 :       *handled_ops_p = false;
    4635      3093106 :       break;
    4636              :     }
    4637              : 
    4638      3534625 :   return NULL_TREE;
    4639              : }
    4640              : 
    4641              : 
    4642              : /* Scan all the statements starting at the current statement.  CTX
    4643              :    contains context information about the OMP directives and
    4644              :    clauses found during the scan.  */
    4645              : 
    4646              : static void
    4647       272786 : scan_omp (gimple_seq *body_p, omp_context *ctx)
    4648              : {
    4649       272786 :   location_t saved_location;
    4650       272786 :   struct walk_stmt_info wi;
    4651              : 
    4652       272786 :   memset (&wi, 0, sizeof (wi));
    4653       272786 :   wi.info = ctx;
    4654       272786 :   wi.want_locations = true;
    4655              : 
    4656       272786 :   saved_location = input_location;
    4657       272786 :   walk_gimple_seq_mod (body_p, scan_omp_1_stmt, scan_omp_1_op, &wi);
    4658       272786 :   input_location = saved_location;
    4659       272786 : }
    4660              : 
    4661              : /* Re-gimplification and code generation routines.  */
    4662              : 
    4663              : /* Remove omp_member_access_dummy_var variables from gimple_bind_vars
    4664              :    of BIND if in a method.  */
    4665              : 
    4666              : static void
    4667       271580 : maybe_remove_omp_member_access_dummy_vars (gbind *bind)
    4668              : {
    4669       271580 :   if (DECL_ARGUMENTS (current_function_decl)
    4670       100875 :       && DECL_ARTIFICIAL (DECL_ARGUMENTS (current_function_decl))
    4671       282343 :       && (TREE_CODE (TREE_TYPE (DECL_ARGUMENTS (current_function_decl)))
    4672              :           == POINTER_TYPE))
    4673              :     {
    4674         3520 :       tree vars = gimple_bind_vars (bind);
    4675        19484 :       for (tree *pvar = &vars; *pvar; )
    4676        15964 :         if (omp_member_access_dummy_var (*pvar))
    4677          719 :           *pvar = DECL_CHAIN (*pvar);
    4678              :         else
    4679        15245 :           pvar = &DECL_CHAIN (*pvar);
    4680         3520 :       gimple_bind_set_vars (bind, vars);
    4681              :     }
    4682       271580 : }
    4683              : 
    4684              : /* Remove omp_member_access_dummy_var variables from BLOCK_VARS of
    4685              :    block and its subblocks.  */
    4686              : 
    4687              : static void
    4688        14511 : remove_member_access_dummy_vars (tree block)
    4689              : {
    4690        25762 :   for (tree *pvar = &BLOCK_VARS (block); *pvar; )
    4691        11251 :     if (omp_member_access_dummy_var (*pvar))
    4692          108 :       *pvar = DECL_CHAIN (*pvar);
    4693              :     else
    4694        11143 :       pvar = &DECL_CHAIN (*pvar);
    4695              : 
    4696        19167 :   for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
    4697         4656 :     remove_member_access_dummy_vars (block);
    4698        14511 : }
    4699              : 
    4700              : /* If a context was created for STMT when it was scanned, return it.  */
    4701              : 
    4702              : static omp_context *
    4703       127223 : maybe_lookup_ctx (gimple *stmt)
    4704              : {
    4705       127223 :   splay_tree_node n;
    4706       127223 :   n = splay_tree_lookup (all_contexts, (splay_tree_key) stmt);
    4707       127223 :   return n ? (omp_context *) n->value : NULL;
    4708              : }
    4709              : 
    4710              : 
    4711              : /* Find the mapping for DECL in CTX or the immediately enclosing
    4712              :    context that has a mapping for DECL.
    4713              : 
    4714              :    If CTX is a nested parallel directive, we may have to use the decl
    4715              :    mappings created in CTX's parent context.  Suppose that we have the
    4716              :    following parallel nesting (variable UIDs showed for clarity):
    4717              : 
    4718              :         iD.1562 = 0;
    4719              :         #omp parallel shared(iD.1562)           -> outer parallel
    4720              :           iD.1562 = iD.1562 + 1;
    4721              : 
    4722              :           #omp parallel shared (iD.1562)        -> inner parallel
    4723              :              iD.1562 = iD.1562 - 1;
    4724              : 
    4725              :    Each parallel structure will create a distinct .omp_data_s structure
    4726              :    for copying iD.1562 in/out of the directive:
    4727              : 
    4728              :         outer parallel          .omp_data_s.1.i -> iD.1562
    4729              :         inner parallel          .omp_data_s.2.i -> iD.1562
    4730              : 
    4731              :    A shared variable mapping will produce a copy-out operation before
    4732              :    the parallel directive and a copy-in operation after it.  So, in
    4733              :    this case we would have:
    4734              : 
    4735              :         iD.1562 = 0;
    4736              :         .omp_data_o.1.i = iD.1562;
    4737              :         #omp parallel shared(iD.1562)           -> outer parallel
    4738              :           .omp_data_i.1 = &.omp_data_o.1
    4739              :           .omp_data_i.1->i = .omp_data_i.1->i + 1;
    4740              : 
    4741              :           .omp_data_o.2.i = iD.1562;            -> **
    4742              :           #omp parallel shared(iD.1562)         -> inner parallel
    4743              :             .omp_data_i.2 = &.omp_data_o.2
    4744              :             .omp_data_i.2->i = .omp_data_i.2->i - 1;
    4745              : 
    4746              : 
    4747              :     ** This is a problem.  The symbol iD.1562 cannot be referenced
    4748              :        inside the body of the outer parallel region.  But since we are
    4749              :        emitting this copy operation while expanding the inner parallel
    4750              :        directive, we need to access the CTX structure of the outer
    4751              :        parallel directive to get the correct mapping:
    4752              : 
    4753              :           .omp_data_o.2.i = .omp_data_i.1->i
    4754              : 
    4755              :     Since there may be other workshare or parallel directives enclosing
    4756              :     the parallel directive, it may be necessary to walk up the context
    4757              :     parent chain.  This is not a problem in general because nested
    4758              :     parallelism happens only rarely.  */
    4759              : 
    4760              : static tree
    4761       132963 : lookup_decl_in_outer_ctx (tree decl, omp_context *ctx)
    4762              : {
    4763       132963 :   tree t;
    4764       132963 :   omp_context *up;
    4765              : 
    4766       193388 :   for (up = ctx->outer, t = NULL; up && t == NULL; up = up->outer)
    4767        60425 :     t = maybe_lookup_decl (decl, up);
    4768              : 
    4769       132963 :   gcc_assert (!ctx->is_nested || t || is_global_var (decl));
    4770              : 
    4771       101649 :   return t ? t : decl;
    4772              : }
    4773              : 
    4774              : 
    4775              : /* Similar to lookup_decl_in_outer_ctx, but return DECL if not found
    4776              :    in outer contexts.  */
    4777              : 
    4778              : static tree
    4779       374509 : maybe_lookup_decl_in_outer_ctx (tree decl, omp_context *ctx)
    4780              : {
    4781       374509 :   tree t = NULL;
    4782       374509 :   omp_context *up;
    4783              : 
    4784       661603 :   for (up = ctx->outer, t = NULL; up && t == NULL; up = up->outer)
    4785       287094 :     t = maybe_lookup_decl (decl, up);
    4786              : 
    4787       374509 :   return t ? t : decl;
    4788              : }
    4789              : 
    4790              : 
    4791              : /* Construct the initialization value for reduction operation OP.  */
    4792              : 
    4793              : tree
    4794        13941 : omp_reduction_init_op (location_t loc, enum tree_code op, tree type)
    4795              : {
    4796        13941 :   switch (op)
    4797              :     {
    4798        11307 :     case PLUS_EXPR:
    4799        11307 :     case MINUS_EXPR:
    4800        11307 :     case BIT_IOR_EXPR:
    4801        11307 :     case BIT_XOR_EXPR:
    4802        11307 :     case TRUTH_OR_EXPR:
    4803        11307 :     case TRUTH_ORIF_EXPR:
    4804        11307 :     case TRUTH_XOR_EXPR:
    4805        11307 :     case NE_EXPR:
    4806        11307 :       return build_zero_cst (type);
    4807              : 
    4808         1562 :     case MULT_EXPR:
    4809         1562 :     case TRUTH_AND_EXPR:
    4810         1562 :     case TRUTH_ANDIF_EXPR:
    4811         1562 :     case EQ_EXPR:
    4812         1562 :       return fold_convert_loc (loc, type, integer_one_node);
    4813              : 
    4814          244 :     case BIT_AND_EXPR:
    4815          244 :       return fold_convert_loc (loc, type, integer_minus_one_node);
    4816              : 
    4817          460 :     case MAX_EXPR:
    4818          460 :       if (SCALAR_FLOAT_TYPE_P (type))
    4819              :         {
    4820          194 :           REAL_VALUE_TYPE min;
    4821          194 :           if (HONOR_INFINITIES (type))
    4822          194 :             real_arithmetic (&min, NEGATE_EXPR, &dconstinf, NULL);
    4823              :           else
    4824            0 :             real_maxval (&min, 1, TYPE_MODE (type));
    4825          194 :           return build_real (type, min);
    4826              :         }
    4827          266 :       else if (POINTER_TYPE_P (type))
    4828              :         {
    4829            2 :           wide_int min
    4830            2 :             = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
    4831            2 :           return wide_int_to_tree (type, min);
    4832            2 :         }
    4833              :       else
    4834              :         {
    4835          264 :           gcc_assert (INTEGRAL_TYPE_P (type));
    4836          264 :           return TYPE_MIN_VALUE (type);
    4837              :         }
    4838              : 
    4839          368 :     case MIN_EXPR:
    4840          368 :       if (SCALAR_FLOAT_TYPE_P (type))
    4841              :         {
    4842          132 :           REAL_VALUE_TYPE max;
    4843          132 :           if (HONOR_INFINITIES (type))
    4844          132 :             max = dconstinf;
    4845              :           else
    4846            0 :             real_maxval (&max, 0, TYPE_MODE (type));
    4847          132 :           return build_real (type, max);
    4848              :         }
    4849          236 :       else if (POINTER_TYPE_P (type))
    4850              :         {
    4851            2 :           wide_int max
    4852            2 :             = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
    4853            2 :           return wide_int_to_tree (type, max);
    4854            2 :         }
    4855              :       else
    4856              :         {
    4857          234 :           gcc_assert (INTEGRAL_TYPE_P (type));
    4858          234 :           return TYPE_MAX_VALUE (type);
    4859              :         }
    4860              : 
    4861            0 :     default:
    4862            0 :       gcc_unreachable ();
    4863              :     }
    4864              : }
    4865              : 
    4866              : /* Construct the initialization value for reduction CLAUSE.  */
    4867              : 
    4868              : tree
    4869        11321 : omp_reduction_init (tree clause, tree type)
    4870              : {
    4871        11321 :   return omp_reduction_init_op (OMP_CLAUSE_LOCATION (clause),
    4872        11321 :                                 OMP_CLAUSE_REDUCTION_CODE (clause), type);
    4873              : }
    4874              : 
    4875              : /* Return alignment to be assumed for var in CLAUSE, which should be
    4876              :    OMP_CLAUSE_ALIGNED.  */
    4877              : 
    4878              : static tree
    4879          137 : omp_clause_aligned_alignment (tree clause)
    4880              : {
    4881          137 :   if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
    4882          120 :     return OMP_CLAUSE_ALIGNED_ALIGNMENT (clause);
    4883              : 
    4884              :   /* Otherwise return implementation defined alignment.  */
    4885           17 :   unsigned int al = 1;
    4886           17 :   opt_scalar_mode mode_iter;
    4887           17 :   auto_vector_modes modes;
    4888           17 :   targetm.vectorize.autovectorize_vector_modes (&modes, true);
    4889           17 :   static enum mode_class classes[]
    4890              :     = { MODE_INT, MODE_VECTOR_INT, MODE_FLOAT, MODE_VECTOR_FLOAT };
    4891           68 :   for (int i = 0; i < 4; i += 2)
    4892              :     /* The for loop above dictates that we only walk through scalar classes.  */
    4893          255 :     FOR_EACH_MODE_IN_CLASS (mode_iter, classes[i])
    4894              :       {
    4895          221 :         scalar_mode mode = mode_iter.require ();
    4896          221 :         machine_mode vmode = targetm.vectorize.preferred_simd_mode (mode);
    4897          221 :         if (GET_MODE_CLASS (vmode) != classes[i + 1])
    4898          102 :           continue;
    4899              :         machine_mode alt_vmode;
    4900          476 :         for (unsigned int j = 0; j < modes.length (); ++j)
    4901          527 :           if (related_vector_mode (modes[j], mode).exists (&alt_vmode)
    4902          578 :               && known_ge (GET_MODE_SIZE (alt_vmode), GET_MODE_SIZE (vmode)))
    4903          119 :             vmode = alt_vmode;
    4904              : 
    4905          119 :         tree type = lang_hooks.types.type_for_mode (mode, 1);
    4906          119 :         if (type == NULL_TREE || TYPE_MODE (type) != mode)
    4907           17 :           continue;
    4908          102 :         type = build_vector_type_for_mode (type, vmode);
    4909          102 :         if (TYPE_MODE (type) != vmode)
    4910            0 :           continue;
    4911          102 :         if (TYPE_ALIGN_UNIT (type) > al)
    4912           17 :           al = TYPE_ALIGN_UNIT (type);
    4913              :       }
    4914           17 :   return build_int_cst (integer_type_node, al);
    4915           17 : }
    4916              : 
    4917              : 
    4918              : /* This structure is part of the interface between lower_rec_simd_input_clauses
    4919              :    and lower_rec_input_clauses.  */
    4920              : 
    4921              : class omplow_simd_context {
    4922              : public:
    4923        81463 :   omplow_simd_context () { memset (this, 0, sizeof (*this)); }
    4924              :   tree idx;
    4925              :   tree lane;
    4926              :   tree lastlane;
    4927              :   vec<tree, va_heap> simt_eargs;
    4928              :   gimple_seq simt_dlist;
    4929              :   poly_uint64 max_vf;
    4930              :   bool is_simt;
    4931              : };
    4932              : 
    4933              : /* Helper function of lower_rec_input_clauses, used for #pragma omp simd
    4934              :    privatization.  */
    4935              : 
    4936              : static bool
    4937        11434 : lower_rec_simd_input_clauses (tree new_var, omp_context *ctx,
    4938              :                               omplow_simd_context *sctx, tree &ivar,
    4939              :                               tree &lvar, tree *rvar = NULL,
    4940              :                               tree *rvar2 = NULL)
    4941              : {
    4942        11434 :   if (known_eq (sctx->max_vf, 0U))
    4943              :     {
    4944         5413 :       sctx->max_vf = (sctx->is_simt ? omp_max_simt_vf ()
    4945         5413 :                       : omp_max_vf (omp_maybe_offloaded_ctx (ctx)));
    4946         5413 :       if (maybe_gt (sctx->max_vf, 1U))
    4947              :         {
    4948         4047 :           tree c = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
    4949              :                                     OMP_CLAUSE_SAFELEN);
    4950         4047 :           if (c)
    4951              :             {
    4952          126 :               poly_uint64 safe_len;
    4953          126 :               if (!poly_int_tree_p (OMP_CLAUSE_SAFELEN_EXPR (c), &safe_len)
    4954          126 :                   || maybe_lt (safe_len, 1U))
    4955            0 :                 sctx->max_vf = 1;
    4956              :               else
    4957          126 :                 sctx->max_vf = lower_bound (sctx->max_vf, safe_len);
    4958              :             }
    4959              :         }
    4960         5413 :       if (sctx->is_simt && !known_eq (sctx->max_vf, 1U))
    4961              :         {
    4962            0 :           for (tree c = gimple_omp_for_clauses (ctx->stmt); c;
    4963            0 :                c = OMP_CLAUSE_CHAIN (c))
    4964              :             {
    4965            0 :               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
    4966            0 :                 continue;
    4967              : 
    4968            0 :               if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    4969              :                 {
    4970              :                   /* UDR reductions are not supported yet for SIMT, disable
    4971              :                      SIMT.  */
    4972            0 :                   sctx->max_vf = 1;
    4973            0 :                   break;
    4974              :                 }
    4975              : 
    4976            0 :               if (truth_value_p (OMP_CLAUSE_REDUCTION_CODE (c))
    4977            0 :                   && !INTEGRAL_TYPE_P (TREE_TYPE (new_var)))
    4978              :                 {
    4979              :                   /* Doing boolean operations on non-integral types is
    4980              :                      for conformance only, it's not worth supporting this
    4981              :                      for SIMT.  */
    4982            0 :                   sctx->max_vf = 1;
    4983            0 :                   break;
    4984              :               }
    4985              :             }
    4986              :         }
    4987         5413 :       if (maybe_gt (sctx->max_vf, 1U))
    4988              :         {
    4989         4034 :           sctx->idx = create_tmp_var (unsigned_type_node);
    4990         4034 :           sctx->lane = create_tmp_var (unsigned_type_node);
    4991              :         }
    4992              :     }
    4993        11434 :   if (known_eq (sctx->max_vf, 1U))
    4994              :     return false;
    4995              : 
    4996         8325 :   if (sctx->is_simt)
    4997              :     {
    4998            0 :       if (is_gimple_reg (new_var))
    4999              :         {
    5000            0 :           ivar = lvar = new_var;
    5001            0 :           return true;
    5002              :         }
    5003            0 :       tree type = TREE_TYPE (new_var), ptype = build_pointer_type (type);
    5004            0 :       ivar = lvar = create_tmp_var (type);
    5005            0 :       TREE_ADDRESSABLE (ivar) = 1;
    5006            0 :       DECL_ATTRIBUTES (ivar) = tree_cons (get_identifier ("omp simt private"),
    5007            0 :                                           NULL, DECL_ATTRIBUTES (ivar));
    5008            0 :       sctx->simt_eargs.safe_push (build1 (ADDR_EXPR, ptype, ivar));
    5009            0 :       tree clobber = build_clobber (type);
    5010            0 :       gimple *g = gimple_build_assign (ivar, clobber);
    5011            0 :       gimple_seq_add_stmt (&sctx->simt_dlist, g);
    5012              :     }
    5013              :   else
    5014              :     {
    5015         8325 :       tree atype = build_array_type_nelts (TREE_TYPE (new_var), sctx->max_vf);
    5016         8325 :       tree avar = create_tmp_var_raw (atype);
    5017         8325 :       if (TREE_ADDRESSABLE (new_var))
    5018         1831 :         TREE_ADDRESSABLE (avar) = 1;
    5019         8325 :       DECL_ATTRIBUTES (avar)
    5020         8325 :         = tree_cons (get_identifier ("omp simd array"), NULL,
    5021         8325 :                      DECL_ATTRIBUTES (avar));
    5022         8325 :       gimple_add_tmp_var (avar);
    5023         8325 :       tree iavar = avar;
    5024         8325 :       if (rvar && !ctx->for_simd_scan_phase)
    5025              :         {
    5026              :           /* For inscan reductions, create another array temporary,
    5027              :              which will hold the reduced value.  */
    5028          234 :           iavar = create_tmp_var_raw (atype);
    5029          234 :           if (TREE_ADDRESSABLE (new_var))
    5030           35 :             TREE_ADDRESSABLE (iavar) = 1;
    5031          234 :           DECL_ATTRIBUTES (iavar)
    5032          234 :             = tree_cons (get_identifier ("omp simd array"), NULL,
    5033              :                          tree_cons (get_identifier ("omp simd inscan"), NULL,
    5034          234 :                                     DECL_ATTRIBUTES (iavar)));
    5035          234 :           gimple_add_tmp_var (iavar);
    5036          234 :           ctx->cb.decl_map->put (avar, iavar);
    5037          234 :           if (sctx->lastlane == NULL_TREE)
    5038          186 :             sctx->lastlane = create_tmp_var (unsigned_type_node);
    5039          234 :           *rvar = build4 (ARRAY_REF, TREE_TYPE (new_var), iavar,
    5040              :                           sctx->lastlane, NULL_TREE, NULL_TREE);
    5041          234 :           TREE_THIS_NOTRAP (*rvar) = 1;
    5042              : 
    5043          234 :           if (ctx->scan_exclusive)
    5044              :             {
    5045              :               /* And for exclusive scan yet another one, which will
    5046              :                  hold the value during the scan phase.  */
    5047          114 :               tree savar = create_tmp_var_raw (atype);
    5048          114 :               if (TREE_ADDRESSABLE (new_var))
    5049           17 :                 TREE_ADDRESSABLE (savar) = 1;
    5050          114 :               DECL_ATTRIBUTES (savar)
    5051          114 :                 = tree_cons (get_identifier ("omp simd array"), NULL,
    5052              :                              tree_cons (get_identifier ("omp simd inscan "
    5053              :                                                         "exclusive"), NULL,
    5054          114 :                                         DECL_ATTRIBUTES (savar)));
    5055          114 :               gimple_add_tmp_var (savar);
    5056          114 :               ctx->cb.decl_map->put (iavar, savar);
    5057          114 :               *rvar2 = build4 (ARRAY_REF, TREE_TYPE (new_var), savar,
    5058              :                                sctx->idx, NULL_TREE, NULL_TREE);
    5059          114 :               TREE_THIS_NOTRAP (*rvar2) = 1;
    5060              :             }
    5061              :         }
    5062         8325 :       ivar = build4 (ARRAY_REF, TREE_TYPE (new_var), iavar, sctx->idx,
    5063              :                      NULL_TREE, NULL_TREE);
    5064         8325 :       lvar = build4 (ARRAY_REF, TREE_TYPE (new_var), avar, sctx->lane,
    5065              :                      NULL_TREE, NULL_TREE);
    5066         8325 :       TREE_THIS_NOTRAP (ivar) = 1;
    5067         8325 :       TREE_THIS_NOTRAP (lvar) = 1;
    5068              :     }
    5069         8325 :   if (DECL_P (new_var))
    5070              :     {
    5071         8130 :       SET_DECL_VALUE_EXPR (new_var, lvar);
    5072         8130 :       DECL_HAS_VALUE_EXPR_P (new_var) = 1;
    5073              :     }
    5074              :   return true;
    5075              : }
    5076              : 
    5077              : /* Helper function of lower_rec_input_clauses.  For a reference
    5078              :    in simd reduction, add an underlying variable it will reference.  */
    5079              : 
    5080              : static void
    5081           64 : handle_simd_reference (location_t loc, tree new_vard, gimple_seq *ilist)
    5082              : {
    5083           64 :   tree z = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_vard)));
    5084           64 :   if (TREE_CONSTANT (z))
    5085              :     {
    5086           64 :       z = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_vard)),
    5087              :                               get_name (new_vard));
    5088           64 :       gimple_add_tmp_var (z);
    5089           64 :       TREE_ADDRESSABLE (z) = 1;
    5090           64 :       z = build_fold_addr_expr_loc (loc, z);
    5091           64 :       gimplify_assign (new_vard, z, ilist);
    5092              :     }
    5093           64 : }
    5094              : 
    5095              : /* Helper function for lower_rec_input_clauses.  Emit into ilist sequence
    5096              :    code to emit (type) (tskred_temp[idx]).  */
    5097              : 
    5098              : static tree
    5099         1133 : task_reduction_read (gimple_seq *ilist, tree tskred_temp, tree type,
    5100              :                      unsigned idx)
    5101              : {
    5102         1133 :   unsigned HOST_WIDE_INT sz
    5103         1133 :     = tree_to_uhwi (TYPE_SIZE_UNIT (pointer_sized_int_node));
    5104         1133 :   tree r = build2 (MEM_REF, pointer_sized_int_node,
    5105         1133 :                    tskred_temp, build_int_cst (TREE_TYPE (tskred_temp),
    5106         1133 :                                                idx * sz));
    5107         1133 :   tree v = create_tmp_var (pointer_sized_int_node);
    5108         1133 :   gassign *g = gimple_build_assign (v, r);
    5109         1133 :   gimple_seq_add_stmt (ilist, g);
    5110         1133 :   if (!useless_type_conversion_p (type, pointer_sized_int_node))
    5111              :     {
    5112          885 :       v = create_tmp_var (type);
    5113          885 :       g = gimple_build_assign (v, NOP_EXPR, gimple_assign_lhs (g));
    5114          885 :       gimple_seq_add_stmt (ilist, g);
    5115              :     }
    5116         1133 :   return v;
    5117              : }
    5118              : 
    5119              : /* Lower early initialization of privatized variable NEW_VAR
    5120              :    if it needs an allocator (has allocate clause).  */
    5121              : 
    5122              : static bool
    5123       154571 : lower_private_allocate (tree var, tree new_var, tree &allocator,
    5124              :                         tree &allocate_ptr, gimple_seq *ilist,
    5125              :                         omp_context *ctx, bool is_ref, tree size)
    5126              : {
    5127       154571 :   if (allocator)
    5128              :     return false;
    5129       154468 :   gcc_assert (allocate_ptr == NULL_TREE);
    5130       154468 :   if (ctx->allocate_map
    5131         3240 :       && (DECL_P (new_var) || (TYPE_P (new_var) && size)))
    5132         3237 :     if (tree *allocatorp = ctx->allocate_map->get (var))
    5133         1257 :       allocator = *allocatorp;
    5134       154468 :   if (allocator == NULL_TREE)
    5135              :     return false;
    5136         1257 :   if (!is_ref && omp_privatize_by_reference (var))
    5137              :     {
    5138            0 :       allocator = NULL_TREE;
    5139            0 :       return false;
    5140              :     }
    5141              : 
    5142         1257 :   if (gimple_code (ctx->stmt) == GIMPLE_OMP_TARGET && DECL_P (allocator))
    5143              :     {
    5144            4 :       allocator = lookup_decl (allocator, ctx);
    5145            4 :       gcc_checking_assert (allocator != NULL_TREE);
    5146              :     }
    5147              : 
    5148         1257 :   unsigned HOST_WIDE_INT ialign = 0;
    5149         1257 :   if (TREE_CODE (allocator) == TREE_LIST)
    5150              :     {
    5151          248 :       ialign = tree_to_uhwi (TREE_VALUE (allocator));
    5152          248 :       allocator = TREE_PURPOSE (allocator);
    5153              :     }
    5154         1257 :   if (TREE_CODE (allocator) != INTEGER_CST)
    5155          443 :     allocator = build_outer_var_ref (allocator, ctx, OMP_CLAUSE_ALLOCATE);
    5156         1257 :   allocator = fold_convert (pointer_sized_int_node, allocator);
    5157         1257 :   if (TREE_CODE (allocator) != INTEGER_CST)
    5158              :     {
    5159          443 :       tree var = create_tmp_var (TREE_TYPE (allocator));
    5160          443 :       gimplify_assign (var, allocator, ilist);
    5161          443 :       allocator = var;
    5162              :     }
    5163              : 
    5164         1257 :   tree ptr_type, align, sz = size;
    5165         1257 :   if (TYPE_P (new_var))
    5166              :     {
    5167          300 :       ptr_type = build_pointer_type (new_var);
    5168          300 :       ialign = MAX (ialign, TYPE_ALIGN_UNIT (new_var));
    5169              :     }
    5170          957 :   else if (is_ref)
    5171              :     {
    5172          123 :       ptr_type = build_pointer_type (TREE_TYPE (TREE_TYPE (new_var)));
    5173          123 :       ialign = MAX (ialign, TYPE_ALIGN_UNIT (TREE_TYPE (ptr_type)));
    5174              :     }
    5175              :   else
    5176              :     {
    5177          834 :       ptr_type = build_pointer_type (TREE_TYPE (new_var));
    5178          834 :       ialign = MAX (ialign, DECL_ALIGN_UNIT (new_var));
    5179          834 :       if (sz == NULL_TREE)
    5180          729 :         sz = fold_convert (size_type_node, DECL_SIZE_UNIT (new_var));
    5181              :     }
    5182         1257 :   align = build_int_cst (size_type_node, ialign);
    5183         1257 :   if (TREE_CODE (sz) != INTEGER_CST)
    5184              :     {
    5185           54 :       tree szvar = create_tmp_var (size_type_node);
    5186           54 :       gimplify_assign (szvar, sz, ilist);
    5187           54 :       sz = szvar;
    5188              :     }
    5189         1257 :   allocate_ptr = create_tmp_var (ptr_type);
    5190         1257 :   tree a = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
    5191         1257 :   gimple *g = gimple_build_call (a, 3, align, sz, allocator);
    5192         1257 :   gimple_call_set_lhs (g, allocate_ptr);
    5193         1257 :   gimple_seq_add_stmt (ilist, g);
    5194         1257 :   if (!is_ref)
    5195              :     {
    5196          834 :       tree x = build_simple_mem_ref (allocate_ptr);
    5197          834 :       TREE_THIS_NOTRAP (x) = 1;
    5198          834 :       SET_DECL_VALUE_EXPR (new_var, x);
    5199          834 :       DECL_HAS_VALUE_EXPR_P (new_var) = 1;
    5200              :     }
    5201              :   return true;
    5202              : }
    5203              : 
    5204              : /* Generate code to implement the input clauses, FIRSTPRIVATE and COPYIN,
    5205              :    from the receiver (aka child) side and initializers for REFERENCE_TYPE
    5206              :    private variables.  Initialization statements go in ILIST, while calls
    5207              :    to destructors go in DLIST.  */
    5208              : 
    5209              : static void
    5210        81463 : lower_rec_input_clauses (tree clauses, gimple_seq *ilist, gimple_seq *dlist,
    5211              :                          omp_context *ctx, struct omp_for_data *fd)
    5212              : {
    5213        81463 :   tree c, copyin_seq, x, ptr;
    5214        81463 :   bool copyin_by_ref = false;
    5215        81463 :   bool lastprivate_firstprivate = false;
    5216        81463 :   bool reduction_omp_orig_ref = false;
    5217        81463 :   int pass;
    5218        81463 :   bool is_simd = (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
    5219        81463 :                   && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD);
    5220        81463 :   omplow_simd_context sctx = omplow_simd_context ();
    5221        81463 :   tree simt_lane = NULL_TREE, simtrec = NULL_TREE;
    5222        81463 :   tree ivar = NULL_TREE, lvar = NULL_TREE, uid = NULL_TREE;
    5223        81463 :   gimple_seq llist[4] = { };
    5224        81463 :   tree nonconst_simd_if = NULL_TREE;
    5225              : 
    5226        81463 :   copyin_seq = NULL;
    5227        81463 :   sctx.is_simt = is_simd && omp_find_clause (clauses, OMP_CLAUSE__SIMT_);
    5228              : 
    5229              :   /* Set max_vf=1 (which will later enforce safelen=1) in simd loops
    5230              :      with data sharing clauses referencing variable sized vars.  That
    5231              :      is unnecessarily hard to support and very unlikely to result in
    5232              :      vectorized code anyway.  */
    5233        10428 :   if (is_simd)
    5234        70072 :     for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
    5235        59644 :       switch (OMP_CLAUSE_CODE (c))
    5236              :         {
    5237         7673 :         case OMP_CLAUSE_LINEAR:
    5238         7673 :           if (OMP_CLAUSE_LINEAR_ARRAY (c))
    5239          216 :             sctx.max_vf = 1;
    5240              :           /* FALLTHRU */
    5241        29598 :         case OMP_CLAUSE_PRIVATE:
    5242        29598 :         case OMP_CLAUSE_FIRSTPRIVATE:
    5243        29598 :         case OMP_CLAUSE_LASTPRIVATE:
    5244        29598 :           if (is_variable_sized (OMP_CLAUSE_DECL (c)))
    5245            0 :             sctx.max_vf = 1;
    5246        29598 :           else if (omp_privatize_by_reference (OMP_CLAUSE_DECL (c)))
    5247              :             {
    5248          325 :               tree rtype = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
    5249          325 :               if (!TREE_CONSTANT (TYPE_SIZE_UNIT (rtype)))
    5250           96 :                 sctx.max_vf = 1;
    5251              :             }
    5252              :           break;
    5253         2825 :         case OMP_CLAUSE_REDUCTION:
    5254         2825 :         case OMP_CLAUSE_IN_REDUCTION:
    5255         2825 :           if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
    5256         2825 :               || is_variable_sized (OMP_CLAUSE_DECL (c)))
    5257          184 :             sctx.max_vf = 1;
    5258         2641 :           else if (omp_privatize_by_reference (OMP_CLAUSE_DECL (c)))
    5259              :             {
    5260          144 :               tree rtype = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
    5261          144 :               if (!TREE_CONSTANT (TYPE_SIZE_UNIT (rtype)))
    5262            0 :                 sctx.max_vf = 1;
    5263              :             }
    5264              :           break;
    5265          759 :         case OMP_CLAUSE_IF:
    5266          759 :           if (integer_zerop (OMP_CLAUSE_IF_EXPR (c)))
    5267          124 :             sctx.max_vf = 1;
    5268          635 :           else if (TREE_CODE (OMP_CLAUSE_IF_EXPR (c)) != INTEGER_CST)
    5269          625 :             nonconst_simd_if = OMP_CLAUSE_IF_EXPR (c);
    5270              :           break;
    5271          635 :         case OMP_CLAUSE_SIMDLEN:
    5272          635 :           if (integer_onep (OMP_CLAUSE_SIMDLEN_EXPR (c)))
    5273          110 :             sctx.max_vf = 1;
    5274              :           break;
    5275          232 :         case OMP_CLAUSE__CONDTEMP_:
    5276              :           /* FIXME: lastprivate(conditional:) not handled for SIMT yet.  */
    5277          232 :           if (sctx.is_simt)
    5278            0 :             sctx.max_vf = 1;
    5279              :           break;
    5280        25595 :         default:
    5281        25595 :           continue;
    5282        25595 :         }
    5283              : 
    5284              :   /* Add a placeholder for simduid.  */
    5285        81463 :   if (sctx.is_simt && maybe_ne (sctx.max_vf, 1U))
    5286            0 :     sctx.simt_eargs.safe_push (NULL_TREE);
    5287              : 
    5288        81463 :   unsigned task_reduction_cnt = 0;
    5289        81463 :   unsigned task_reduction_cntorig = 0;
    5290        81463 :   unsigned task_reduction_cnt_full = 0;
    5291        81463 :   unsigned task_reduction_cntorig_full = 0;
    5292        81463 :   unsigned task_reduction_other_cnt = 0;
    5293        81463 :   tree tskred_atype = NULL_TREE, tskred_avar = NULL_TREE;
    5294        81463 :   tree tskred_base = NULL_TREE, tskred_temp = NULL_TREE;
    5295              :   /* Do all the fixed sized types in the first pass, and the variable sized
    5296              :      types in the second pass.  This makes sure that the scalar arguments to
    5297              :      the variable sized types are processed before we use them in the
    5298              :      variable sized operations.  For task reductions we use 4 passes, in the
    5299              :      first two we ignore them, in the third one gather arguments for
    5300              :      GOMP_task_reduction_remap call and in the last pass actually handle
    5301              :      the task reductions.  */
    5302       494960 :   for (pass = 0; pass < ((task_reduction_cnt || task_reduction_other_cnt)
    5303       247480 :                          ? 4 : 2); ++pass)
    5304              :     {
    5305       166021 :       if (pass == 2 && task_reduction_cnt)
    5306              :         {
    5307          878 :           tskred_atype
    5308          878 :             = build_array_type_nelts (ptr_type_node, task_reduction_cnt
    5309          878 :                                                      + task_reduction_cntorig);
    5310          878 :           tskred_avar = create_tmp_var_raw (tskred_atype);
    5311          878 :           gimple_add_tmp_var (tskred_avar);
    5312          878 :           TREE_ADDRESSABLE (tskred_avar) = 1;
    5313          878 :           task_reduction_cnt_full = task_reduction_cnt;
    5314          878 :           task_reduction_cntorig_full = task_reduction_cntorig;
    5315              :         }
    5316       165143 :       else if (pass == 3 && task_reduction_cnt)
    5317              :         {
    5318          878 :           x = builtin_decl_explicit (BUILT_IN_GOMP_TASK_REDUCTION_REMAP);
    5319          878 :           gimple *g
    5320          878 :             = gimple_build_call (x, 3, size_int (task_reduction_cnt),
    5321          878 :                                  size_int (task_reduction_cntorig),
    5322              :                                  build_fold_addr_expr (tskred_avar));
    5323          878 :           gimple_seq_add_stmt (ilist, g);
    5324              :         }
    5325       166021 :       if (pass == 3 && task_reduction_other_cnt)
    5326              :         {
    5327              :           /* For reduction clauses, build
    5328              :              tskred_base = (void *) tskred_temp[2]
    5329              :                            + omp_get_thread_num () * tskred_temp[1]
    5330              :              or if tskred_temp[1] is known to be constant, that constant
    5331              :              directly.  This is the start of the private reduction copy block
    5332              :              for the current thread.  */
    5333          849 :           tree v = create_tmp_var (integer_type_node);
    5334          849 :           x = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
    5335          849 :           gimple *g = gimple_build_call (x, 0);
    5336          849 :           gimple_call_set_lhs (g, v);
    5337          849 :           gimple_seq_add_stmt (ilist, g);
    5338          849 :           c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
    5339          849 :           tskred_temp = OMP_CLAUSE_DECL (c);
    5340          849 :           if (is_taskreg_ctx (ctx))
    5341          559 :             tskred_temp = lookup_decl (tskred_temp, ctx);
    5342          849 :           tree v2 = create_tmp_var (sizetype);
    5343          849 :           g = gimple_build_assign (v2, NOP_EXPR, v);
    5344          849 :           gimple_seq_add_stmt (ilist, g);
    5345          849 :           if (ctx->task_reductions[0])
    5346          832 :             v = fold_convert (sizetype, ctx->task_reductions[0]);
    5347              :           else
    5348           17 :             v = task_reduction_read (ilist, tskred_temp, sizetype, 1);
    5349          849 :           tree v3 = create_tmp_var (sizetype);
    5350          849 :           g = gimple_build_assign (v3, MULT_EXPR, v2, v);
    5351          849 :           gimple_seq_add_stmt (ilist, g);
    5352          849 :           v = task_reduction_read (ilist, tskred_temp, ptr_type_node, 2);
    5353          849 :           tskred_base = create_tmp_var (ptr_type_node);
    5354          849 :           g = gimple_build_assign (tskred_base, POINTER_PLUS_EXPR, v, v3);
    5355          849 :           gimple_seq_add_stmt (ilist, g);
    5356              :         }
    5357       166021 :       task_reduction_cnt = 0;
    5358       166021 :       task_reduction_cntorig = 0;
    5359       166021 :       task_reduction_other_cnt = 0;
    5360       810060 :       for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
    5361              :         {
    5362       644043 :           enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
    5363       644043 :           tree var, new_var;
    5364       644043 :           bool by_ref;
    5365       644043 :           location_t clause_loc = OMP_CLAUSE_LOCATION (c);
    5366       644043 :           bool task_reduction_p = false;
    5367       644043 :           bool task_reduction_needs_orig_p = false;
    5368       644043 :           tree cond = NULL_TREE;
    5369       644043 :           tree allocator, allocate_ptr;
    5370              : 
    5371       644043 :           switch (c_kind)
    5372              :             {
    5373       148968 :             case OMP_CLAUSE_PRIVATE:
    5374       148968 :               if (OMP_CLAUSE_PRIVATE_DEBUG (c))
    5375       462770 :                 continue;
    5376              :               break;
    5377        63780 :             case OMP_CLAUSE_SHARED:
    5378              :               /* Ignore shared directives in teams construct inside
    5379              :                  of target construct.  */
    5380        63780 :               if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
    5381        63780 :                   && !is_host_teams_ctx (ctx))
    5382        25416 :                 continue;
    5383        38364 :               if (maybe_lookup_decl (OMP_CLAUSE_DECL (c), ctx) == NULL)
    5384              :                 {
    5385        12424 :                   gcc_assert (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c)
    5386              :                               || is_global_var (OMP_CLAUSE_DECL (c)));
    5387        12424 :                   continue;
    5388              :                 }
    5389              :             case OMP_CLAUSE_FIRSTPRIVATE:
    5390              :             case OMP_CLAUSE_COPYIN:
    5391              :               break;
    5392        15800 :             case OMP_CLAUSE_LINEAR:
    5393        15800 :               if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
    5394        15800 :                   && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
    5395              :                 lastprivate_firstprivate = true;
    5396              :               break;
    5397        39032 :             case OMP_CLAUSE_REDUCTION:
    5398        39032 :             case OMP_CLAUSE_IN_REDUCTION:
    5399        39032 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
    5400        30684 :                   || is_task_ctx (ctx)
    5401        67376 :                   || OMP_CLAUSE_REDUCTION_TASK (c))
    5402              :                 {
    5403        12948 :                   task_reduction_p = true;
    5404        12948 :                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
    5405              :                     {
    5406         4600 :                       task_reduction_other_cnt++;
    5407         4600 :                       if (pass == 2)
    5408         1150 :                         continue;
    5409              :                     }
    5410              :                   else
    5411         8348 :                     task_reduction_cnt++;
    5412        11798 :                   if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
    5413              :                     {
    5414          756 :                       var = OMP_CLAUSE_DECL (c);
    5415              :                       /* If var is a global variable that isn't privatized
    5416              :                          in outer contexts, we don't need to look up the
    5417              :                          original address, it is always the address of the
    5418              :                          global variable itself.  */
    5419          756 :                       if (!DECL_P (var)
    5420          332 :                           || omp_privatize_by_reference (var)
    5421         1013 :                           || !is_global_var
    5422          257 :                                 (maybe_lookup_decl_in_outer_ctx (var, ctx)))
    5423              :                         {
    5424          640 :                           task_reduction_needs_orig_p = true;
    5425          640 :                           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
    5426          532 :                             task_reduction_cntorig++;
    5427              :                         }
    5428              :                     }
    5429              :                 }
    5430        26084 :               else if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
    5431       381642 :                 reduction_omp_orig_ref = true;
    5432              :               break;
    5433         3396 :             case OMP_CLAUSE__REDUCTEMP_:
    5434         3396 :               if (!is_taskreg_ctx (ctx))
    5435         1160 :                 continue;
    5436              :               /* FALLTHRU */
    5437       121108 :             case OMP_CLAUSE__LOOPTEMP_:
    5438              :               /* Handle _looptemp_/_reductemp_ clauses only on
    5439              :                  parallel/task.  */
    5440       121108 :               if (fd)
    5441        75594 :                 continue;
    5442              :               break;
    5443        46764 :             case OMP_CLAUSE_LASTPRIVATE:
    5444        46764 :               if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
    5445              :                 {
    5446         2066 :                   lastprivate_firstprivate = true;
    5447         2066 :                   if (pass != 0 || is_taskloop_ctx (ctx))
    5448         1347 :                     continue;
    5449              :                 }
    5450              :               /* Even without corresponding firstprivate, if
    5451              :                  decl is Fortran allocatable, it needs outer var
    5452              :                  reference.  */
    5453        44698 :               else if (pass == 0
    5454        67011 :                        && lang_hooks.decls.omp_private_outer_ref
    5455        22313 :                                                         (OMP_CLAUSE_DECL (c)))
    5456              :                 lastprivate_firstprivate = true;
    5457              :               break;
    5458          274 :             case OMP_CLAUSE_ALIGNED:
    5459          274 :               if (pass != 1)
    5460          137 :                 continue;
    5461          137 :               var = OMP_CLAUSE_DECL (c);
    5462          137 :               if (TREE_CODE (TREE_TYPE (var)) == POINTER_TYPE
    5463          137 :                   && !is_global_var (var))
    5464              :                 {
    5465           73 :                   new_var = maybe_lookup_decl (var, ctx);
    5466           73 :                   if (new_var == NULL_TREE)
    5467            6 :                     new_var = maybe_lookup_decl_in_outer_ctx (var, ctx);
    5468           73 :                   x = builtin_decl_explicit (BUILT_IN_ASSUME_ALIGNED);
    5469           73 :                   tree alarg = omp_clause_aligned_alignment (c);
    5470           73 :                   alarg = fold_convert_loc (clause_loc, size_type_node, alarg);
    5471           73 :                   x = build_call_expr_loc (clause_loc, x, 2, new_var, alarg);
    5472           73 :                   x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
    5473           73 :                   x = build2 (MODIFY_EXPR, TREE_TYPE (new_var), new_var, x);
    5474           73 :                   gimplify_and_add (x, ilist);
    5475              :                 }
    5476           64 :               else if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE
    5477           64 :                        && is_global_var (var))
    5478              :                 {
    5479           64 :                   tree ptype = build_pointer_type (TREE_TYPE (var)), t, t2;
    5480           64 :                   new_var = lookup_decl (var, ctx);
    5481           64 :                   t = maybe_lookup_decl_in_outer_ctx (var, ctx);
    5482           64 :                   t = build_fold_addr_expr_loc (clause_loc, t);
    5483           64 :                   t2 = builtin_decl_explicit (BUILT_IN_ASSUME_ALIGNED);
    5484           64 :                   tree alarg = omp_clause_aligned_alignment (c);
    5485           64 :                   alarg = fold_convert_loc (clause_loc, size_type_node, alarg);
    5486           64 :                   t = build_call_expr_loc (clause_loc, t2, 2, t, alarg);
    5487           64 :                   t = fold_convert_loc (clause_loc, ptype, t);
    5488           64 :                   x = create_tmp_var (ptype);
    5489           64 :                   t = build2 (MODIFY_EXPR, ptype, x, t);
    5490           64 :                   gimplify_and_add (t, ilist);
    5491           64 :                   t = build_simple_mem_ref_loc (clause_loc, x);
    5492           64 :                   SET_DECL_VALUE_EXPR (new_var, t);
    5493           64 :                   DECL_HAS_VALUE_EXPR_P (new_var) = 1;
    5494              :                 }
    5495          137 :               continue;
    5496         1634 :             case OMP_CLAUSE__CONDTEMP_:
    5497         1634 :               if (is_parallel_ctx (ctx)
    5498         1634 :                   || (is_simd && !OMP_CLAUSE__CONDTEMP__ITER (c)))
    5499              :                 break;
    5500         1158 :               continue;
    5501       142954 :             default:
    5502       142954 :               continue;
    5503       144249 :             }
    5504              : 
    5505       381642 :           if (task_reduction_p != (pass >= 2))
    5506        15708 :             continue;
    5507              : 
    5508       365934 :           allocator = NULL_TREE;
    5509       365934 :           allocate_ptr = NULL_TREE;
    5510       365934 :           new_var = var = OMP_CLAUSE_DECL (c);
    5511       365934 :           if ((c_kind == OMP_CLAUSE_REDUCTION
    5512       365934 :                || c_kind == OMP_CLAUSE_IN_REDUCTION)
    5513        31354 :               && TREE_CODE (var) == MEM_REF)
    5514              :             {
    5515         4530 :               var = TREE_OPERAND (var, 0);
    5516         4530 :               if (TREE_CODE (var) == POINTER_PLUS_EXPR)
    5517          773 :                 var = TREE_OPERAND (var, 0);
    5518         4530 :               if (TREE_CODE (var) == INDIRECT_REF
    5519         4306 :                   || TREE_CODE (var) == ADDR_EXPR)
    5520         2602 :                 var = TREE_OPERAND (var, 0);
    5521         4530 :               if (is_variable_sized (var))
    5522              :                 {
    5523          179 :                   gcc_assert (DECL_HAS_VALUE_EXPR_P (var));
    5524          179 :                   var = DECL_VALUE_EXPR (var);
    5525          179 :                   gcc_assert (TREE_CODE (var) == INDIRECT_REF);
    5526          179 :                   var = TREE_OPERAND (var, 0);
    5527          179 :                   gcc_assert (DECL_P (var));
    5528              :                 }
    5529         4530 :               new_var = var;
    5530              :             }
    5531        31354 :           if (c_kind == OMP_CLAUSE_IN_REDUCTION && is_omp_target (ctx->stmt))
    5532              :             {
    5533          844 :               splay_tree_key key = (splay_tree_key) &DECL_CONTEXT (var);
    5534          844 :               new_var = (tree) splay_tree_lookup (ctx->field_map, key)->value;
    5535              :             }
    5536       365090 :           else if (c_kind != OMP_CLAUSE_COPYIN)
    5537       364062 :             new_var = lookup_decl (var, ctx);
    5538              : 
    5539       365934 :           if (c_kind == OMP_CLAUSE_SHARED || c_kind == OMP_CLAUSE_COPYIN)
    5540              :             {
    5541        26292 :               if (pass != 0)
    5542        13145 :                 continue;
    5543              :             }
    5544              :           /* C/C++ array section reductions.  */
    5545       339642 :           else if ((c_kind == OMP_CLAUSE_REDUCTION
    5546              :                     || c_kind == OMP_CLAUSE_IN_REDUCTION)
    5547       339642 :                    && var != OMP_CLAUSE_DECL (c))
    5548              :             {
    5549         4530 :               if (pass == 0)
    5550          960 :                 continue;
    5551              : 
    5552         3570 :               tree bias = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
    5553         3570 :               tree orig_var = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
    5554              : 
    5555         3570 :               if (TREE_CODE (orig_var) == POINTER_PLUS_EXPR)
    5556              :                 {
    5557          723 :                   tree b = TREE_OPERAND (orig_var, 1);
    5558          723 :                   if (is_omp_target (ctx->stmt))
    5559              :                     b = NULL_TREE;
    5560              :                   else
    5561          723 :                     b = maybe_lookup_decl (b, ctx);
    5562          723 :                   if (b == NULL)
    5563              :                     {
    5564           17 :                       b = TREE_OPERAND (orig_var, 1);
    5565           17 :                       b = maybe_lookup_decl_in_outer_ctx (b, ctx);
    5566              :                     }
    5567          723 :                   if (integer_zerop (bias))
    5568              :                     bias = b;
    5569              :                   else
    5570              :                     {
    5571            0 :                       bias = fold_convert_loc (clause_loc,
    5572            0 :                                                TREE_TYPE (b), bias);
    5573            0 :                       bias = fold_build2_loc (clause_loc, PLUS_EXPR,
    5574            0 :                                               TREE_TYPE (b), b, bias);
    5575              :                     }
    5576          723 :                   orig_var = TREE_OPERAND (orig_var, 0);
    5577              :                 }
    5578         3570 :               if (pass == 2)
    5579              :                 {
    5580         1149 :                   tree out = maybe_lookup_decl_in_outer_ctx (var, ctx);
    5581         1149 :                   if (is_global_var (out)
    5582          241 :                       && TREE_CODE (TREE_TYPE (out)) != POINTER_TYPE
    5583         1336 :                       && (TREE_CODE (TREE_TYPE (out)) != REFERENCE_TYPE
    5584            0 :                           || (TREE_CODE (TREE_TYPE (TREE_TYPE (out)))
    5585              :                               != POINTER_TYPE)))
    5586              :                     x = var;
    5587          962 :                   else if (is_omp_target (ctx->stmt))
    5588              :                     x = out;
    5589              :                   else
    5590              :                     {
    5591          866 :                       bool by_ref = use_pointer_for_field (var, NULL);
    5592          866 :                       x = build_receiver_ref (var, by_ref, ctx);
    5593          866 :                       if (TREE_CODE (TREE_TYPE (var)) == REFERENCE_TYPE
    5594          866 :                           && (TREE_CODE (TREE_TYPE (TREE_TYPE (var)))
    5595              :                               == POINTER_TYPE))
    5596           32 :                         x = build_fold_addr_expr (x);
    5597              :                     }
    5598         1149 :                   if (TREE_CODE (orig_var) == INDIRECT_REF)
    5599           40 :                     x = build_simple_mem_ref (x);
    5600         1109 :                   else if (TREE_CODE (orig_var) == ADDR_EXPR)
    5601              :                     {
    5602          646 :                       if (var == TREE_OPERAND (orig_var, 0))
    5603          583 :                         x = build_fold_addr_expr (x);
    5604              :                     }
    5605         1149 :                   bias = fold_convert (sizetype, bias);
    5606         1149 :                   x = fold_convert (ptr_type_node, x);
    5607         1149 :                   x = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
    5608         1149 :                                        TREE_TYPE (x), x, bias);
    5609         1149 :                   unsigned cnt = task_reduction_cnt - 1;
    5610         1149 :                   if (!task_reduction_needs_orig_p)
    5611         1061 :                     cnt += (task_reduction_cntorig_full
    5612         1061 :                             - task_reduction_cntorig);
    5613              :                   else
    5614           88 :                     cnt = task_reduction_cntorig - 1;
    5615         1149 :                   tree r = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
    5616         1149 :                                    size_int (cnt), NULL_TREE, NULL_TREE);
    5617         1149 :                   gimplify_assign (r, x, ilist);
    5618         1149 :                   continue;
    5619         1149 :                 }
    5620              : 
    5621         2421 :               if (TREE_CODE (orig_var) == INDIRECT_REF
    5622         2309 :                   || TREE_CODE (orig_var) == ADDR_EXPR)
    5623         1410 :                 orig_var = TREE_OPERAND (orig_var, 0);
    5624         2421 :               tree d = OMP_CLAUSE_DECL (c);
    5625         2421 :               tree type = TREE_TYPE (d);
    5626         2421 :               gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
    5627         2421 :               tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    5628         2421 :               tree sz = v;
    5629         2421 :               const char *name = get_name (orig_var);
    5630         2421 :               if (pass != 3 && !TREE_CONSTANT (v))
    5631              :                 {
    5632           93 :                   tree t;
    5633           93 :                   if (is_omp_target (ctx->stmt))
    5634              :                     t = NULL_TREE;
    5635              :                   else
    5636           93 :                     t = maybe_lookup_decl (v, ctx);
    5637           93 :                   if (t)
    5638           88 :                     v = t;
    5639              :                   else
    5640            5 :                     v = maybe_lookup_decl_in_outer_ctx (v, ctx);
    5641           93 :                   gimplify_expr (&v, ilist, NULL, is_gimple_val, fb_rvalue);
    5642          186 :                   t = fold_build2_loc (clause_loc, PLUS_EXPR,
    5643           93 :                                        TREE_TYPE (v), v,
    5644           93 :                                        build_int_cst (TREE_TYPE (v), 1));
    5645           93 :                   sz = fold_build2_loc (clause_loc, MULT_EXPR,
    5646           93 :                                         TREE_TYPE (v), t,
    5647           93 :                                         TYPE_SIZE_UNIT (TREE_TYPE (type)));
    5648              :                 }
    5649         2421 :               if (pass == 3)
    5650              :                 {
    5651         1461 :                   tree xv = create_tmp_var (ptr_type_node);
    5652         1461 :                   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
    5653              :                     {
    5654         1149 :                       unsigned cnt = task_reduction_cnt - 1;
    5655         1149 :                       if (!task_reduction_needs_orig_p)
    5656         1061 :                         cnt += (task_reduction_cntorig_full
    5657         1061 :                                 - task_reduction_cntorig);
    5658              :                       else
    5659           88 :                         cnt = task_reduction_cntorig - 1;
    5660         1149 :                       x = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
    5661         1149 :                                   size_int (cnt), NULL_TREE, NULL_TREE);
    5662              : 
    5663         1149 :                       gimple *g = gimple_build_assign (xv, x);
    5664         1149 :                       gimple_seq_add_stmt (ilist, g);
    5665              :                     }
    5666              :                   else
    5667              :                     {
    5668          312 :                       unsigned int idx = *ctx->task_reduction_map->get (c);
    5669          312 :                       tree off;
    5670          312 :                       if (ctx->task_reductions[1 + idx])
    5671           81 :                         off = fold_convert (sizetype,
    5672              :                                             ctx->task_reductions[1 + idx]);
    5673              :                       else
    5674          231 :                         off = task_reduction_read (ilist, tskred_temp, sizetype,
    5675          231 :                                                    7 + 3 * idx + 1);
    5676          312 :                       gimple *g = gimple_build_assign (xv, POINTER_PLUS_EXPR,
    5677              :                                                        tskred_base, off);
    5678          312 :                       gimple_seq_add_stmt (ilist, g);
    5679              :                     }
    5680         1461 :                   x = fold_convert (build_pointer_type (boolean_type_node),
    5681              :                                     xv);
    5682         1461 :                   if (TREE_CONSTANT (v))
    5683         1065 :                     x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (x), x,
    5684              :                                      TYPE_SIZE_UNIT (type));
    5685              :                   else
    5686              :                     {
    5687          396 :                       tree t;
    5688          396 :                       if (is_omp_target (ctx->stmt))
    5689              :                         t = NULL_TREE;
    5690              :                       else
    5691          336 :                         t = maybe_lookup_decl (v, ctx);
    5692          336 :                       if (t)
    5693          316 :                         v = t;
    5694              :                       else
    5695           80 :                         v = maybe_lookup_decl_in_outer_ctx (v, ctx);
    5696          396 :                       gimplify_expr (&v, ilist, NULL, is_gimple_val,
    5697              :                                      fb_rvalue);
    5698          792 :                       t = fold_build2_loc (clause_loc, PLUS_EXPR,
    5699          396 :                                            TREE_TYPE (v), v,
    5700          396 :                                            build_int_cst (TREE_TYPE (v), 1));
    5701          396 :                       t = fold_build2_loc (clause_loc, MULT_EXPR,
    5702          396 :                                            TREE_TYPE (v), t,
    5703          396 :                                            TYPE_SIZE_UNIT (TREE_TYPE (type)));
    5704          396 :                       x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (x), x, t);
    5705              :                     }
    5706         1461 :                   cond = create_tmp_var (TREE_TYPE (x));
    5707         1461 :                   gimplify_assign (cond, x, ilist);
    5708         1461 :                   x = xv;
    5709              :                 }
    5710          960 :               else if (lower_private_allocate (var, type, allocator,
    5711              :                                                allocate_ptr, ilist, ctx,
    5712              :                                                true,
    5713          960 :                                                TREE_CONSTANT (v)
    5714          867 :                                                ? TYPE_SIZE_UNIT (type)
    5715              :                                                : sz))
    5716          300 :                 x = allocate_ptr;
    5717          660 :               else if (TREE_CONSTANT (v))
    5718              :                 {
    5719          572 :                   x = create_tmp_var_raw (type, name);
    5720          572 :                   gimple_add_tmp_var (x);
    5721          572 :                   TREE_ADDRESSABLE (x) = 1;
    5722          572 :                   x = build_fold_addr_expr_loc (clause_loc, x);
    5723              :                 }
    5724              :               else
    5725              :                 {
    5726           88 :                   tree atmp
    5727           88 :                     = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
    5728           88 :                   tree al = size_int (TYPE_ALIGN (TREE_TYPE (type)));
    5729           88 :                   x = build_call_expr_loc (clause_loc, atmp, 2, sz, al);
    5730              :                 }
    5731              : 
    5732         2421 :               tree ptype = build_pointer_type (TREE_TYPE (type));
    5733         2421 :               x = fold_convert_loc (clause_loc, ptype, x);
    5734         2421 :               tree y = create_tmp_var (ptype, name);
    5735         2421 :               gimplify_assign (y, x, ilist);
    5736         2421 :               x = y;
    5737         2421 :               tree yb = y;
    5738              : 
    5739         2421 :               if (!integer_zerop (bias))
    5740              :                 {
    5741         1542 :                   bias = fold_convert_loc (clause_loc, pointer_sized_int_node,
    5742              :                                            bias);
    5743         1542 :                   yb = fold_convert_loc (clause_loc, pointer_sized_int_node,
    5744              :                                          x);
    5745         1542 :                   yb = fold_build2_loc (clause_loc, MINUS_EXPR,
    5746              :                                         pointer_sized_int_node, yb, bias);
    5747         1542 :                   x = fold_convert_loc (clause_loc, TREE_TYPE (x), yb);
    5748         1542 :                   yb = create_tmp_var (ptype, name);
    5749         1542 :                   gimplify_assign (yb, x, ilist);
    5750         1542 :                   x = yb;
    5751              :                 }
    5752              : 
    5753         2421 :               d = TREE_OPERAND (d, 0);
    5754         2421 :               if (TREE_CODE (d) == POINTER_PLUS_EXPR)
    5755          420 :                 d = TREE_OPERAND (d, 0);
    5756         2421 :               if (TREE_CODE (d) == ADDR_EXPR)
    5757              :                 {
    5758         1298 :                   if (orig_var != var)
    5759              :                     {
    5760           91 :                       gcc_assert (is_variable_sized (orig_var));
    5761           91 :                       x = fold_convert_loc (clause_loc, TREE_TYPE (new_var),
    5762              :                                             x);
    5763           91 :                       gimplify_assign (new_var, x, ilist);
    5764           91 :                       tree new_orig_var = lookup_decl (orig_var, ctx);
    5765           91 :                       tree t = build_fold_indirect_ref (new_var);
    5766           91 :                       DECL_IGNORED_P (new_var) = 0;
    5767           91 :                       TREE_THIS_NOTRAP (t) = 1;
    5768           91 :                       SET_DECL_VALUE_EXPR (new_orig_var, t);
    5769           91 :                       DECL_HAS_VALUE_EXPR_P (new_orig_var) = 1;
    5770              :                     }
    5771              :                   else
    5772              :                     {
    5773         1207 :                       x = build2 (MEM_REF, TREE_TYPE (new_var), x,
    5774              :                                   build_int_cst (ptype, 0));
    5775         1207 :                       SET_DECL_VALUE_EXPR (new_var, x);
    5776         1207 :                       DECL_HAS_VALUE_EXPR_P (new_var) = 1;
    5777              :                     }
    5778              :                 }
    5779              :               else
    5780              :                 {
    5781         1123 :                   gcc_assert (orig_var == var);
    5782         1123 :                   if (TREE_CODE (d) == INDIRECT_REF)
    5783              :                     {
    5784          112 :                       x = create_tmp_var (ptype, name);
    5785          112 :                       TREE_ADDRESSABLE (x) = 1;
    5786          112 :                       gimplify_assign (x, yb, ilist);
    5787          112 :                       x = build_fold_addr_expr_loc (clause_loc, x);
    5788              :                     }
    5789         1123 :                   x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
    5790         1123 :                   gimplify_assign (new_var, x, ilist);
    5791              :                 }
    5792              :               /* GOMP_taskgroup_reduction_register memsets the whole
    5793              :                  array to zero.  If the initializer is zero, we don't
    5794              :                  need to initialize it again, just mark it as ever
    5795              :                  used unconditionally, i.e. cond = true.  */
    5796         2421 :               if (cond
    5797         1461 :                   && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
    5798         3354 :                   && initializer_zerop (omp_reduction_init (c,
    5799          933 :                                                             TREE_TYPE (type))))
    5800              :                 {
    5801          624 :                   gimple *g = gimple_build_assign (build_simple_mem_ref (cond),
    5802              :                                                    boolean_true_node);
    5803          624 :                   gimple_seq_add_stmt (ilist, g);
    5804          624 :                   continue;
    5805          624 :                 }
    5806         1797 :               tree end = create_artificial_label (UNKNOWN_LOCATION);
    5807         1797 :               if (cond)
    5808              :                 {
    5809          837 :                   gimple *g;
    5810          837 :                   if (!is_parallel_ctx (ctx))
    5811              :                     {
    5812          786 :                       tree condv = create_tmp_var (boolean_type_node);
    5813          786 :                       g = gimple_build_assign (condv,
    5814              :                                                build_simple_mem_ref (cond));
    5815          786 :                       gimple_seq_add_stmt (ilist, g);
    5816          786 :                       tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
    5817          786 :                       g = gimple_build_cond (NE_EXPR, condv,
    5818              :                                              boolean_false_node, end, lab1);
    5819          786 :                       gimple_seq_add_stmt (ilist, g);
    5820          786 :                       gimple_seq_add_stmt (ilist, gimple_build_label (lab1));
    5821              :                     }
    5822          837 :                   g = gimple_build_assign (build_simple_mem_ref (cond),
    5823              :                                            boolean_true_node);
    5824          837 :                   gimple_seq_add_stmt (ilist, g);
    5825              :                 }
    5826              : 
    5827         1797 :               tree y1 = create_tmp_var (ptype);
    5828         1797 :               gimplify_assign (y1, y, ilist);
    5829         1797 :               tree i2 = NULL_TREE, y2 = NULL_TREE;
    5830         1797 :               tree body2 = NULL_TREE, end2 = NULL_TREE;
    5831         1797 :               tree y3 = NULL_TREE, y4 = NULL_TREE;
    5832         1797 :               if (task_reduction_needs_orig_p)
    5833              :                 {
    5834          112 :                   y3 = create_tmp_var (ptype);
    5835          112 :                   tree ref;
    5836          112 :                   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
    5837           88 :                     ref = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
    5838           88 :                                   size_int (task_reduction_cnt_full
    5839              :                                             + task_reduction_cntorig - 1),
    5840              :                                   NULL_TREE, NULL_TREE);
    5841              :                   else
    5842              :                     {
    5843           24 :                       unsigned int idx = *ctx->task_reduction_map->get (c);
    5844           24 :                       ref = task_reduction_read (ilist, tskred_temp, ptype,
    5845           24 :                                                  7 + 3 * idx);
    5846              :                     }
    5847          112 :                   gimplify_assign (y3, ref, ilist);
    5848              :                 }
    5849         1685 :               else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) || is_simd)
    5850              :                 {
    5851          672 :                   if (pass != 3)
    5852              :                     {
    5853          256 :                       y2 = create_tmp_var (ptype);
    5854          256 :                       gimplify_assign (y2, y, ilist);
    5855              :                     }
    5856          672 :                   if (is_simd || OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
    5857              :                     {
    5858          192 :                       tree ref = build_outer_var_ref (var, ctx);
    5859              :                       /* For ref build_outer_var_ref already performs this.  */
    5860          192 :                       if (TREE_CODE (d) == INDIRECT_REF)
    5861            0 :                         gcc_assert (omp_privatize_by_reference (var));
    5862          192 :                       else if (TREE_CODE (d) == ADDR_EXPR)
    5863          100 :                         ref = build_fold_addr_expr (ref);
    5864           92 :                       else if (omp_privatize_by_reference (var))
    5865            8 :                         ref = build_fold_addr_expr (ref);
    5866          192 :                       ref = fold_convert_loc (clause_loc, ptype, ref);
    5867          192 :                       if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
    5868          192 :                           && OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
    5869              :                         {
    5870            8 :                           y3 = create_tmp_var (ptype);
    5871            8 :                           gimplify_assign (y3, unshare_expr (ref), ilist);
    5872              :                         }
    5873          192 :                       if (is_simd)
    5874              :                         {
    5875          184 :                           y4 = create_tmp_var (ptype);
    5876          184 :                           gimplify_assign (y4, ref, dlist);
    5877              :                         }
    5878              :                     }
    5879              :                 }
    5880         1797 :               tree i = create_tmp_var (TREE_TYPE (v));
    5881         1797 :               gimplify_assign (i, build_int_cst (TREE_TYPE (v), 0), ilist);
    5882         1797 :               tree body = create_artificial_label (UNKNOWN_LOCATION);
    5883         1797 :               gimple_seq_add_stmt (ilist, gimple_build_label (body));
    5884         1797 :               if (y2)
    5885              :                 {
    5886          256 :                   i2 = create_tmp_var (TREE_TYPE (v));
    5887          256 :                   gimplify_assign (i2, build_int_cst (TREE_TYPE (v), 0), dlist);
    5888          256 :                   body2 = create_artificial_label (UNKNOWN_LOCATION);
    5889          256 :                   end2 = create_artificial_label (UNKNOWN_LOCATION);
    5890          256 :                   gimple_seq_add_stmt (dlist, gimple_build_label (body2));
    5891              :                 }
    5892         1797 :               if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    5893              :                 {
    5894          600 :                   tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
    5895          600 :                   tree decl_placeholder
    5896          600 :                     = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c);
    5897          600 :                   SET_DECL_VALUE_EXPR (decl_placeholder,
    5898              :                                        build_simple_mem_ref (y1));
    5899          600 :                   DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 1;
    5900          600 :                   SET_DECL_VALUE_EXPR (placeholder,
    5901              :                                        y3 ? build_simple_mem_ref (y3)
    5902              :                                        : error_mark_node);
    5903          600 :                   DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
    5904          600 :                   x = lang_hooks.decls.omp_clause_default_ctor
    5905          720 :                                 (c, build_simple_mem_ref (y1),
    5906          120 :                                  y3 ? build_simple_mem_ref (y3) : NULL_TREE);
    5907          600 :                   if (x)
    5908          152 :                     gimplify_and_add (x, ilist);
    5909          600 :                   if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
    5910              :                     {
    5911          576 :                       gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
    5912          576 :                       lower_omp (&tseq, ctx);
    5913          576 :                       gimple_seq_add_seq (ilist, tseq);
    5914              :                     }
    5915          600 :                   OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
    5916          600 :                   if (is_simd)
    5917              :                     {
    5918            0 :                       SET_DECL_VALUE_EXPR (decl_placeholder,
    5919              :                                            build_simple_mem_ref (y2));
    5920            0 :                       SET_DECL_VALUE_EXPR (placeholder,
    5921              :                                            build_simple_mem_ref (y4));
    5922            0 :                       gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
    5923            0 :                       lower_omp (&tseq, ctx);
    5924            0 :                       gimple_seq_add_seq (dlist, tseq);
    5925            0 :                       OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
    5926              :                     }
    5927          600 :                   DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
    5928          600 :                   DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 0;
    5929          600 :                   if (y2)
    5930              :                     {
    5931           72 :                       x = lang_hooks.decls.omp_clause_dtor
    5932           72 :                                                 (c, build_simple_mem_ref (y2));
    5933           72 :                       if (x)
    5934           40 :                         gimplify_and_add (x, dlist);
    5935              :                     }
    5936              :                 }
    5937              :               else
    5938              :                 {
    5939         1197 :                   x = omp_reduction_init (c, TREE_TYPE (type));
    5940         1197 :                   enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
    5941              : 
    5942              :                   /* reduction(-:var) sums up the partial results, so it
    5943              :                      acts identically to reduction(+:var).  */
    5944         1197 :                   if (code == MINUS_EXPR)
    5945            0 :                     code = PLUS_EXPR;
    5946              : 
    5947         1197 :                   gimplify_assign (build_simple_mem_ref (y1), x, ilist);
    5948         1197 :                   if (is_simd)
    5949              :                     {
    5950          184 :                       x = build2 (code, TREE_TYPE (type),
    5951              :                                   build_simple_mem_ref (y4),
    5952              :                                   build_simple_mem_ref (y2));
    5953          184 :                       gimplify_assign (build_simple_mem_ref (y4), x, dlist);
    5954              :                     }
    5955              :                 }
    5956         1797 :               gimple *g
    5957         1797 :                 = gimple_build_assign (y1, POINTER_PLUS_EXPR, y1,
    5958         1797 :                                        TYPE_SIZE_UNIT (TREE_TYPE (type)));
    5959         1797 :               gimple_seq_add_stmt (ilist, g);
    5960         1797 :               if (y3)
    5961              :                 {
    5962          120 :                   g = gimple_build_assign (y3, POINTER_PLUS_EXPR, y3,
    5963          120 :                                            TYPE_SIZE_UNIT (TREE_TYPE (type)));
    5964          120 :                   gimple_seq_add_stmt (ilist, g);
    5965              :                 }
    5966         1797 :               g = gimple_build_assign (i, PLUS_EXPR, i,
    5967         1797 :                                        build_int_cst (TREE_TYPE (i), 1));
    5968         1797 :               gimple_seq_add_stmt (ilist, g);
    5969         1797 :               g = gimple_build_cond (LE_EXPR, i, v, body, end);
    5970         1797 :               gimple_seq_add_stmt (ilist, g);
    5971         1797 :               gimple_seq_add_stmt (ilist, gimple_build_label (end));
    5972         1797 :               if (y2)
    5973              :                 {
    5974          256 :                   g = gimple_build_assign (y2, POINTER_PLUS_EXPR, y2,
    5975          256 :                                            TYPE_SIZE_UNIT (TREE_TYPE (type)));
    5976          256 :                   gimple_seq_add_stmt (dlist, g);
    5977          256 :                   if (y4)
    5978              :                     {
    5979          184 :                       g = gimple_build_assign
    5980          184 :                                         (y4, POINTER_PLUS_EXPR, y4,
    5981          184 :                                          TYPE_SIZE_UNIT (TREE_TYPE (type)));
    5982          184 :                       gimple_seq_add_stmt (dlist, g);
    5983              :                     }
    5984          256 :                   g = gimple_build_assign (i2, PLUS_EXPR, i2,
    5985          256 :                                            build_int_cst (TREE_TYPE (i2), 1));
    5986          256 :                   gimple_seq_add_stmt (dlist, g);
    5987          256 :                   g = gimple_build_cond (LE_EXPR, i2, v, body2, end2);
    5988          256 :                   gimple_seq_add_stmt (dlist, g);
    5989          256 :                   gimple_seq_add_stmt (dlist, gimple_build_label (end2));
    5990              :                 }
    5991         1797 :               if (allocator)
    5992              :                 {
    5993          300 :                   tree f = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
    5994          300 :                   g = gimple_build_call (f, 2, allocate_ptr, allocator);
    5995          300 :                   gimple_seq_add_stmt (dlist, g);
    5996              :                 }
    5997         1797 :               continue;
    5998         1797 :             }
    5999       335112 :           else if (pass == 2)
    6000              :             {
    6001          938 :               tree out = maybe_lookup_decl_in_outer_ctx (var, ctx);
    6002          938 :               if (is_global_var (out))
    6003              :                 x = var;
    6004          382 :               else if (is_omp_target (ctx->stmt))
    6005              :                 x = out;
    6006              :               else
    6007              :                 {
    6008          322 :                   bool by_ref = use_pointer_for_field (var, ctx);
    6009          322 :                   x = build_receiver_ref (var, by_ref, ctx);
    6010              :                 }
    6011          938 :               if (!omp_privatize_by_reference (var))
    6012          836 :                 x = build_fold_addr_expr (x);
    6013          938 :               x = fold_convert (ptr_type_node, x);
    6014          938 :               unsigned cnt = task_reduction_cnt - 1;
    6015          938 :               if (!task_reduction_needs_orig_p)
    6016          893 :                 cnt += task_reduction_cntorig_full - task_reduction_cntorig;
    6017              :               else
    6018           45 :                 cnt = task_reduction_cntorig - 1;
    6019          938 :               tree r = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
    6020          938 :                                size_int (cnt), NULL_TREE, NULL_TREE);
    6021          938 :               gimplify_assign (r, x, ilist);
    6022          938 :               continue;
    6023          938 :             }
    6024       334174 :           else if (pass == 3)
    6025              :             {
    6026         1776 :               tree type = TREE_TYPE (new_var);
    6027         1776 :               if (!omp_privatize_by_reference (var))
    6028         1654 :                 type = build_pointer_type (type);
    6029         1776 :               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
    6030              :                 {
    6031          938 :                   unsigned cnt = task_reduction_cnt - 1;
    6032          938 :                   if (!task_reduction_needs_orig_p)
    6033          893 :                     cnt += (task_reduction_cntorig_full
    6034          893 :                             - task_reduction_cntorig);
    6035              :                   else
    6036           45 :                     cnt = task_reduction_cntorig - 1;
    6037          938 :                   x = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
    6038          938 :                               size_int (cnt), NULL_TREE, NULL_TREE);
    6039              :                 }
    6040              :               else
    6041              :                 {
    6042          838 :                   unsigned int idx = *ctx->task_reduction_map->get (c);
    6043          838 :                   tree off;
    6044          838 :                   if (ctx->task_reductions[1 + idx])
    6045          838 :                     off = fold_convert (sizetype,
    6046              :                                         ctx->task_reductions[1 + idx]);
    6047              :                   else
    6048            0 :                     off = task_reduction_read (ilist, tskred_temp, sizetype,
    6049            0 :                                                7 + 3 * idx + 1);
    6050          838 :                   x = fold_build2 (POINTER_PLUS_EXPR, ptr_type_node,
    6051              :                                    tskred_base, off);
    6052              :                 }
    6053         1776 :               x = fold_convert (type, x);
    6054         1776 :               tree t;
    6055         1776 :               if (omp_privatize_by_reference (var))
    6056              :                 {
    6057          122 :                   gimplify_assign (new_var, x, ilist);
    6058          122 :                   t = new_var;
    6059          122 :                   new_var = build_simple_mem_ref (new_var);
    6060              :                 }
    6061              :               else
    6062              :                 {
    6063         1654 :                   t = create_tmp_var (type);
    6064         1654 :                   gimplify_assign (t, x, ilist);
    6065         1654 :                   SET_DECL_VALUE_EXPR (new_var, build_simple_mem_ref (t));
    6066         1654 :                   DECL_HAS_VALUE_EXPR_P (new_var) = 1;
    6067              :                 }
    6068         1776 :               t = fold_convert (build_pointer_type (boolean_type_node), t);
    6069         1776 :               t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
    6070              :                                TYPE_SIZE_UNIT (TREE_TYPE (type)));
    6071         1776 :               cond = create_tmp_var (TREE_TYPE (t));
    6072         1776 :               gimplify_assign (cond, t, ilist);
    6073              :             }
    6074       332398 :           else if (is_variable_sized (var))
    6075              :             {
    6076              :               /* For variable sized types, we need to allocate the
    6077              :                  actual storage here.  Call alloca and store the
    6078              :                  result in the pointer decl that we created elsewhere.  */
    6079          270 :               if (pass == 0)
    6080          138 :                 continue;
    6081              : 
    6082          132 :               if (c_kind != OMP_CLAUSE_FIRSTPRIVATE || !is_task_ctx (ctx))
    6083              :                 {
    6084          118 :                   tree tmp;
    6085              : 
    6086          118 :                   ptr = DECL_VALUE_EXPR (new_var);
    6087          118 :                   gcc_assert (TREE_CODE (ptr) == INDIRECT_REF);
    6088          118 :                   ptr = TREE_OPERAND (ptr, 0);
    6089          118 :                   gcc_assert (DECL_P (ptr));
    6090          118 :                   x = TYPE_SIZE_UNIT (TREE_TYPE (new_var));
    6091              : 
    6092          118 :                   if (lower_private_allocate (var, new_var, allocator,
    6093              :                                               allocate_ptr, ilist, ctx,
    6094              :                                               false, x))
    6095            8 :                     tmp = allocate_ptr;
    6096              :                   else
    6097              :                     {
    6098              :                       /* void *tmp = __builtin_alloca */
    6099          110 :                       tree atmp
    6100          110 :                         = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
    6101          110 :                       gcall *stmt
    6102          110 :                         = gimple_build_call (atmp, 2, x,
    6103          110 :                                              size_int (DECL_ALIGN (var)));
    6104          110 :                       cfun->calls_alloca = 1;
    6105          110 :                       tmp = create_tmp_var_raw (ptr_type_node);
    6106          110 :                       gimple_add_tmp_var (tmp);
    6107          110 :                       gimple_call_set_lhs (stmt, tmp);
    6108              : 
    6109          110 :                       gimple_seq_add_stmt (ilist, stmt);
    6110              :                     }
    6111              : 
    6112          118 :                   x = fold_convert_loc (clause_loc, TREE_TYPE (ptr), tmp);
    6113          118 :                   gimplify_assign (ptr, x, ilist);
    6114              :                 }
    6115              :             }
    6116       332128 :           else if (omp_privatize_by_reference (var)
    6117       332128 :                    && (c_kind != OMP_CLAUSE_FIRSTPRIVATE
    6118         2018 :                        || !OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c)))
    6119              :             {
    6120              :               /* For references that are being privatized for Fortran,
    6121              :                  allocate new backing storage for the new pointer
    6122              :                  variable.  This allows us to avoid changing all the
    6123              :                  code that expects a pointer to something that expects
    6124              :                  a direct variable.  */
    6125         5069 :               if (pass == 0)
    6126         2637 :                 continue;
    6127              : 
    6128         2432 :               x = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var)));
    6129         2432 :               if (c_kind == OMP_CLAUSE_FIRSTPRIVATE && is_task_ctx (ctx))
    6130              :                 {
    6131          147 :                   x = build_receiver_ref (var, false, ctx);
    6132          147 :                   if (ctx->allocate_map)
    6133           16 :                     if (tree *allocatep = ctx->allocate_map->get (var))
    6134              :                       {
    6135           16 :                         allocator = *allocatep;
    6136           16 :                         if (TREE_CODE (allocator) == TREE_LIST)
    6137            0 :                           allocator = TREE_PURPOSE (allocator);
    6138           16 :                         if (TREE_CODE (allocator) != INTEGER_CST)
    6139            8 :                           allocator = build_outer_var_ref (allocator, ctx);
    6140           16 :                         allocator = fold_convert (pointer_sized_int_node,
    6141              :                                                   allocator);
    6142           16 :                         allocate_ptr = unshare_expr (x);
    6143              :                       }
    6144          147 :                   if (allocator == NULL_TREE)
    6145          131 :                     x = build_fold_addr_expr_loc (clause_loc, x);
    6146              :                 }
    6147         2285 :               else if (lower_private_allocate (var, new_var, allocator,
    6148              :                                                allocate_ptr,
    6149              :                                                ilist, ctx, true, x))
    6150           95 :                 x = allocate_ptr;
    6151         2190 :               else if (TREE_CONSTANT (x))
    6152              :                 {
    6153              :                   /* For reduction in SIMD loop, defer adding the
    6154              :                      initialization of the reference, because if we decide
    6155              :                      to use SIMD array for it, the initialization could cause
    6156              :                      expansion ICE.  Ditto for other privatization clauses.  */
    6157         1481 :                   if (is_simd)
    6158              :                     x = NULL_TREE;
    6159              :                   else
    6160              :                     {
    6161         1108 :                       x = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_var)),
    6162              :                                               get_name (var));
    6163         1108 :                       gimple_add_tmp_var (x);
    6164         1108 :                       TREE_ADDRESSABLE (x) = 1;
    6165         1108 :                       x = build_fold_addr_expr_loc (clause_loc, x);
    6166              :                     }
    6167              :                 }
    6168              :               else
    6169              :                 {
    6170          709 :                   tree atmp
    6171          709 :                     = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
    6172          709 :                   tree rtype = TREE_TYPE (TREE_TYPE (new_var));
    6173          709 :                   tree al = size_int (TYPE_ALIGN (rtype));
    6174          709 :                   x = build_call_expr_loc (clause_loc, atmp, 2, x, al);
    6175              :                 }
    6176              : 
    6177         2059 :               if (x)
    6178              :                 {
    6179         2059 :                   x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
    6180         2059 :                   gimplify_assign (new_var, x, ilist);
    6181              :                 }
    6182              : 
    6183         2432 :               new_var = build_simple_mem_ref_loc (clause_loc, new_var);
    6184              :             }
    6185       327059 :           else if ((c_kind == OMP_CLAUSE_REDUCTION
    6186              :                     || c_kind == OMP_CLAUSE_IN_REDUCTION)
    6187       327059 :                    && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    6188              :             {
    6189         2006 :               if (pass == 0)
    6190         1003 :                 continue;
    6191              :             }
    6192       325053 :           else if (pass != 0)
    6193       162270 :             continue;
    6194              : 
    6195       181273 :           switch (OMP_CLAUSE_CODE (c))
    6196              :             {
    6197        12633 :             case OMP_CLAUSE_SHARED:
    6198              :               /* Ignore shared directives in teams construct inside
    6199              :                  target construct.  */
    6200        12633 :               if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
    6201        12633 :                   && !is_host_teams_ctx (ctx))
    6202            0 :                 continue;
    6203              :               /* Shared global vars are just accessed directly.  */
    6204        12633 :               if (is_global_var (new_var))
    6205              :                 break;
    6206              :               /* For taskloop firstprivate/lastprivate, represented
    6207              :                  as firstprivate and shared clause on the task, new_var
    6208              :                  is the firstprivate var.  */
    6209        12466 :               if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
    6210              :                 break;
    6211              :               /* Set up the DECL_VALUE_EXPR for shared variables now.  This
    6212              :                  needs to be delayed until after fixup_child_record_type so
    6213              :                  that we get the correct type during the dereference.  */
    6214        12154 :               by_ref = use_pointer_for_field (var, ctx);
    6215        12154 :               x = build_receiver_ref (var, by_ref, ctx);
    6216        12154 :               SET_DECL_VALUE_EXPR (new_var, x);
    6217        12154 :               DECL_HAS_VALUE_EXPR_P (new_var) = 1;
    6218              : 
    6219              :               /* ??? If VAR is not passed by reference, and the variable
    6220              :                  hasn't been initialized yet, then we'll get a warning for
    6221              :                  the store into the omp_data_s structure.  Ideally, we'd be
    6222              :                  able to notice this and not store anything at all, but
    6223              :                  we're generating code too early.  Suppress the warning.  */
    6224        12154 :               if (!by_ref)
    6225         5338 :                 suppress_warning (var, OPT_Wuninitialized);
    6226              :               break;
    6227              : 
    6228          238 :             case OMP_CLAUSE__CONDTEMP_:
    6229          238 :               if (is_parallel_ctx (ctx))
    6230              :                 {
    6231          107 :                   x = build_receiver_ref (var, false, ctx);
    6232          107 :                   SET_DECL_VALUE_EXPR (new_var, x);
    6233          107 :                   DECL_HAS_VALUE_EXPR_P (new_var) = 1;
    6234              :                 }
    6235          131 :               else if (is_simd && !OMP_CLAUSE__CONDTEMP__ITER (c))
    6236              :                 {
    6237          131 :                   x = build_zero_cst (TREE_TYPE (var));
    6238          131 :                   goto do_private;
    6239              :                 }
    6240              :               break;
    6241              : 
    6242        22821 :             case OMP_CLAUSE_LASTPRIVATE:
    6243        22821 :               if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
    6244              :                 break;
    6245              :               /* FALLTHRU */
    6246              : 
    6247        95391 :             case OMP_CLAUSE_PRIVATE:
    6248        95391 :               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE)
    6249        22313 :                 x = build_outer_var_ref (var, ctx);
    6250        73078 :               else if (OMP_CLAUSE_PRIVATE_OUTER_REF (c))
    6251              :                 {
    6252          163 :                   if (is_task_ctx (ctx))
    6253           12 :                     x = build_receiver_ref (var, false, ctx);
    6254              :                   else
    6255          151 :                     x = build_outer_var_ref (var, ctx, OMP_CLAUSE_PRIVATE);
    6256              :                 }
    6257              :               else
    6258              :                 x = NULL;
    6259       102147 :             do_private:
    6260       102147 :               tree nx;
    6261       102147 :               bool copy_ctor;
    6262       102147 :               copy_ctor = false;
    6263       102147 :               lower_private_allocate (var, new_var, allocator, allocate_ptr,
    6264              :                                       ilist, ctx, false, NULL_TREE);
    6265       102147 :               nx = unshare_expr (new_var);
    6266       102147 :               if (is_simd
    6267        28660 :                   && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    6268       110357 :                   && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
    6269           22 :                 copy_ctor = true;
    6270       102147 :               if (copy_ctor)
    6271           22 :                 nx = lang_hooks.decls.omp_clause_copy_ctor (c, nx, x);
    6272              :               else
    6273       102125 :                 nx = lang_hooks.decls.omp_clause_default_ctor (c, nx, x);
    6274       102147 :               if (is_simd)
    6275              :                 {
    6276        28660 :                   tree y = lang_hooks.decls.omp_clause_dtor (c, new_var);
    6277        27268 :                   if ((TREE_ADDRESSABLE (new_var) || nx || y
    6278        27222 :                        || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    6279         7208 :                            && (gimple_omp_for_collapse (ctx->stmt) != 1
    6280         1388 :                                || (gimple_omp_for_index (ctx->stmt, 0)
    6281              :                                    != new_var)))
    6282        20316 :                        || OMP_CLAUSE_CODE (c) == OMP_CLAUSE__CONDTEMP_
    6283        20185 :                        || omp_privatize_by_reference (var))
    6284        35778 :                       && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
    6285              :                                                        ivar, lvar))
    6286              :                     {
    6287         6899 :                       if (omp_privatize_by_reference (var))
    6288              :                         {
    6289           93 :                           gcc_assert (TREE_CODE (new_var) == MEM_REF);
    6290           93 :                           tree new_vard = TREE_OPERAND (new_var, 0);
    6291           93 :                           gcc_assert (DECL_P (new_vard));
    6292           93 :                           SET_DECL_VALUE_EXPR (new_vard,
    6293              :                                                build_fold_addr_expr (lvar));
    6294           93 :                           DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
    6295              :                         }
    6296              : 
    6297         6899 :                       if (nx)
    6298              :                         {
    6299           76 :                           tree iv = unshare_expr (ivar);
    6300           76 :                           if (copy_ctor)
    6301           22 :                             x = lang_hooks.decls.omp_clause_copy_ctor (c, iv,
    6302              :                                                                        x);
    6303              :                           else
    6304           54 :                             x = lang_hooks.decls.omp_clause_default_ctor (c,
    6305              :                                                                           iv,
    6306              :                                                                           x);
    6307              :                         }
    6308         6823 :                       else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__CONDTEMP_)
    6309              :                         {
    6310           61 :                           x = build2 (MODIFY_EXPR, TREE_TYPE (ivar),
    6311              :                                       unshare_expr (ivar), x);
    6312           61 :                           nx = x;
    6313              :                         }
    6314         6899 :                       if (nx && x)
    6315          137 :                         gimplify_and_add (x, &llist[0]);
    6316         6899 :                       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    6317         6899 :                           && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
    6318              :                         {
    6319           61 :                           tree v = new_var;
    6320           61 :                           if (!DECL_P (v))
    6321              :                             {
    6322           14 :                               gcc_assert (TREE_CODE (v) == MEM_REF);
    6323           14 :                               v = TREE_OPERAND (v, 0);
    6324           14 :                               gcc_assert (DECL_P (v));
    6325              :                             }
    6326           61 :                           v = *ctx->lastprivate_conditional_map->get (v);
    6327           61 :                           tree t = create_tmp_var (TREE_TYPE (v));
    6328           61 :                           tree z = build_zero_cst (TREE_TYPE (v));
    6329           61 :                           tree orig_v
    6330           61 :                             = build_outer_var_ref (var, ctx,
    6331              :                                                    OMP_CLAUSE_LASTPRIVATE);
    6332           61 :                           gimple_seq_add_stmt (dlist,
    6333           61 :                                                gimple_build_assign (t, z));
    6334           61 :                           gcc_assert (DECL_HAS_VALUE_EXPR_P (v));
    6335           61 :                           tree civar = DECL_VALUE_EXPR (v);
    6336           61 :                           gcc_assert (TREE_CODE (civar) == ARRAY_REF);
    6337           61 :                           civar = unshare_expr (civar);
    6338           61 :                           TREE_OPERAND (civar, 1) = sctx.idx;
    6339           61 :                           x = build2 (MODIFY_EXPR, TREE_TYPE (t), t,
    6340              :                                       unshare_expr (civar));
    6341          122 :                           x = build2 (COMPOUND_EXPR, TREE_TYPE (orig_v), x,
    6342           61 :                                       build2 (MODIFY_EXPR, TREE_TYPE (orig_v),
    6343              :                                               orig_v, unshare_expr (ivar)));
    6344           61 :                           tree cond = build2 (LT_EXPR, boolean_type_node, t,
    6345              :                                               civar);
    6346           61 :                           x = build3 (COND_EXPR, void_type_node, cond, x,
    6347              :                                       void_node);
    6348           61 :                           gimple_seq tseq = NULL;
    6349           61 :                           gimplify_and_add (x, &tseq);
    6350           61 :                           if (ctx->outer)
    6351           35 :                             lower_omp (&tseq, ctx->outer);
    6352           61 :                           gimple_seq_add_seq (&llist[1], tseq);
    6353              :                         }
    6354         6899 :                       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    6355         6899 :                           && ctx->for_simd_scan_phase)
    6356              :                         {
    6357            9 :                           x = unshare_expr (ivar);
    6358            9 :                           tree orig_v
    6359            9 :                             = build_outer_var_ref (var, ctx,
    6360              :                                                    OMP_CLAUSE_LASTPRIVATE);
    6361            9 :                           x = lang_hooks.decls.omp_clause_assign_op (c, x,
    6362              :                                                                      orig_v);
    6363            9 :                           gimplify_and_add (x, &llist[0]);
    6364              :                         }
    6365         6899 :                       if (y)
    6366              :                         {
    6367           76 :                           y = lang_hooks.decls.omp_clause_dtor (c, ivar);
    6368           76 :                           if (y)
    6369           76 :                             gimplify_and_add (y, &llist[1]);
    6370              :                         }
    6371              :                       break;
    6372              :                     }
    6373        21761 :                   if (omp_privatize_by_reference (var))
    6374              :                     {
    6375           33 :                       gcc_assert (TREE_CODE (new_var) == MEM_REF);
    6376           33 :                       tree new_vard = TREE_OPERAND (new_var, 0);
    6377           33 :                       gcc_assert (DECL_P (new_vard));
    6378           33 :                       tree type = TREE_TYPE (TREE_TYPE (new_vard));
    6379           33 :                       x = TYPE_SIZE_UNIT (type);
    6380           33 :                       if (TREE_CONSTANT (x))
    6381              :                         {
    6382           33 :                           x = create_tmp_var_raw (type, get_name (var));
    6383           33 :                           gimple_add_tmp_var (x);
    6384           33 :                           TREE_ADDRESSABLE (x) = 1;
    6385           33 :                           x = build_fold_addr_expr_loc (clause_loc, x);
    6386           33 :                           x = fold_convert_loc (clause_loc,
    6387           33 :                                                 TREE_TYPE (new_vard), x);
    6388           33 :                           gimplify_assign (new_vard, x, ilist);
    6389              :                         }
    6390              :                     }
    6391              :                 }
    6392        95248 :               if (nx)
    6393          533 :                 gimplify_and_add (nx, ilist);
    6394        95248 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    6395        15904 :                   && is_simd
    6396        97049 :                   && ctx->for_simd_scan_phase)
    6397              :                 {
    6398            5 :                   tree orig_v = build_outer_var_ref (var, ctx,
    6399              :                                                      OMP_CLAUSE_LASTPRIVATE);
    6400            5 :                   x = lang_hooks.decls.omp_clause_assign_op (c, new_var,
    6401              :                                                              orig_v);
    6402            5 :                   gimplify_and_add (x, ilist);
    6403              :                 }
    6404              :               /* FALLTHRU */
    6405              : 
    6406       126708 :             do_dtor:
    6407       126708 :               x = lang_hooks.decls.omp_clause_dtor (c, new_var);
    6408       126708 :               if (x)
    6409         1179 :                 gimplify_and_add (x, dlist);
    6410       126708 :               if (allocator)
    6411              :                 {
    6412          891 :                   if (!is_gimple_val (allocator))
    6413              :                     {
    6414           22 :                       tree avar = create_tmp_var (TREE_TYPE (allocator));
    6415           22 :                       gimplify_assign (avar, allocator, dlist);
    6416           22 :                       allocator = avar;
    6417              :                     }
    6418          891 :                   if (!is_gimple_val (allocate_ptr))
    6419              :                     {
    6420           59 :                       tree apvar = create_tmp_var (TREE_TYPE (allocate_ptr));
    6421           59 :                       gimplify_assign (apvar, allocate_ptr, dlist);
    6422           59 :                       allocate_ptr = apvar;
    6423              :                     }
    6424          891 :                   tree f = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
    6425          891 :                   gimple *g
    6426          891 :                     = gimple_build_call (f, 2, allocate_ptr, allocator);
    6427          891 :                   gimple_seq_add_stmt (dlist, g);
    6428              :                 }
    6429              :               break;
    6430              : 
    6431         7900 :             case OMP_CLAUSE_LINEAR:
    6432         7900 :               if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
    6433         1275 :                 goto do_firstprivate;
    6434         6625 :               if (OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
    6435              :                 x = NULL;
    6436              :               else
    6437         3638 :                 x = build_outer_var_ref (var, ctx);
    6438         6625 :               goto do_private;
    6439              : 
    6440        29274 :             case OMP_CLAUSE_FIRSTPRIVATE:
    6441        29274 :               if (is_task_ctx (ctx))
    6442              :                 {
    6443         4648 :                   if ((omp_privatize_by_reference (var)
    6444          147 :                        && !OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c))
    6445         4648 :                       || is_variable_sized (var))
    6446          161 :                     goto do_dtor;
    6447         4487 :                   else if (is_global_var (maybe_lookup_decl_in_outer_ctx (var,
    6448              :                                                                           ctx))
    6449         4487 :                            || use_pointer_for_field (var, NULL))
    6450              :                     {
    6451          430 :                       x = build_receiver_ref (var, false, ctx);
    6452          430 :                       if (ctx->allocate_map)
    6453           43 :                         if (tree *allocatep = ctx->allocate_map->get (var))
    6454              :                           {
    6455           43 :                             allocator = *allocatep;
    6456           43 :                             if (TREE_CODE (allocator) == TREE_LIST)
    6457            4 :                               allocator = TREE_PURPOSE (allocator);
    6458           43 :                             if (TREE_CODE (allocator) != INTEGER_CST)
    6459           14 :                               allocator = build_outer_var_ref (allocator, ctx);
    6460           43 :                             allocator = fold_convert (pointer_sized_int_node,
    6461              :                                                       allocator);
    6462           43 :                             allocate_ptr = unshare_expr (x);
    6463           43 :                             x = build_simple_mem_ref (x);
    6464           43 :                             TREE_THIS_NOTRAP (x) = 1;
    6465              :                           }
    6466          430 :                       SET_DECL_VALUE_EXPR (new_var, x);
    6467          430 :                       DECL_HAS_VALUE_EXPR_P (new_var) = 1;
    6468          430 :                       goto do_dtor;
    6469              :                     }
    6470              :                 }
    6471        28683 :               if (OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c)
    6472        28683 :                   && omp_privatize_by_reference (var))
    6473              :                 {
    6474            0 :                   x = build_outer_var_ref (var, ctx);
    6475            0 :                   gcc_assert (TREE_CODE (x) == MEM_REF
    6476              :                               && integer_zerop (TREE_OPERAND (x, 1)));
    6477            0 :                   x = TREE_OPERAND (x, 0);
    6478            0 :                   x = lang_hooks.decls.omp_clause_copy_ctor
    6479            0 :                                                 (c, unshare_expr (new_var), x);
    6480            0 :                   gimplify_and_add (x, ilist);
    6481            0 :                   goto do_dtor;
    6482              :                 }
    6483        29958 :             do_firstprivate:
    6484        29958 :               lower_private_allocate (var, new_var, allocator, allocate_ptr,
    6485              :                                       ilist, ctx, false, NULL_TREE);
    6486        29958 :               x = build_outer_var_ref (var, ctx);
    6487        29958 :               if (is_simd)
    6488              :                 {
    6489         1069 :                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
    6490         1069 :                       && gimple_omp_for_combined_into_p (ctx->stmt))
    6491              :                     {
    6492          668 :                       tree t = OMP_CLAUSE_LINEAR_STEP (c);
    6493          668 :                       if (DECL_P (t))
    6494          114 :                         t = build_outer_var_ref (t, ctx);
    6495          668 :                       tree stept = TREE_TYPE (t);
    6496          668 :                       tree ct = omp_find_clause (clauses,
    6497              :                                                  OMP_CLAUSE__LOOPTEMP_);
    6498          668 :                       gcc_assert (ct);
    6499          668 :                       tree l = OMP_CLAUSE_DECL (ct);
    6500          668 :                       tree n1 = fd->loop.n1;
    6501          668 :                       tree step = fd->loop.step;
    6502          668 :                       tree itype = TREE_TYPE (l);
    6503          668 :                       if (POINTER_TYPE_P (itype))
    6504            0 :                         itype = signed_type_for (itype);
    6505          668 :                       l = fold_build2 (MINUS_EXPR, itype, l, n1);
    6506          668 :                       if (TYPE_UNSIGNED (itype)
    6507          668 :                           && fd->loop.cond_code == GT_EXPR)
    6508            0 :                         l = fold_build2 (TRUNC_DIV_EXPR, itype,
    6509              :                                          fold_build1 (NEGATE_EXPR, itype, l),
    6510              :                                          fold_build1 (NEGATE_EXPR,
    6511              :                                                       itype, step));
    6512              :                       else
    6513          668 :                         l = fold_build2 (TRUNC_DIV_EXPR, itype, l, step);
    6514          668 :                       t = fold_build2 (MULT_EXPR, stept,
    6515              :                                        fold_convert (stept, l), t);
    6516              : 
    6517          668 :                       if (OMP_CLAUSE_LINEAR_ARRAY (c))
    6518              :                         {
    6519          108 :                           if (omp_privatize_by_reference (var))
    6520              :                             {
    6521           72 :                               gcc_assert (TREE_CODE (new_var) == MEM_REF);
    6522           72 :                               tree new_vard = TREE_OPERAND (new_var, 0);
    6523           72 :                               gcc_assert (DECL_P (new_vard));
    6524           72 :                               tree type = TREE_TYPE (TREE_TYPE (new_vard));
    6525           72 :                               nx = TYPE_SIZE_UNIT (type);
    6526           72 :                               if (TREE_CONSTANT (nx))
    6527              :                                 {
    6528           24 :                                   nx = create_tmp_var_raw (type,
    6529              :                                                            get_name (var));
    6530           24 :                                   gimple_add_tmp_var (nx);
    6531           24 :                                   TREE_ADDRESSABLE (nx) = 1;
    6532           24 :                                   nx = build_fold_addr_expr_loc (clause_loc,
    6533              :                                                                  nx);
    6534           24 :                                   nx = fold_convert_loc (clause_loc,
    6535           24 :                                                          TREE_TYPE (new_vard),
    6536              :                                                          nx);
    6537           24 :                                   gimplify_assign (new_vard, nx, ilist);
    6538              :                                 }
    6539              :                             }
    6540              : 
    6541          108 :                           x = lang_hooks.decls.omp_clause_linear_ctor
    6542          108 :                                                         (c, new_var, x, t);
    6543          108 :                           gimplify_and_add (x, ilist);
    6544          108 :                           goto do_dtor;
    6545              :                         }
    6546              : 
    6547          560 :                       if (POINTER_TYPE_P (TREE_TYPE (x)))
    6548           11 :                         x = fold_build_pointer_plus (x, t);
    6549              :                       else
    6550          549 :                         x = fold_build2 (PLUS_EXPR, TREE_TYPE (x), x,
    6551              :                                          fold_convert (TREE_TYPE (x), t));
    6552              :                     }
    6553              : 
    6554          961 :                   if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
    6555          940 :                        || TREE_ADDRESSABLE (new_var)
    6556          805 :                        || omp_privatize_by_reference (var))
    6557         1223 :                       && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
    6558              :                                                        ivar, lvar))
    6559              :                     {
    6560          152 :                       if (omp_privatize_by_reference (var))
    6561              :                         {
    6562           22 :                           gcc_assert (TREE_CODE (new_var) == MEM_REF);
    6563           22 :                           tree new_vard = TREE_OPERAND (new_var, 0);
    6564           22 :                           gcc_assert (DECL_P (new_vard));
    6565           22 :                           SET_DECL_VALUE_EXPR (new_vard,
    6566              :                                                build_fold_addr_expr (lvar));
    6567           22 :                           DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
    6568              :                         }
    6569          152 :                       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
    6570              :                         {
    6571          131 :                           tree iv = create_tmp_var (TREE_TYPE (new_var));
    6572          131 :                           x = lang_hooks.decls.omp_clause_copy_ctor (c, iv, x);
    6573          131 :                           gimplify_and_add (x, ilist);
    6574          131 :                           gimple_stmt_iterator gsi
    6575          131 :                             = gsi_start (*gimple_omp_body_ptr (ctx->stmt));
    6576          131 :                           gassign *g
    6577          131 :                             = gimple_build_assign (unshare_expr (lvar), iv);
    6578          131 :                           gsi_insert_before_without_update (&gsi, g,
    6579              :                                                             GSI_SAME_STMT);
    6580          131 :                           tree t = OMP_CLAUSE_LINEAR_STEP (c);
    6581          131 :                           enum tree_code code = PLUS_EXPR;
    6582          131 :                           if (POINTER_TYPE_P (TREE_TYPE (new_var)))
    6583              :                             code = POINTER_PLUS_EXPR;
    6584          131 :                           g = gimple_build_assign (iv, code, iv, t);
    6585          131 :                           gsi_insert_before_without_update (&gsi, g,
    6586              :                                                             GSI_SAME_STMT);
    6587          131 :                           break;
    6588              :                         }
    6589           21 :                       x = lang_hooks.decls.omp_clause_copy_ctor
    6590           21 :                                                 (c, unshare_expr (ivar), x);
    6591           21 :                       gimplify_and_add (x, &llist[0]);
    6592           21 :                       x = lang_hooks.decls.omp_clause_dtor (c, ivar);
    6593           21 :                       if (x)
    6594           21 :                         gimplify_and_add (x, &llist[1]);
    6595              :                       break;
    6596              :                     }
    6597          809 :                   if (omp_privatize_by_reference (var))
    6598              :                     {
    6599          105 :                       gcc_assert (TREE_CODE (new_var) == MEM_REF);
    6600          105 :                       tree new_vard = TREE_OPERAND (new_var, 0);
    6601          105 :                       gcc_assert (DECL_P (new_vard));
    6602          105 :                       tree type = TREE_TYPE (TREE_TYPE (new_vard));
    6603          105 :                       nx = TYPE_SIZE_UNIT (type);
    6604          105 :                       if (TREE_CONSTANT (nx))
    6605              :                         {
    6606           57 :                           nx = create_tmp_var_raw (type, get_name (var));
    6607           57 :                           gimple_add_tmp_var (nx);
    6608           57 :                           TREE_ADDRESSABLE (nx) = 1;
    6609           57 :                           nx = build_fold_addr_expr_loc (clause_loc, nx);
    6610           57 :                           nx = fold_convert_loc (clause_loc,
    6611           57 :                                                  TREE_TYPE (new_vard), nx);
    6612           57 :                           gimplify_assign (new_vard, nx, ilist);
    6613              :                         }
    6614              :                     }
    6615              :                 }
    6616        29698 :               x = lang_hooks.decls.omp_clause_copy_ctor
    6617        29698 :                                                 (c, unshare_expr (new_var), x);
    6618        29694 :               gimplify_and_add (x, ilist);
    6619        29694 :               goto do_dtor;
    6620              : 
    6621        20984 :             case OMP_CLAUSE__LOOPTEMP_:
    6622        20984 :             case OMP_CLAUSE__REDUCTEMP_:
    6623        20984 :               gcc_assert (is_taskreg_ctx (ctx));
    6624        20984 :               x = build_outer_var_ref (var, ctx);
    6625        20984 :               x = build2 (MODIFY_EXPR, TREE_TYPE (new_var), new_var, x);
    6626        20984 :               gimplify_and_add (x, ilist);
    6627        20984 :               break;
    6628              : 
    6629          514 :             case OMP_CLAUSE_COPYIN:
    6630          514 :               by_ref = use_pointer_for_field (var, NULL);
    6631          514 :               x = build_receiver_ref (var, by_ref, ctx);
    6632          514 :               x = lang_hooks.decls.omp_clause_assign_op (c, new_var, x);
    6633          514 :               append_to_statement_list (x, &copyin_seq);
    6634          514 :               copyin_by_ref |= by_ref;
    6635          514 :               break;
    6636              : 
    6637        13831 :             case OMP_CLAUSE_REDUCTION:
    6638        13831 :             case OMP_CLAUSE_IN_REDUCTION:
    6639              :               /* OpenACC reductions are initialized using the
    6640              :                  GOACC_REDUCTION internal function.  */
    6641        13831 :               if (is_gimple_omp_oacc (ctx->stmt))
    6642              :                 break;
    6643         9849 :               if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    6644              :                 {
    6645         1503 :                   tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
    6646         1503 :                   gimple *tseq;
    6647         1503 :                   tree ptype = TREE_TYPE (placeholder);
    6648         1503 :                   if (cond)
    6649              :                     {
    6650          327 :                       x = error_mark_node;
    6651          327 :                       if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c)
    6652          327 :                           && !task_reduction_needs_orig_p)
    6653           32 :                         x = var;
    6654          295 :                       else if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
    6655              :                         {
    6656           57 :                           tree pptype = build_pointer_type (ptype);
    6657           57 :                           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
    6658           45 :                             x = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
    6659           45 :                                         size_int (task_reduction_cnt_full
    6660              :                                                   + task_reduction_cntorig - 1),
    6661              :                                         NULL_TREE, NULL_TREE);
    6662              :                           else
    6663              :                             {
    6664           12 :                               unsigned int idx
    6665           12 :                                 = *ctx->task_reduction_map->get (c);
    6666           12 :                               x = task_reduction_read (ilist, tskred_temp,
    6667           12 :                                                        pptype, 7 + 3 * idx);
    6668              :                             }
    6669           57 :                           x = fold_convert (pptype, x);
    6670           57 :                           x = build_simple_mem_ref (x);
    6671              :                         }
    6672              :                     }
    6673              :                   else
    6674              :                     {
    6675         1176 :                       lower_private_allocate (var, new_var, allocator,
    6676              :                                               allocate_ptr, ilist, ctx, false,
    6677              :                                               NULL_TREE);
    6678         1176 :                       x = build_outer_var_ref (var, ctx);
    6679              : 
    6680         1176 :                       if (omp_privatize_by_reference (var)
    6681         1176 :                           && !useless_type_conversion_p (ptype, TREE_TYPE (x)))
    6682           49 :                         x = build_fold_addr_expr_loc (clause_loc, x);
    6683              :                     }
    6684         1503 :                   SET_DECL_VALUE_EXPR (placeholder, x);
    6685         1503 :                   DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
    6686         1503 :                   tree new_vard = new_var;
    6687         1503 :                   if (omp_privatize_by_reference (var))
    6688              :                     {
    6689          219 :                       gcc_assert (TREE_CODE (new_var) == MEM_REF);
    6690          219 :                       new_vard = TREE_OPERAND (new_var, 0);
    6691          219 :                       gcc_assert (DECL_P (new_vard));
    6692              :                     }
    6693         1503 :                   tree rvar = NULL_TREE, *rvarp = NULL, rvar2 = NULL_TREE;
    6694         1503 :                   if (is_simd
    6695          316 :                       && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
    6696         1819 :                       && OMP_CLAUSE_REDUCTION_INSCAN (c))
    6697              :                     rvarp = &rvar;
    6698         1503 :                   if (is_simd
    6699         1503 :                       && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
    6700              :                                                        ivar, lvar, rvarp,
    6701              :                                                        &rvar2))
    6702              :                     {
    6703          179 :                       if (new_vard == new_var)
    6704              :                         {
    6705          133 :                           gcc_assert (DECL_VALUE_EXPR (new_var) == lvar);
    6706          133 :                           SET_DECL_VALUE_EXPR (new_var, ivar);
    6707              :                         }
    6708              :                       else
    6709              :                         {
    6710           46 :                           SET_DECL_VALUE_EXPR (new_vard,
    6711              :                                                build_fold_addr_expr (ivar));
    6712           46 :                           DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
    6713              :                         }
    6714          179 :                       x = lang_hooks.decls.omp_clause_default_ctor
    6715          179 :                                 (c, unshare_expr (ivar),
    6716              :                                  build_outer_var_ref (var, ctx));
    6717          179 :                       if (rvarp && ctx->for_simd_scan_phase)
    6718              :                         {
    6719           16 :                           if (x)
    6720            8 :                             gimplify_and_add (x, &llist[0]);
    6721           16 :                           x = lang_hooks.decls.omp_clause_dtor (c, ivar);
    6722           16 :                           if (x)
    6723            8 :                             gimplify_and_add (x, &llist[1]);
    6724          506 :                           break;
    6725              :                         }
    6726           78 :                       else if (rvarp)
    6727              :                         {
    6728           78 :                           if (x)
    6729              :                             {
    6730           38 :                               gimplify_and_add (x, &llist[0]);
    6731              : 
    6732           38 :                               tree ivar2 = unshare_expr (lvar);
    6733           38 :                               TREE_OPERAND (ivar2, 1) = sctx.idx;
    6734           38 :                               x = lang_hooks.decls.omp_clause_default_ctor
    6735           38 :                                     (c, ivar2, build_outer_var_ref (var, ctx));
    6736           38 :                               gimplify_and_add (x, &llist[0]);
    6737              : 
    6738           38 :                               if (rvar2)
    6739              :                                 {
    6740           16 :                                   x = lang_hooks.decls.omp_clause_default_ctor
    6741           16 :                                         (c, unshare_expr (rvar2),
    6742              :                                          build_outer_var_ref (var, ctx));
    6743           16 :                                   gimplify_and_add (x, &llist[0]);
    6744              :                                 }
    6745              : 
    6746              :                               /* For types that need construction, add another
    6747              :                                  private var which will be default constructed
    6748              :                                  and optionally initialized with
    6749              :                                  OMP_CLAUSE_REDUCTION_GIMPLE_INIT, as in the
    6750              :                                  loop we want to assign this value instead of
    6751              :                                  constructing and destructing it in each
    6752              :                                  iteration.  */
    6753           38 :                               tree nv = create_tmp_var_raw (TREE_TYPE (ivar));
    6754           38 :                               gimple_add_tmp_var (nv);
    6755           60 :                               ctx->cb.decl_map->put (TREE_OPERAND (rvar2
    6756              :                                                                    ? rvar2
    6757              :                                                                    : ivar, 0),
    6758              :                                                      nv);
    6759           38 :                               x = lang_hooks.decls.omp_clause_default_ctor
    6760           38 :                                     (c, nv, build_outer_var_ref (var, ctx));
    6761           38 :                               gimplify_and_add (x, ilist);
    6762              : 
    6763           38 :                               if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
    6764              :                                 {
    6765           19 :                                   tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
    6766           19 :                                   x = DECL_VALUE_EXPR (new_vard);
    6767           19 :                                   tree vexpr = nv;
    6768           19 :                                   if (new_vard != new_var)
    6769           11 :                                     vexpr = build_fold_addr_expr (nv);
    6770           19 :                                   SET_DECL_VALUE_EXPR (new_vard, vexpr);
    6771           19 :                                   lower_omp (&tseq, ctx);
    6772           19 :                                   SET_DECL_VALUE_EXPR (new_vard, x);
    6773           19 :                                   gimple_seq_add_seq (ilist, tseq);
    6774           19 :                                   OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
    6775              :                                 }
    6776              : 
    6777           38 :                               x = lang_hooks.decls.omp_clause_dtor (c, nv);
    6778           38 :                               if (x)
    6779           38 :                                 gimplify_and_add (x, dlist);
    6780              :                             }
    6781              : 
    6782           78 :                           tree ref = build_outer_var_ref (var, ctx);
    6783           78 :                           x = unshare_expr (ivar);
    6784           78 :                           x = lang_hooks.decls.omp_clause_assign_op (c, x,
    6785              :                                                                      ref);
    6786           78 :                           gimplify_and_add (x, &llist[0]);
    6787              : 
    6788           78 :                           ref = build_outer_var_ref (var, ctx);
    6789           78 :                           x = lang_hooks.decls.omp_clause_assign_op (c, ref,
    6790              :                                                                      rvar);
    6791           78 :                           gimplify_and_add (x, &llist[3]);
    6792              : 
    6793           78 :                           DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
    6794           78 :                           if (new_vard == new_var)
    6795           48 :                             SET_DECL_VALUE_EXPR (new_var, lvar);
    6796              :                           else
    6797           30 :                             SET_DECL_VALUE_EXPR (new_vard,
    6798              :                                                  build_fold_addr_expr (lvar));
    6799              : 
    6800           78 :                           x = lang_hooks.decls.omp_clause_dtor (c, ivar);
    6801           78 :                           if (x)
    6802           38 :                             gimplify_and_add (x, &llist[1]);
    6803              : 
    6804           78 :                           tree ivar2 = unshare_expr (lvar);
    6805           78 :                           TREE_OPERAND (ivar2, 1) = sctx.idx;
    6806           78 :                           x = lang_hooks.decls.omp_clause_dtor (c, ivar2);
    6807           78 :                           if (x)
    6808           38 :                             gimplify_and_add (x, &llist[1]);
    6809              : 
    6810           78 :                           if (rvar2)
    6811              :                             {
    6812           36 :                               x = lang_hooks.decls.omp_clause_dtor (c, rvar2);
    6813           36 :                               if (x)
    6814           16 :                                 gimplify_and_add (x, &llist[1]);
    6815              :                             }
    6816              :                           break;
    6817              :                         }
    6818           85 :                       if (x)
    6819           34 :                         gimplify_and_add (x, &llist[0]);
    6820           85 :                       if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
    6821              :                         {
    6822           81 :                           tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
    6823           81 :                           lower_omp (&tseq, ctx);
    6824           81 :                           gimple_seq_add_seq (&llist[0], tseq);
    6825              :                         }
    6826           85 :                       OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
    6827           85 :                       tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
    6828           85 :                       lower_omp (&tseq, ctx);
    6829           85 :                       gimple_seq_add_seq (&llist[1], tseq);
    6830           85 :                       OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
    6831           85 :                       DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
    6832           85 :                       if (new_vard == new_var)
    6833           75 :                         SET_DECL_VALUE_EXPR (new_var, lvar);
    6834              :                       else
    6835           10 :                         SET_DECL_VALUE_EXPR (new_vard,
    6836              :                                              build_fold_addr_expr (lvar));
    6837           85 :                       x = lang_hooks.decls.omp_clause_dtor (c, ivar);
    6838           85 :                       if (x)
    6839           38 :                         gimplify_and_add (x, &llist[1]);
    6840              :                       break;
    6841              :                     }
    6842              :                   /* If this is a reference to constant size reduction var
    6843              :                      with placeholder, we haven't emitted the initializer
    6844              :                      for it because it is undesirable if SIMD arrays are used.
    6845              :                      But if they aren't used, we need to emit the deferred
    6846              :                      initialization now.  */
    6847         1324 :                   else if (omp_privatize_by_reference (var) && is_simd)
    6848           46 :                     handle_simd_reference (clause_loc, new_vard, ilist);
    6849              : 
    6850         1324 :                   tree lab2 = NULL_TREE;
    6851         1324 :                   if (cond)
    6852              :                     {
    6853          327 :                       gimple *g;
    6854          327 :                       if (!is_parallel_ctx (ctx))
    6855              :                         {
    6856          302 :                           tree condv = create_tmp_var (boolean_type_node);
    6857          302 :                           tree m = build_simple_mem_ref (cond);
    6858          302 :                           g = gimple_build_assign (condv, m);
    6859          302 :                           gimple_seq_add_stmt (ilist, g);
    6860          302 :                           tree lab1
    6861          302 :                             = create_artificial_label (UNKNOWN_LOCATION);
    6862          302 :                           lab2 = create_artificial_label (UNKNOWN_LOCATION);
    6863          302 :                           g = gimple_build_cond (NE_EXPR, condv,
    6864              :                                                  boolean_false_node,
    6865              :                                                  lab2, lab1);
    6866          302 :                           gimple_seq_add_stmt (ilist, g);
    6867          302 :                           gimple_seq_add_stmt (ilist,
    6868          302 :                                                gimple_build_label (lab1));
    6869              :                         }
    6870          327 :                       g = gimple_build_assign (build_simple_mem_ref (cond),
    6871              :                                                boolean_true_node);
    6872          327 :                       gimple_seq_add_stmt (ilist, g);
    6873              :                     }
    6874          327 :                   x = lang_hooks.decls.omp_clause_default_ctor
    6875         1324 :                                 (c, unshare_expr (new_var),
    6876              :                                  cond ? NULL_TREE
    6877          997 :                                  : build_outer_var_ref (var, ctx));
    6878         1324 :                   if (x)
    6879          302 :                     gimplify_and_add (x, ilist);
    6880              : 
    6881         1324 :                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
    6882         1324 :                       && OMP_CLAUSE_REDUCTION_INSCAN (c))
    6883              :                     {
    6884          158 :                       if (ctx->for_simd_scan_phase)
    6885          997 :                         goto do_dtor;
    6886          142 :                       if (x || (!is_simd
    6887           32 :                                 && OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c)))
    6888              :                         {
    6889           70 :                           tree nv = create_tmp_var_raw (TREE_TYPE (new_var));
    6890           70 :                           gimple_add_tmp_var (nv);
    6891           70 :                           ctx->cb.decl_map->put (new_vard, nv);
    6892           70 :                           x = lang_hooks.decls.omp_clause_default_ctor
    6893           70 :                                 (c, nv, build_outer_var_ref (var, ctx));
    6894           70 :                           if (x)
    6895           70 :                             gimplify_and_add (x, ilist);
    6896           70 :                           if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
    6897              :                             {
    6898           35 :                               tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
    6899           35 :                               tree vexpr = nv;
    6900           35 :                               if (new_vard != new_var)
    6901           19 :                                 vexpr = build_fold_addr_expr (nv);
    6902           35 :                               SET_DECL_VALUE_EXPR (new_vard, vexpr);
    6903           35 :                               DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
    6904           35 :                               lower_omp (&tseq, ctx);
    6905           35 :                               SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
    6906           35 :                               DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
    6907           35 :                               gimple_seq_add_seq (ilist, tseq);
    6908              :                             }
    6909           70 :                           OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
    6910           70 :                           if (is_simd && ctx->scan_exclusive)
    6911              :                             {
    6912           16 :                               tree nv2
    6913           16 :                                 = create_tmp_var_raw (TREE_TYPE (new_var));
    6914           16 :                               gimple_add_tmp_var (nv2);
    6915           16 :                               ctx->cb.decl_map->put (nv, nv2);
    6916           16 :                               x = lang_hooks.decls.omp_clause_default_ctor
    6917           16 :                                     (c, nv2, build_outer_var_ref (var, ctx));
    6918           16 :                               gimplify_and_add (x, ilist);
    6919           16 :                               x = lang_hooks.decls.omp_clause_dtor (c, nv2);
    6920           16 :                               if (x)
    6921           16 :                                 gimplify_and_add (x, dlist);
    6922              :                             }
    6923           70 :                           x = lang_hooks.decls.omp_clause_dtor (c, nv);
    6924           70 :                           if (x)
    6925           70 :                             gimplify_and_add (x, dlist);
    6926              :                         }
    6927           72 :                       else if (is_simd
    6928           40 :                                && ctx->scan_exclusive
    6929           92 :                                && TREE_ADDRESSABLE (TREE_TYPE (new_var)))
    6930              :                         {
    6931            0 :                           tree nv2 = create_tmp_var_raw (TREE_TYPE (new_var));
    6932            0 :                           gimple_add_tmp_var (nv2);
    6933            0 :                           ctx->cb.decl_map->put (new_vard, nv2);
    6934            0 :                           x = lang_hooks.decls.omp_clause_dtor (c, nv2);
    6935            0 :                           if (x)
    6936            0 :                             gimplify_and_add (x, dlist);
    6937              :                         }
    6938          142 :                       DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
    6939          142 :                       goto do_dtor;
    6940              :                     }
    6941              : 
    6942         1166 :                   if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
    6943              :                     {
    6944         1118 :                       tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
    6945         1118 :                       if (c_kind == OMP_CLAUSE_IN_REDUCTION
    6946         1118 :                           && is_omp_target (ctx->stmt))
    6947              :                         {
    6948           12 :                           tree d = maybe_lookup_decl_in_outer_ctx (var, ctx);
    6949           12 :                           tree oldv = NULL_TREE;
    6950           12 :                           gcc_assert (d);
    6951           12 :                           if (DECL_HAS_VALUE_EXPR_P (d))
    6952            6 :                             oldv = DECL_VALUE_EXPR (d);
    6953           12 :                           SET_DECL_VALUE_EXPR (d, new_vard);
    6954           12 :                           DECL_HAS_VALUE_EXPR_P (d) = 1;
    6955           12 :                           lower_omp (&tseq, ctx);
    6956           12 :                           if (oldv)
    6957            6 :                             SET_DECL_VALUE_EXPR (d, oldv);
    6958              :                           else
    6959              :                             {
    6960            6 :                               SET_DECL_VALUE_EXPR (d, NULL_TREE);
    6961            6 :                               DECL_HAS_VALUE_EXPR_P (d) = 0;
    6962              :                             }
    6963              :                         }
    6964              :                       else
    6965         1106 :                         lower_omp (&tseq, ctx);
    6966         1118 :                       gimple_seq_add_seq (ilist, tseq);
    6967              :                     }
    6968         1166 :                   OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
    6969         1166 :                   if (is_simd)
    6970              :                     {
    6971           43 :                       tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
    6972           43 :                       lower_omp (&tseq, ctx);
    6973           43 :                       gimple_seq_add_seq (dlist, tseq);
    6974           43 :                       OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
    6975              :                     }
    6976         1166 :                   DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
    6977         1166 :                   if (cond)
    6978              :                     {
    6979          327 :                       if (lab2)
    6980          302 :                         gimple_seq_add_stmt (ilist, gimple_build_label (lab2));
    6981              :                       break;
    6982              :                     }
    6983          839 :                   goto do_dtor;
    6984              :                 }
    6985              :               else
    6986              :                 {
    6987         8346 :                   x = omp_reduction_init (c, TREE_TYPE (new_var));
    6988         8346 :                   gcc_assert (TREE_CODE (TREE_TYPE (new_var)) != ARRAY_TYPE);
    6989         8346 :                   enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
    6990              : 
    6991         8346 :                   if (cond)
    6992              :                     {
    6993         1449 :                       gimple *g;
    6994         1449 :                       tree lab2 = NULL_TREE;
    6995              :                       /* GOMP_taskgroup_reduction_register memsets the whole
    6996              :                          array to zero.  If the initializer is zero, we don't
    6997              :                          need to initialize it again, just mark it as ever
    6998              :                          used unconditionally, i.e. cond = true.  */
    6999         1449 :                       if (initializer_zerop (x))
    7000              :                         {
    7001         1268 :                           g = gimple_build_assign (build_simple_mem_ref (cond),
    7002              :                                                    boolean_true_node);
    7003         1268 :                           gimple_seq_add_stmt (ilist, g);
    7004         3277 :                           break;
    7005              :                         }
    7006              : 
    7007              :                       /* Otherwise, emit
    7008              :                          if (!cond) { cond = true; new_var = x; }  */
    7009          181 :                       if (!is_parallel_ctx (ctx))
    7010              :                         {
    7011          177 :                           tree condv = create_tmp_var (boolean_type_node);
    7012          177 :                           tree m = build_simple_mem_ref (cond);
    7013          177 :                           g = gimple_build_assign (condv, m);
    7014          177 :                           gimple_seq_add_stmt (ilist, g);
    7015          177 :                           tree lab1
    7016          177 :                             = create_artificial_label (UNKNOWN_LOCATION);
    7017          177 :                           lab2 = create_artificial_label (UNKNOWN_LOCATION);
    7018          177 :                           g = gimple_build_cond (NE_EXPR, condv,
    7019              :                                                  boolean_false_node,
    7020              :                                                  lab2, lab1);
    7021          177 :                           gimple_seq_add_stmt (ilist, g);
    7022          177 :                           gimple_seq_add_stmt (ilist,
    7023          177 :                                                gimple_build_label (lab1));
    7024              :                         }
    7025          181 :                       g = gimple_build_assign (build_simple_mem_ref (cond),
    7026              :                                                boolean_true_node);
    7027          181 :                       gimple_seq_add_stmt (ilist, g);
    7028          181 :                       gimplify_assign (new_var, x, ilist);
    7029          181 :                       if (lab2)
    7030          177 :                         gimple_seq_add_stmt (ilist, gimple_build_label (lab2));
    7031              :                       break;
    7032              :                     }
    7033              : 
    7034              :                   /* reduction(-:var) sums up the partial results, so it
    7035              :                      acts identically to reduction(+:var).  */
    7036         6897 :                   if (code == MINUS_EXPR)
    7037           93 :                     code = PLUS_EXPR;
    7038              : 
    7039         6897 :                   bool is_truth_op
    7040         6897 :                     = (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR);
    7041         6897 :                   tree new_vard = new_var;
    7042         6897 :                   if (is_simd && omp_privatize_by_reference (var))
    7043              :                     {
    7044           52 :                       gcc_assert (TREE_CODE (new_var) == MEM_REF);
    7045           52 :                       new_vard = TREE_OPERAND (new_var, 0);
    7046           52 :                       gcc_assert (DECL_P (new_vard));
    7047              :                     }
    7048         6897 :                   tree rvar = NULL_TREE, *rvarp = NULL, rvar2 = NULL_TREE;
    7049         6897 :                   if (is_simd
    7050         2325 :                       && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
    7051         9222 :                       && OMP_CLAUSE_REDUCTION_INSCAN (c))
    7052              :                     rvarp = &rvar;
    7053         6897 :                   if (is_simd
    7054         6897 :                       && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
    7055              :                                                        ivar, lvar, rvarp,
    7056              :                                                        &rvar2))
    7057              :                     {
    7058         1095 :                       if (new_vard != new_var)
    7059              :                         {
    7060           34 :                           SET_DECL_VALUE_EXPR (new_vard,
    7061              :                                                build_fold_addr_expr (lvar));
    7062           34 :                           DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
    7063              :                         }
    7064              : 
    7065         1095 :                       tree ref = build_outer_var_ref (var, ctx);
    7066              : 
    7067         1095 :                       if (rvarp)
    7068              :                         {
    7069          197 :                           if (ctx->for_simd_scan_phase)
    7070              :                             break;
    7071          156 :                           gimplify_assign (ivar, ref, &llist[0]);
    7072          156 :                           ref = build_outer_var_ref (var, ctx);
    7073          156 :                           gimplify_assign (ref, rvar, &llist[3]);
    7074          156 :                           break;
    7075              :                         }
    7076              : 
    7077          898 :                       gimplify_assign (unshare_expr (ivar), x, &llist[0]);
    7078              : 
    7079          898 :                       if (sctx.is_simt)
    7080              :                         {
    7081            0 :                           if (!simt_lane)
    7082            0 :                             simt_lane = create_tmp_var (unsigned_type_node);
    7083            0 :                           x = build_call_expr_internal_loc
    7084            0 :                             (UNKNOWN_LOCATION, IFN_GOMP_SIMT_XCHG_BFLY,
    7085            0 :                              TREE_TYPE (ivar), 2, ivar, simt_lane);
    7086              :                           /* Make sure x is evaluated unconditionally.  */
    7087            0 :                           tree bfly_var = create_tmp_var (TREE_TYPE (ivar));
    7088            0 :                           gimplify_assign (bfly_var, x, &llist[2]);
    7089            0 :                           x = build2 (code, TREE_TYPE (ivar), ivar, bfly_var);
    7090            0 :                           gimplify_assign (ivar, x, &llist[2]);
    7091              :                         }
    7092          898 :                       tree ivar2 = ivar;
    7093          898 :                       tree ref2 = ref;
    7094          898 :                       if (is_truth_op)
    7095              :                         {
    7096          139 :                           tree zero = build_zero_cst (TREE_TYPE (ivar));
    7097          139 :                           ivar2 = fold_build2_loc (clause_loc, NE_EXPR,
    7098              :                                                    boolean_type_node, ivar,
    7099              :                                                    zero);
    7100          139 :                           ref2 = fold_build2_loc (clause_loc, NE_EXPR,
    7101              :                                                   boolean_type_node, ref,
    7102              :                                                   zero);
    7103              :                         }
    7104          898 :                       x = build2 (code, TREE_TYPE (ref), ref2, ivar2);
    7105          898 :                       if (is_truth_op)
    7106          139 :                         x = fold_convert (TREE_TYPE (ref), x);
    7107          898 :                       ref = build_outer_var_ref (var, ctx);
    7108          898 :                       gimplify_assign (ref, x, &llist[1]);
    7109              : 
    7110              :                     }
    7111              :                   else
    7112              :                     {
    7113         5802 :                       lower_private_allocate (var, new_var, allocator,
    7114              :                                               allocate_ptr, ilist, ctx,
    7115              :                                               false, NULL_TREE);
    7116         5802 :                       if (omp_privatize_by_reference (var) && is_simd)
    7117           18 :                         handle_simd_reference (clause_loc, new_vard, ilist);
    7118         5802 :                       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
    7119         5802 :                           && OMP_CLAUSE_REDUCTION_INSCAN (c))
    7120              :                         break;
    7121         5439 :                       gimplify_assign (new_var, x, ilist);
    7122         5439 :                       if (is_simd)
    7123              :                         {
    7124         1022 :                           tree ref = build_outer_var_ref (var, ctx);
    7125         1022 :                           tree new_var2 = new_var;
    7126         1022 :                           tree ref2 = ref;
    7127         1022 :                           if (is_truth_op)
    7128              :                             {
    7129           24 :                               tree zero = build_zero_cst (TREE_TYPE (new_var));
    7130           24 :                               new_var2
    7131           24 :                                 = fold_build2_loc (clause_loc, NE_EXPR,
    7132              :                                                    boolean_type_node, new_var,
    7133              :                                                    zero);
    7134           24 :                               ref2 = fold_build2_loc (clause_loc, NE_EXPR,
    7135              :                                                       boolean_type_node, ref,
    7136              :                                                       zero);
    7137              :                             }
    7138         1022 :                           x = build2 (code, TREE_TYPE (ref2), ref2, new_var2);
    7139         1022 :                           if (is_truth_op)
    7140           24 :                             x = fold_convert (TREE_TYPE (new_var), x);
    7141         1022 :                           ref = build_outer_var_ref (var, ctx);
    7142         1022 :                           gimplify_assign (ref, x, dlist);
    7143              :                         }
    7144         5439 :                       if (allocator)
    7145           70 :                         goto do_dtor;
    7146              :                     }
    7147              :                 }
    7148         6267 :               break;
    7149              : 
    7150            0 :             default:
    7151            0 :               gcc_unreachable ();
    7152              :             }
    7153              :         }
    7154              :     }
    7155        81459 :   if (tskred_avar)
    7156              :     {
    7157          878 :       tree clobber = build_clobber (TREE_TYPE (tskred_avar));
    7158          878 :       gimple_seq_add_stmt (ilist, gimple_build_assign (tskred_avar, clobber));
    7159              :     }
    7160              : 
    7161        81459 :   if (known_eq (sctx.max_vf, 1U))
    7162              :     {
    7163         1805 :       sctx.is_simt = false;
    7164         1805 :       if (ctx->lastprivate_conditional_map)
    7165              :         {
    7166           52 :           if (gimple_omp_for_combined_into_p (ctx->stmt))
    7167              :             {
    7168              :               /* Signal to lower_omp_1 that it should use parent context.  */
    7169           43 :               ctx->combined_into_simd_safelen1 = true;
    7170          370 :               for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
    7171          327 :                 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7172          327 :                     && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
    7173              :                   {
    7174           57 :                     tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
    7175           57 :                     omp_context *outer = ctx->outer;
    7176           57 :                     if (gimple_code (outer->stmt) == GIMPLE_OMP_SCAN)
    7177            6 :                       outer = outer->outer;
    7178           57 :                     tree *v = ctx->lastprivate_conditional_map->get (o);
    7179           57 :                     tree po = lookup_decl (OMP_CLAUSE_DECL (c), outer);
    7180           57 :                     tree *pv = outer->lastprivate_conditional_map->get (po);
    7181           57 :                     *v = *pv;
    7182              :                   }
    7183              :             }
    7184              :           else
    7185              :             {
    7186              :               /* When not vectorized, treat lastprivate(conditional:) like
    7187              :                  normal lastprivate, as there will be just one simd lane
    7188              :                  writing the privatized variable.  */
    7189            9 :               delete ctx->lastprivate_conditional_map;
    7190            9 :               ctx->lastprivate_conditional_map = NULL;
    7191              :             }
    7192              :         }
    7193              :     }
    7194              : 
    7195        81459 :   if (nonconst_simd_if)
    7196              :     {
    7197          625 :       if (sctx.lane == NULL_TREE)
    7198              :         {
    7199          565 :           sctx.idx = create_tmp_var (unsigned_type_node);
    7200          565 :           sctx.lane = create_tmp_var (unsigned_type_node);
    7201              :         }
    7202              :       /* FIXME: For now.  */
    7203          625 :       sctx.is_simt = false;
    7204              :     }
    7205              : 
    7206        81459 :   if (sctx.lane || sctx.is_simt)
    7207              :     {
    7208         4599 :       uid = create_tmp_var (ptr_type_node, "simduid");
    7209              :       /* Don't want uninit warnings on simduid, it is always uninitialized,
    7210              :          but we use it not for the value, but for the DECL_UID only.  */
    7211         4599 :       suppress_warning (uid, OPT_Wuninitialized);
    7212         4599 :       c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SIMDUID_);
    7213         4599 :       OMP_CLAUSE__SIMDUID__DECL (c) = uid;
    7214         4599 :       OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (ctx->stmt);
    7215         4599 :       gimple_omp_for_set_clauses (ctx->stmt, c);
    7216              :     }
    7217              :   /* Emit calls denoting privatized variables and initializing a pointer to
    7218              :      structure that holds private variables as fields after ompdevlow pass.  */
    7219        81459 :   if (sctx.is_simt)
    7220              :     {
    7221            0 :       sctx.simt_eargs[0] = uid;
    7222            0 :       gimple *g
    7223            0 :         = gimple_build_call_internal_vec (IFN_GOMP_SIMT_ENTER, sctx.simt_eargs);
    7224            0 :       gimple_call_set_lhs (g, uid);
    7225            0 :       gimple_seq_add_stmt (ilist, g);
    7226            0 :       sctx.simt_eargs.release ();
    7227              : 
    7228            0 :       simtrec = create_tmp_var (ptr_type_node, ".omp_simt");
    7229            0 :       g = gimple_build_call_internal (IFN_GOMP_SIMT_ENTER_ALLOC, 1, uid);
    7230            0 :       gimple_call_set_lhs (g, simtrec);
    7231            0 :       gimple_seq_add_stmt (ilist, g);
    7232              :     }
    7233        81459 :   if (sctx.lane)
    7234              :     {
    7235         8573 :       gimple *g = gimple_build_call_internal (IFN_GOMP_SIMD_LANE,
    7236              :                                               2 + (nonconst_simd_if != NULL),
    7237              :                                               uid, integer_zero_node,
    7238              :                                               nonconst_simd_if);
    7239         4599 :       gimple_call_set_lhs (g, sctx.lane);
    7240         4599 :       gimple_stmt_iterator gsi = gsi_start (*gimple_omp_body_ptr (ctx->stmt));
    7241         4599 :       gsi_insert_before_without_update (&gsi, g, GSI_SAME_STMT);
    7242         4599 :       g = gimple_build_assign (sctx.lane, INTEGER_CST,
    7243              :                                build_int_cst (unsigned_type_node, 0));
    7244         4599 :       gimple_seq_add_stmt (ilist, g);
    7245         4599 :       if (sctx.lastlane)
    7246              :         {
    7247          186 :           g = gimple_build_call_internal (IFN_GOMP_SIMD_LAST_LANE,
    7248              :                                           2, uid, sctx.lane);
    7249          186 :           gimple_call_set_lhs (g, sctx.lastlane);
    7250          186 :           gimple_seq_add_stmt (dlist, g);
    7251          186 :           gimple_seq_add_seq (dlist, llist[3]);
    7252              :         }
    7253              :       /* Emit reductions across SIMT lanes in log_2(simt_vf) steps.  */
    7254         4599 :       if (llist[2])
    7255              :         {
    7256            0 :           tree simt_vf = create_tmp_var (unsigned_type_node);
    7257            0 :           g = gimple_build_call_internal (IFN_GOMP_SIMT_VF, 0);
    7258            0 :           gimple_call_set_lhs (g, simt_vf);
    7259            0 :           gimple_seq_add_stmt (dlist, g);
    7260              : 
    7261            0 :           tree t = build_int_cst (unsigned_type_node, 1);
    7262            0 :           g = gimple_build_assign (simt_lane, INTEGER_CST, t);
    7263            0 :           gimple_seq_add_stmt (dlist, g);
    7264              : 
    7265            0 :           t = build_int_cst (unsigned_type_node, 0);
    7266            0 :           g = gimple_build_assign (sctx.idx, INTEGER_CST, t);
    7267            0 :           gimple_seq_add_stmt (dlist, g);
    7268              : 
    7269            0 :           tree body = create_artificial_label (UNKNOWN_LOCATION);
    7270            0 :           tree header = create_artificial_label (UNKNOWN_LOCATION);
    7271            0 :           tree end = create_artificial_label (UNKNOWN_LOCATION);
    7272            0 :           gimple_seq_add_stmt (dlist, gimple_build_goto (header));
    7273            0 :           gimple_seq_add_stmt (dlist, gimple_build_label (body));
    7274              : 
    7275            0 :           gimple_seq_add_seq (dlist, llist[2]);
    7276              : 
    7277            0 :           g = gimple_build_assign (simt_lane, LSHIFT_EXPR, simt_lane, integer_one_node);
    7278            0 :           gimple_seq_add_stmt (dlist, g);
    7279              : 
    7280            0 :           gimple_seq_add_stmt (dlist, gimple_build_label (header));
    7281            0 :           g = gimple_build_cond (LT_EXPR, simt_lane, simt_vf, body, end);
    7282            0 :           gimple_seq_add_stmt (dlist, g);
    7283              : 
    7284            0 :           gimple_seq_add_stmt (dlist, gimple_build_label (end));
    7285              :         }
    7286        13797 :       for (int i = 0; i < 2; i++)
    7287         9198 :         if (llist[i])
    7288              :           {
    7289         2268 :             tree vf = create_tmp_var (unsigned_type_node);
    7290         2268 :             g = gimple_build_call_internal (IFN_GOMP_SIMD_VF, 1, uid);
    7291         2268 :             gimple_call_set_lhs (g, vf);
    7292         2268 :             gimple_seq *seq = i == 0 ? ilist : dlist;
    7293         2268 :             gimple_seq_add_stmt (seq, g);
    7294         2268 :             tree t = build_int_cst (unsigned_type_node, 0);
    7295         2268 :             g = gimple_build_assign (sctx.idx, INTEGER_CST, t);
    7296         2268 :             gimple_seq_add_stmt (seq, g);
    7297         2268 :             tree body = create_artificial_label (UNKNOWN_LOCATION);
    7298         2268 :             tree header = create_artificial_label (UNKNOWN_LOCATION);
    7299         2268 :             tree end = create_artificial_label (UNKNOWN_LOCATION);
    7300         2268 :             gimple_seq_add_stmt (seq, gimple_build_goto (header));
    7301         2268 :             gimple_seq_add_stmt (seq, gimple_build_label (body));
    7302         2268 :             gimple_seq_add_seq (seq, llist[i]);
    7303         2268 :             t = build_int_cst (unsigned_type_node, 1);
    7304         2268 :             g = gimple_build_assign (sctx.idx, PLUS_EXPR, sctx.idx, t);
    7305         2268 :             gimple_seq_add_stmt (seq, g);
    7306         2268 :             gimple_seq_add_stmt (seq, gimple_build_label (header));
    7307         2268 :             g = gimple_build_cond (LT_EXPR, sctx.idx, vf, body, end);
    7308         2268 :             gimple_seq_add_stmt (seq, g);
    7309         2268 :             gimple_seq_add_stmt (seq, gimple_build_label (end));
    7310              :           }
    7311              :     }
    7312        81459 :   if (sctx.is_simt)
    7313              :     {
    7314            0 :       gimple_seq_add_seq (dlist, sctx.simt_dlist);
    7315            0 :       gimple *g
    7316            0 :         = gimple_build_call_internal (IFN_GOMP_SIMT_EXIT, 1, simtrec);
    7317            0 :       gimple_seq_add_stmt (dlist, g);
    7318              :     }
    7319              : 
    7320              :   /* The copyin sequence is not to be executed by the main thread, since
    7321              :      that would result in self-copies.  Perhaps not visible to scalars,
    7322              :      but it certainly is to C++ operator=.  */
    7323        81459 :   if (copyin_seq)
    7324              :     {
    7325          440 :       x = build_call_expr (builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM),
    7326              :                            0);
    7327          440 :       x = build2 (NE_EXPR, boolean_type_node, x,
    7328          440 :                   build_int_cst (TREE_TYPE (x), 0));
    7329          440 :       x = build3 (COND_EXPR, void_type_node, x, copyin_seq, NULL);
    7330          440 :       gimplify_and_add (x, ilist);
    7331              :     }
    7332              : 
    7333              :   /* If any copyin variable is passed by reference, we must ensure the
    7334              :      master thread doesn't modify it before it is copied over in all
    7335              :      threads.  Similarly for variables in both firstprivate and
    7336              :      lastprivate clauses we need to ensure the lastprivate copying
    7337              :      happens after firstprivate copying in all threads.  And similarly
    7338              :      for UDRs if initializer expression refers to omp_orig.  */
    7339        81459 :   if (copyin_by_ref || lastprivate_firstprivate
    7340        79452 :       || (reduction_omp_orig_ref
    7341          101 :           && !ctx->scan_inclusive
    7342          101 :           && !ctx->scan_exclusive))
    7343              :     {
    7344              :       /* Don't add any barrier for #pragma omp simd or
    7345              :          #pragma omp distribute.  */
    7346         2108 :       if (!is_task_ctx (ctx)
    7347         2108 :           && (gimple_code (ctx->stmt) != GIMPLE_OMP_FOR
    7348         1553 :               || gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_FOR))
    7349              :         {
    7350          929 :           int barrier_kind;
    7351          929 :           switch (gimple_code (ctx->stmt))
    7352              :             {
    7353              :             case GIMPLE_OMP_SECTIONS:
    7354              :             case GIMPLE_OMP_SCOPE:
    7355              :             case GIMPLE_OMP_FOR:
    7356              :               barrier_kind = GOMP_BARRIER_IMPLICIT_WORKSHARE;
    7357              :               break;
    7358          540 :             case GIMPLE_OMP_PARALLEL:
    7359          540 :             case GIMPLE_OMP_TEAMS:
    7360          540 :               barrier_kind = GOMP_BARRIER_IMPLICIT_PARALLEL;
    7361          540 :               break;
    7362            0 :             default:
    7363            0 :               gcc_unreachable ();
    7364              :             }
    7365          929 :           gimple_seq_add_stmt (ilist,
    7366              :                                omp_build_barrier (NULL_TREE, barrier_kind));
    7367              :         }
    7368              :     }
    7369              : 
    7370              :   /* If max_vf is non-zero, then we can use only a vectorization factor
    7371              :      up to the max_vf we chose.  So stick it into the safelen clause.  */
    7372        81459 :   if (maybe_ne (sctx.max_vf, 0U))
    7373              :     {
    7374         5839 :       tree c = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
    7375              :                                 OMP_CLAUSE_SAFELEN);
    7376         5839 :       poly_uint64 safe_len;
    7377         5839 :       if (c == NULL_TREE
    7378         5839 :           || (poly_int_tree_p (OMP_CLAUSE_SAFELEN_EXPR (c), &safe_len)
    7379          656 :               && maybe_gt (safe_len, sctx.max_vf)))
    7380              :         {
    7381         5718 :           c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN);
    7382         5718 :           OMP_CLAUSE_SAFELEN_EXPR (c) = build_int_cst (integer_type_node,
    7383         5718 :                                                        sctx.max_vf);
    7384         5718 :           OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (ctx->stmt);
    7385         5718 :           gimple_omp_for_set_clauses (ctx->stmt, c);
    7386              :         }
    7387              :     }
    7388        81459 : }
    7389              : 
    7390              : /* Create temporary variables for lastprivate(conditional:) implementation
    7391              :    in context CTX with CLAUSES.  */
    7392              : 
    7393              : static void
    7394        49844 : lower_lastprivate_conditional_clauses (tree *clauses, omp_context *ctx)
    7395              : {
    7396        49844 :   tree iter_type = NULL_TREE;
    7397        49844 :   tree cond_ptr = NULL_TREE;
    7398        49844 :   tree iter_var = NULL_TREE;
    7399        49844 :   bool is_simd = (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
    7400        49844 :                   && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD);
    7401        49844 :   tree next = *clauses;
    7402       244123 :   for (tree c = *clauses; c; c = OMP_CLAUSE_CHAIN (c))
    7403       194279 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7404       194279 :         && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
    7405              :       {
    7406          414 :         if (is_simd)
    7407              :           {
    7408          131 :             tree cc = omp_find_clause (next, OMP_CLAUSE__CONDTEMP_);
    7409          131 :             gcc_assert (cc);
    7410          131 :             if (iter_type == NULL_TREE)
    7411              :               {
    7412          101 :                 iter_type = TREE_TYPE (OMP_CLAUSE_DECL (cc));
    7413          101 :                 iter_var = create_tmp_var_raw (iter_type);
    7414          101 :                 DECL_CONTEXT (iter_var) = current_function_decl;
    7415          101 :                 DECL_SEEN_IN_BIND_EXPR_P (iter_var) = 1;
    7416          101 :                 DECL_CHAIN (iter_var) = ctx->block_vars;
    7417          101 :                 ctx->block_vars = iter_var;
    7418          101 :                 tree c3
    7419          101 :                   = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
    7420          101 :                 OMP_CLAUSE__CONDTEMP__ITER (c3) = 1;
    7421          101 :                 OMP_CLAUSE_DECL (c3) = iter_var;
    7422          101 :                 OMP_CLAUSE_CHAIN (c3) = *clauses;
    7423          101 :                 *clauses = c3;
    7424          101 :                 ctx->lastprivate_conditional_map = new hash_map<tree, tree>;
    7425              :               }
    7426          131 :             next = OMP_CLAUSE_CHAIN (cc);
    7427          131 :             tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
    7428          131 :             tree v = lookup_decl (OMP_CLAUSE_DECL (cc), ctx);
    7429          131 :             ctx->lastprivate_conditional_map->put (o, v);
    7430          131 :             continue;
    7431          131 :           }
    7432          283 :         if (iter_type == NULL)
    7433              :           {
    7434          209 :             if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR)
    7435              :               {
    7436          197 :                 struct omp_for_data fd;
    7437          197 :                 omp_extract_for_data (as_a <gomp_for *> (ctx->stmt), &fd,
    7438              :                                       NULL);
    7439          197 :                 iter_type = unsigned_type_for (fd.iter_type);
    7440              :               }
    7441           12 :             else if (gimple_code (ctx->stmt) == GIMPLE_OMP_SECTIONS)
    7442           12 :               iter_type = unsigned_type_node;
    7443          209 :             tree c2 = omp_find_clause (*clauses, OMP_CLAUSE__CONDTEMP_);
    7444          209 :             if (c2)
    7445              :               {
    7446          107 :                 cond_ptr
    7447          107 :                   = lookup_decl_in_outer_ctx (OMP_CLAUSE_DECL (c2), ctx);
    7448          107 :                 OMP_CLAUSE_DECL (c2) = cond_ptr;
    7449              :               }
    7450              :             else
    7451              :               {
    7452          102 :                 cond_ptr = create_tmp_var_raw (build_pointer_type (iter_type));
    7453          102 :                 DECL_CONTEXT (cond_ptr) = current_function_decl;
    7454          102 :                 DECL_SEEN_IN_BIND_EXPR_P (cond_ptr) = 1;
    7455          102 :                 DECL_CHAIN (cond_ptr) = ctx->block_vars;
    7456          102 :                 ctx->block_vars = cond_ptr;
    7457          102 :                 c2 = build_omp_clause (UNKNOWN_LOCATION,
    7458              :                                        OMP_CLAUSE__CONDTEMP_);
    7459          102 :                 OMP_CLAUSE_DECL (c2) = cond_ptr;
    7460          102 :                 OMP_CLAUSE_CHAIN (c2) = *clauses;
    7461          102 :                 *clauses = c2;
    7462              :               }
    7463          209 :             iter_var = create_tmp_var_raw (iter_type);
    7464          209 :             DECL_CONTEXT (iter_var) = current_function_decl;
    7465          209 :             DECL_SEEN_IN_BIND_EXPR_P (iter_var) = 1;
    7466          209 :             DECL_CHAIN (iter_var) = ctx->block_vars;
    7467          209 :             ctx->block_vars = iter_var;
    7468          209 :             tree c3
    7469          209 :               = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
    7470          209 :             OMP_CLAUSE__CONDTEMP__ITER (c3) = 1;
    7471          209 :             OMP_CLAUSE_DECL (c3) = iter_var;
    7472          209 :             OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
    7473          209 :             OMP_CLAUSE_CHAIN (c2) = c3;
    7474          209 :             ctx->lastprivate_conditional_map = new hash_map<tree, tree>;
    7475              :           }
    7476          283 :         tree v = create_tmp_var_raw (iter_type);
    7477          283 :         DECL_CONTEXT (v) = current_function_decl;
    7478          283 :         DECL_SEEN_IN_BIND_EXPR_P (v) = 1;
    7479          283 :         DECL_CHAIN (v) = ctx->block_vars;
    7480          283 :         ctx->block_vars = v;
    7481          283 :         tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
    7482          283 :         ctx->lastprivate_conditional_map->put (o, v);
    7483              :       }
    7484        49844 : }
    7485              : 
    7486              : 
    7487              : /* Generate code to implement the LASTPRIVATE clauses.  This is used for
    7488              :    both parallel and workshare constructs.  PREDICATE may be NULL if it's
    7489              :    always true.  BODY_P is the sequence to insert early initialization
    7490              :    if needed, STMT_LIST is where the non-conditional lastprivate handling
    7491              :    goes into and CSTMT_LIST is a sequence that needs to be run in a critical
    7492              :    section.  */
    7493              : 
    7494              : static void
    7495        49844 : lower_lastprivate_clauses (tree clauses, tree predicate, gimple_seq *body_p,
    7496              :                            gimple_seq *stmt_list, gimple_seq *cstmt_list,
    7497              :                            omp_context *ctx)
    7498              : {
    7499        49844 :   tree x, c, label = NULL, orig_clauses = clauses;
    7500        49844 :   bool par_clauses = false;
    7501        49844 :   tree simduid = NULL, lastlane = NULL, simtcond = NULL, simtlast = NULL;
    7502        49844 :   unsigned HOST_WIDE_INT conditional_off = 0;
    7503        49844 :   gimple_seq post_stmt_list = NULL;
    7504              : 
    7505              :   /* Early exit if there are no lastprivate or linear clauses.  */
    7506       211994 :   for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
    7507       175236 :     if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_LASTPRIVATE
    7508       175236 :         || (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_LINEAR
    7509         6857 :             && !OMP_CLAUSE_LINEAR_NO_COPYOUT (clauses)))
    7510              :       break;
    7511        49844 :   if (clauses == NULL)
    7512              :     {
    7513              :       /* If this was a workshare clause, see if it had been combined
    7514              :          with its parallel.  In that case, look for the clauses on the
    7515              :          parallel statement itself.  */
    7516        36758 :       if (is_parallel_ctx (ctx))
    7517        32632 :         return;
    7518              : 
    7519        36758 :       ctx = ctx->outer;
    7520        36758 :       if (ctx == NULL || !is_parallel_ctx (ctx))
    7521              :         return;
    7522              : 
    7523        12927 :       clauses = omp_find_clause (gimple_omp_parallel_clauses (ctx->stmt),
    7524              :                                  OMP_CLAUSE_LASTPRIVATE);
    7525        12927 :       if (clauses == NULL)
    7526              :         return;
    7527              :       par_clauses = true;
    7528              :     }
    7529              : 
    7530        17212 :   bool maybe_simt = false;
    7531        17212 :   if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
    7532        17212 :       && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
    7533              :     {
    7534         7806 :       maybe_simt = omp_find_clause (orig_clauses, OMP_CLAUSE__SIMT_);
    7535         7806 :       simduid = omp_find_clause (orig_clauses, OMP_CLAUSE__SIMDUID_);
    7536         7806 :       if (simduid)
    7537         3908 :         simduid = OMP_CLAUSE__SIMDUID__DECL (simduid);
    7538              :     }
    7539              : 
    7540        17212 :   if (predicate)
    7541              :     {
    7542        17089 :       gcond *stmt;
    7543        17089 :       tree label_true, arm1, arm2;
    7544        17089 :       enum tree_code pred_code = TREE_CODE (predicate);
    7545              : 
    7546        17089 :       label = create_artificial_label (UNKNOWN_LOCATION);
    7547        17089 :       label_true = create_artificial_label (UNKNOWN_LOCATION);
    7548        17089 :       if (TREE_CODE_CLASS (pred_code) == tcc_comparison)
    7549              :         {
    7550        17089 :           arm1 = TREE_OPERAND (predicate, 0);
    7551        17089 :           arm2 = TREE_OPERAND (predicate, 1);
    7552        17089 :           gimplify_expr (&arm1, stmt_list, NULL, is_gimple_val, fb_rvalue);
    7553        17089 :           gimplify_expr (&arm2, stmt_list, NULL, is_gimple_val, fb_rvalue);
    7554              :         }
    7555              :       else
    7556              :         {
    7557            0 :           arm1 = predicate;
    7558            0 :           gimplify_expr (&arm1, stmt_list, NULL, is_gimple_val, fb_rvalue);
    7559            0 :           arm2 = boolean_false_node;
    7560            0 :           pred_code = NE_EXPR;
    7561              :         }
    7562        17089 :       if (maybe_simt)
    7563              :         {
    7564            0 :           c = build2 (pred_code, boolean_type_node, arm1, arm2);
    7565            0 :           c = fold_convert (integer_type_node, c);
    7566            0 :           simtcond = create_tmp_var (integer_type_node);
    7567            0 :           gimplify_assign (simtcond, c, stmt_list);
    7568            0 :           gcall *g = gimple_build_call_internal (IFN_GOMP_SIMT_VOTE_ANY,
    7569              :                                                  1, simtcond);
    7570            0 :           c = create_tmp_var (integer_type_node);
    7571            0 :           gimple_call_set_lhs (g, c);
    7572            0 :           gimple_seq_add_stmt (stmt_list, g);
    7573            0 :           stmt = gimple_build_cond (NE_EXPR, c, integer_zero_node,
    7574              :                                     label_true, label);
    7575              :         }
    7576              :       else
    7577        17089 :         stmt = gimple_build_cond (pred_code, arm1, arm2, label_true, label);
    7578        17089 :       gimple_seq_add_stmt (stmt_list, stmt);
    7579        17089 :       gimple_seq_add_stmt (stmt_list, gimple_build_label (label_true));
    7580              :     }
    7581              : 
    7582        17212 :   tree cond_ptr = NULL_TREE;
    7583        64994 :   for (c = clauses; c ;)
    7584              :     {
    7585        59559 :       tree var, new_var;
    7586        59559 :       location_t clause_loc = OMP_CLAUSE_LOCATION (c);
    7587        59559 :       gimple_seq *this_stmt_list = stmt_list;
    7588        59559 :       tree lab2 = NULL_TREE;
    7589              : 
    7590        59559 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7591        23344 :           && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
    7592          414 :           && ctx->lastprivate_conditional_map
    7593        59960 :           && !ctx->combined_into_simd_safelen1)
    7594              :         {
    7595          344 :           gcc_assert (body_p);
    7596          344 :           if (simduid)
    7597           61 :             goto next;
    7598          283 :           if (cond_ptr == NULL_TREE)
    7599              :             {
    7600          209 :               cond_ptr = omp_find_clause (orig_clauses, OMP_CLAUSE__CONDTEMP_);
    7601          209 :               cond_ptr = OMP_CLAUSE_DECL (cond_ptr);
    7602              :             }
    7603          283 :           tree type = TREE_TYPE (TREE_TYPE (cond_ptr));
    7604          283 :           tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
    7605          283 :           tree v = *ctx->lastprivate_conditional_map->get (o);
    7606          283 :           gimplify_assign (v, build_zero_cst (type), body_p);
    7607          283 :           this_stmt_list = cstmt_list;
    7608          283 :           tree mem;
    7609          283 :           if (POINTER_TYPE_P (TREE_TYPE (cond_ptr)))
    7610              :             {
    7611          136 :               mem = build2 (MEM_REF, type, cond_ptr,
    7612          136 :                             build_int_cst (TREE_TYPE (cond_ptr),
    7613          136 :                                            conditional_off));
    7614          136 :               conditional_off += tree_to_uhwi (TYPE_SIZE_UNIT (type));
    7615              :             }
    7616              :           else
    7617          147 :             mem = build4 (ARRAY_REF, type, cond_ptr,
    7618          147 :                           size_int (conditional_off++), NULL_TREE, NULL_TREE);
    7619          283 :           tree mem2 = copy_node (mem);
    7620          283 :           gimple_seq seq = NULL;
    7621          283 :           mem = force_gimple_operand (mem, &seq, true, NULL_TREE);
    7622          283 :           gimple_seq_add_seq (this_stmt_list, seq);
    7623          283 :           tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
    7624          283 :           lab2 = create_artificial_label (UNKNOWN_LOCATION);
    7625          283 :           gimple *g = gimple_build_cond (GT_EXPR, v, mem, lab1, lab2);
    7626          283 :           gimple_seq_add_stmt (this_stmt_list, g);
    7627          283 :           gimple_seq_add_stmt (this_stmt_list, gimple_build_label (lab1));
    7628          283 :           gimplify_assign (mem2, v, this_stmt_list);
    7629              :         }
    7630        59215 :       else if (predicate
    7631        58969 :                && ctx->combined_into_simd_safelen1
    7632          116 :                && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7633           57 :                && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
    7634        59272 :                && ctx->lastprivate_conditional_map)
    7635              :         this_stmt_list = &post_stmt_list;
    7636              : 
    7637        59498 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7638        59498 :           || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
    7639         4913 :               && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
    7640              :         {
    7641        28196 :           var = OMP_CLAUSE_DECL (c);
    7642        28196 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7643        23283 :               && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
    7644        29227 :               && is_taskloop_ctx (ctx))
    7645              :             {
    7646          312 :               gcc_checking_assert (ctx->outer && is_task_ctx (ctx->outer));
    7647          312 :               new_var = lookup_decl (var, ctx->outer);
    7648              :             }
    7649              :           else
    7650              :             {
    7651        27884 :               new_var = lookup_decl (var, ctx);
    7652              :               /* Avoid uninitialized warnings for lastprivate and
    7653              :                  for linear iterators.  */
    7654        27884 :               if (predicate
    7655        55575 :                   && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7656         4913 :                       || OMP_CLAUSE_LINEAR_NO_COPYIN (c)))
    7657        26416 :                 suppress_warning (new_var, OPT_Wuninitialized);
    7658              :             }
    7659              : 
    7660        28196 :           if (!maybe_simt && simduid && DECL_HAS_VALUE_EXPR_P (new_var))
    7661              :             {
    7662         6637 :               tree val = DECL_VALUE_EXPR (new_var);
    7663         6637 :               if (TREE_CODE (val) == ARRAY_REF
    7664         6558 :                   && VAR_P (TREE_OPERAND (val, 0))
    7665        13195 :                   && lookup_attribute ("omp simd array",
    7666         6558 :                                        DECL_ATTRIBUTES (TREE_OPERAND (val,
    7667              :                                                                       0))))
    7668              :                 {
    7669         6558 :                   if (lastlane == NULL)
    7670              :                     {
    7671         3094 :                       lastlane = create_tmp_var (unsigned_type_node);
    7672         3094 :                       gcall *g
    7673         3094 :                         = gimple_build_call_internal (IFN_GOMP_SIMD_LAST_LANE,
    7674              :                                                       2, simduid,
    7675         3094 :                                                       TREE_OPERAND (val, 1));
    7676         3094 :                       gimple_call_set_lhs (g, lastlane);
    7677         3094 :                       gimple_seq_add_stmt (this_stmt_list, g);
    7678              :                     }
    7679         6558 :                   new_var = build4 (ARRAY_REF, TREE_TYPE (val),
    7680         6558 :                                     TREE_OPERAND (val, 0), lastlane,
    7681              :                                     NULL_TREE, NULL_TREE);
    7682         6558 :                   TREE_THIS_NOTRAP (new_var) = 1;
    7683              :                 }
    7684              :             }
    7685        21559 :           else if (maybe_simt)
    7686              :             {
    7687            0 :               int *k = ctx->lastprivate_iv_map
    7688            0 :                          ? ctx->lastprivate_iv_map->get (var)
    7689              :                          : NULL;
    7690            0 :               if (k == NULL)
    7691              :                 {
    7692            0 :                   tree val = (DECL_HAS_VALUE_EXPR_P (new_var)
    7693            0 :                                 ? DECL_VALUE_EXPR (new_var)
    7694              :                                 : new_var);
    7695            0 :                   bool is_addr = TREE_CODE (val) == ADDR_EXPR;
    7696            0 :                   if (is_addr)
    7697            0 :                     val = TREE_OPERAND (val, 0);
    7698            0 :                   if (simtlast == NULL)
    7699              :                     {
    7700            0 :                       simtlast = create_tmp_var (unsigned_type_node);
    7701            0 :                       gcall *g
    7702            0 :                         = gimple_build_call_internal (IFN_GOMP_SIMT_LAST_LANE,
    7703              :                                                       1, simtcond);
    7704            0 :                       gimple_call_set_lhs (g, simtlast);
    7705            0 :                       gimple_seq_add_stmt (this_stmt_list, g);
    7706              :                     }
    7707            0 :                   x = build_call_expr_internal_loc (UNKNOWN_LOCATION,
    7708              :                                                     IFN_GOMP_SIMT_XCHG_IDX,
    7709            0 :                                                     TREE_TYPE (val), 2, val,
    7710              :                                                     simtlast);
    7711            0 :                   gimplify_assign (is_addr ? val : new_var, x, this_stmt_list);
    7712              :                 }
    7713            0 :               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
    7714              :                 {
    7715            0 :                   OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) = NULL;
    7716            0 :                   gimplify_assign (new_var,
    7717            0 :                                    gimple_omp_for_index (ctx->stmt, *k),
    7718            0 :                                    &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
    7719              :                 }
    7720              :             }
    7721              : 
    7722        28196 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7723        28196 :               && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c))
    7724              :             {
    7725         7927 :               lower_omp (&OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c), ctx);
    7726         7927 :               gimple_seq_add_seq (this_stmt_list,
    7727         7927 :                                   OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
    7728         7927 :               OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) = NULL;
    7729              :             }
    7730        20269 :           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
    7731        20269 :                    && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c))
    7732              :             {
    7733          440 :               lower_omp (&OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c), ctx);
    7734          440 :               gimple_seq_add_seq (this_stmt_list,
    7735          440 :                                   OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
    7736          440 :               OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) = NULL;
    7737              :             }
    7738              : 
    7739        28196 :           x = NULL_TREE;
    7740        28196 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    7741        23283 :               && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
    7742        28256 :               && is_taskloop_ctx (ctx))
    7743              :             {
    7744           76 :               tree ovar = maybe_lookup_decl_in_outer_ctx (var,
    7745           38 :                                                           ctx->outer->outer);
    7746           38 :               if (is_global_var (ovar))
    7747            7 :                 x = ovar;
    7748              :             }
    7749            7 :           if (!x)
    7750        28189 :             x = build_outer_var_ref (var, ctx, OMP_CLAUSE_LASTPRIVATE);
    7751        28196 :           if (omp_privatize_by_reference (var))
    7752          724 :             new_var = build_simple_mem_ref_loc (clause_loc, new_var);
    7753        28196 :           x = lang_hooks.decls.omp_clause_assign_op (c, x, new_var);
    7754        28196 :           gimplify_and_add (x, this_stmt_list);
    7755              : 
    7756        28196 :           if (lab2)
    7757          283 :             gimple_seq_add_stmt (this_stmt_list, gimple_build_label (lab2));
    7758              :         }
    7759              : 
    7760        59559 :      next:
    7761        59559 :       c = OMP_CLAUSE_CHAIN (c);
    7762        59559 :       if (c == NULL && !par_clauses)
    7763              :         {
    7764              :           /* If this was a workshare clause, see if it had been combined
    7765              :              with its parallel.  In that case, continue looking for the
    7766              :              clauses also on the parallel statement itself.  */
    7767        13086 :           if (is_parallel_ctx (ctx))
    7768              :             break;
    7769              : 
    7770        13086 :           ctx = ctx->outer;
    7771        13086 :           if (ctx == NULL || !is_parallel_ctx (ctx))
    7772              :             break;
    7773              : 
    7774         1309 :           c = omp_find_clause (gimple_omp_parallel_clauses (ctx->stmt),
    7775              :                                OMP_CLAUSE_LASTPRIVATE);
    7776         1309 :           par_clauses = true;
    7777              :         }
    7778              :     }
    7779              : 
    7780        17212 :   if (label)
    7781        17089 :     gimple_seq_add_stmt (stmt_list, gimple_build_label (label));
    7782        17212 :   gimple_seq_add_seq (stmt_list, post_stmt_list);
    7783              : }
    7784              : 
    7785              : /* Lower the OpenACC reductions of CLAUSES for compute axis LEVEL
    7786              :    (which might be a placeholder).  INNER is true if this is an inner
    7787              :    axis of a multi-axis loop.  FORK and JOIN are (optional) fork and
    7788              :    join markers.  Generate the before-loop forking sequence in
    7789              :    FORK_SEQ and the after-loop joining sequence to JOIN_SEQ.  The
    7790              :    general form of these sequences is
    7791              : 
    7792              :      GOACC_REDUCTION_SETUP
    7793              :      GOACC_FORK
    7794              :      GOACC_REDUCTION_INIT
    7795              :      ...
    7796              :      GOACC_REDUCTION_FINI
    7797              :      GOACC_JOIN
    7798              :      GOACC_REDUCTION_TEARDOWN.  */
    7799              : 
    7800              : static void
    7801        25946 : lower_oacc_reductions (location_t loc, tree clauses, tree level, bool inner,
    7802              :                        gcall *fork, gcall *private_marker, gcall *join,
    7803              :                        gimple_seq *fork_seq, gimple_seq *join_seq,
    7804              :                        omp_context *ctx)
    7805              : {
    7806        25946 :   gimple_seq before_fork = NULL;
    7807        25946 :   gimple_seq after_fork = NULL;
    7808        25946 :   gimple_seq before_join = NULL;
    7809        25946 :   gimple_seq after_join = NULL;
    7810        25946 :   tree init_code = NULL_TREE, fini_code = NULL_TREE,
    7811        25946 :     setup_code = NULL_TREE, teardown_code = NULL_TREE;
    7812        25946 :   unsigned offset = 0;
    7813              : 
    7814        96516 :   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    7815        70570 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
    7816              :       {
    7817              :         /* No 'reduction' clauses on OpenACC 'kernels'.  */
    7818         7751 :         gcc_checking_assert (!is_oacc_kernels (ctx));
    7819              :         /* Likewise, on OpenACC 'kernels' decomposed parts.  */
    7820         7751 :         gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
    7821              : 
    7822         7751 :         tree orig = OMP_CLAUSE_DECL (c);
    7823         7751 :         tree orig_clause;
    7824         7751 :         tree var = maybe_lookup_decl (orig, ctx);
    7825         7751 :         tree ref_to_res = NULL_TREE;
    7826         7751 :         tree incoming, outgoing, v1, v2, v3;
    7827         7751 :         bool is_private = false;
    7828              : 
    7829         7751 :         enum tree_code rcode = OMP_CLAUSE_REDUCTION_CODE (c);
    7830         7751 :         if (rcode == MINUS_EXPR)
    7831              :           rcode = PLUS_EXPR;
    7832         6609 :         else if (rcode == TRUTH_ANDIF_EXPR)
    7833              :           rcode = BIT_AND_EXPR;
    7834         6431 :         else if (rcode == TRUTH_ORIF_EXPR)
    7835          433 :           rcode = BIT_IOR_EXPR;
    7836         7751 :         tree op = build_int_cst (unsigned_type_node, rcode);
    7837              : 
    7838         7751 :         if (!var)
    7839            4 :           var = orig;
    7840              : 
    7841         7751 :         incoming = outgoing = var;
    7842              : 
    7843         7751 :         if (!inner)
    7844              :           {
    7845              :             /* See if an outer construct also reduces this variable.  */
    7846              :             omp_context *outer = ctx;
    7847              : 
    7848         7385 :             while (omp_context *probe = outer->outer)
    7849              :               {
    7850         4708 :                 enum gimple_code type = gimple_code (probe->stmt);
    7851         4708 :                 tree cls;
    7852              : 
    7853         4708 :                 switch (type)
    7854              :                   {
    7855         2220 :                   case GIMPLE_OMP_FOR:
    7856         2220 :                     cls = gimple_omp_for_clauses (probe->stmt);
    7857         2220 :                     break;
    7858              : 
    7859         2488 :                   case GIMPLE_OMP_TARGET:
    7860              :                     /* No 'reduction' clauses inside OpenACC 'kernels'
    7861              :                        regions.  */
    7862         2488 :                     gcc_checking_assert (!is_oacc_kernels (probe));
    7863              : 
    7864         2488 :                     if (!is_gimple_omp_offloaded (probe->stmt))
    7865           26 :                       goto do_lookup;
    7866              : 
    7867         2462 :                     cls = gimple_omp_target_clauses (probe->stmt);
    7868         2462 :                     break;
    7869              : 
    7870            0 :                   default:
    7871            0 :                     goto do_lookup;
    7872              :                   }
    7873              : 
    7874         4682 :                 outer = probe;
    7875        14249 :                 for (; cls;  cls = OMP_CLAUSE_CHAIN (cls))
    7876        11632 :                   if (OMP_CLAUSE_CODE (cls) == OMP_CLAUSE_REDUCTION
    7877        11632 :                       && orig == OMP_CLAUSE_DECL (cls))
    7878              :                     {
    7879         1772 :                       incoming = outgoing = lookup_decl (orig, probe);
    7880         1772 :                       goto has_outer_reduction;
    7881              :                     }
    7882         9860 :                   else if ((OMP_CLAUSE_CODE (cls) == OMP_CLAUSE_FIRSTPRIVATE
    7883         8971 :                             || OMP_CLAUSE_CODE (cls) == OMP_CLAUSE_PRIVATE)
    7884        11164 :                            && orig == OMP_CLAUSE_DECL (cls))
    7885              :                     {
    7886          293 :                       is_private = true;
    7887          293 :                       goto do_lookup;
    7888              :                     }
    7889              :               }
    7890              : 
    7891         2677 :           do_lookup:
    7892              :             /* This is the outermost construct with this reduction,
    7893              :                see if there's a mapping for it.  */
    7894         2996 :             orig_clause = NULL_TREE;
    7895         2996 :             if (gimple_code (outer->stmt) == GIMPLE_OMP_TARGET)
    7896         7557 :               for (tree cls = gimple_omp_target_clauses (outer->stmt);
    7897         7557 :                    cls; cls = OMP_CLAUSE_CHAIN (cls))
    7898         7250 :                 if (OMP_CLAUSE_CODE (cls) == OMP_CLAUSE_MAP
    7899         4823 :                     && orig == OMP_CLAUSE_DECL (cls)
    7900         9805 :                     && maybe_lookup_field (cls, outer))
    7901              :                   {
    7902              :                     orig_clause = cls;
    7903              :                     break;
    7904              :                   }
    7905         2996 :             if (orig_clause != NULL_TREE && !is_private)
    7906              :               {
    7907         2553 :                 ref_to_res = build_receiver_ref (orig_clause, false, outer);
    7908         2553 :                 if (omp_privatize_by_reference (orig))
    7909           79 :                   ref_to_res = build_simple_mem_ref (ref_to_res);
    7910              : 
    7911         2553 :                 tree type = TREE_TYPE (var);
    7912         2553 :                 if (POINTER_TYPE_P (type))
    7913           79 :                   type = TREE_TYPE (type);
    7914              : 
    7915         2553 :                 outgoing = var;
    7916         2553 :                 incoming = omp_reduction_init_op (loc, rcode, type);
    7917              :               }
    7918              :             else
    7919              :               {
    7920              :                 /* Try to look at enclosing contexts for reduction var,
    7921              :                    use original if no mapping found.  */
    7922          443 :                 tree t = NULL_TREE;
    7923          443 :                 omp_context *c = ctx->outer;
    7924          895 :                 while (c && !t)
    7925              :                   {
    7926          452 :                     t = maybe_lookup_decl (orig, c);
    7927          452 :                     c = c->outer;
    7928              :                   }
    7929          443 :                 incoming = outgoing = (t ? t : orig);
    7930              :               }
    7931              : 
    7932         2553 :           has_outer_reduction:;
    7933              :           }
    7934              : 
    7935         4325 :         if (!ref_to_res)
    7936         5198 :           ref_to_res = integer_zero_node;
    7937              : 
    7938         7751 :         if (omp_privatize_by_reference (orig))
    7939              :           {
    7940          159 :             tree type = TREE_TYPE (var);
    7941          159 :             const char *id = IDENTIFIER_POINTER (DECL_NAME (var));
    7942              : 
    7943          159 :             if (!inner)
    7944              :               {
    7945          110 :                 tree x = create_tmp_var (TREE_TYPE (type), id);
    7946          110 :                 gimplify_assign (var, build_fold_addr_expr (x), fork_seq);
    7947              :               }
    7948              : 
    7949          159 :             v1 = create_tmp_var (type, id);
    7950          159 :             v2 = create_tmp_var (type, id);
    7951          159 :             v3 = create_tmp_var (type, id);
    7952              : 
    7953          159 :             gimplify_assign (v1, var, fork_seq);
    7954          159 :             gimplify_assign (v2, var, fork_seq);
    7955          159 :             gimplify_assign (v3, var, fork_seq);
    7956              : 
    7957          159 :             var = build_simple_mem_ref (var);
    7958          159 :             v1 = build_simple_mem_ref (v1);
    7959          159 :             v2 = build_simple_mem_ref (v2);
    7960          159 :             v3 = build_simple_mem_ref (v3);
    7961          159 :             outgoing = build_simple_mem_ref (outgoing);
    7962              : 
    7963          159 :             if (!TREE_CONSTANT (incoming))
    7964           80 :               incoming = build_simple_mem_ref (incoming);
    7965              :           }
    7966              :         else
    7967              :           /* Note that 'var' might be a mem ref.  */
    7968              :           v1 = v2 = v3 = var;
    7969              : 
    7970              :         /* Determine position in reduction buffer, which may be used
    7971              :            by target.  The parser has ensured that this is not a
    7972              :            variable-sized type.  */
    7973         7751 :         fixed_size_mode mode
    7974         7751 :           = as_a <fixed_size_mode> (TYPE_MODE (TREE_TYPE (var)));
    7975         7751 :         unsigned align = GET_MODE_ALIGNMENT (mode) /  BITS_PER_UNIT;
    7976         7751 :         offset = (offset + align - 1) & ~(align - 1);
    7977         7751 :         tree off = build_int_cst (sizetype, offset);
    7978         7751 :         offset += GET_MODE_SIZE (mode);
    7979              : 
    7980         7751 :         if (!init_code)
    7981              :           {
    7982         6969 :             init_code = build_int_cst (integer_type_node,
    7983              :                                        IFN_GOACC_REDUCTION_INIT);
    7984         6969 :             fini_code = build_int_cst (integer_type_node,
    7985              :                                        IFN_GOACC_REDUCTION_FINI);
    7986         6969 :             setup_code = build_int_cst (integer_type_node,
    7987              :                                         IFN_GOACC_REDUCTION_SETUP);
    7988         6969 :             teardown_code = build_int_cst (integer_type_node,
    7989              :                                            IFN_GOACC_REDUCTION_TEARDOWN);
    7990              :           }
    7991              : 
    7992         7751 :         tree setup_call
    7993        15502 :           = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
    7994         7751 :                                           TREE_TYPE (var), 6, setup_code,
    7995              :                                           unshare_expr (ref_to_res),
    7996              :                                           unshare_expr (incoming),
    7997              :                                           level, op, off);
    7998         7751 :         tree init_call
    7999        15502 :           = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
    8000         7751 :                                           TREE_TYPE (var), 6, init_code,
    8001              :                                           unshare_expr (ref_to_res),
    8002              :                                           unshare_expr (v1), level, op, off);
    8003         7751 :         tree fini_call
    8004        15502 :           = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
    8005         7751 :                                           TREE_TYPE (var), 6, fini_code,
    8006              :                                           unshare_expr (ref_to_res),
    8007              :                                           unshare_expr (v2), level, op, off);
    8008         7751 :         tree teardown_call
    8009        15502 :           = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
    8010         7751 :                                           TREE_TYPE (var), 6, teardown_code,
    8011              :                                           ref_to_res, unshare_expr (v3),
    8012              :                                           level, op, off);
    8013              : 
    8014         7751 :         gimplify_assign (unshare_expr (v1), setup_call, &before_fork);
    8015         7751 :         gimplify_assign (unshare_expr (v2), init_call, &after_fork);
    8016         7751 :         gimplify_assign (unshare_expr (v3), fini_call, &before_join);
    8017         7751 :         gimplify_assign (unshare_expr (outgoing), teardown_call, &after_join);
    8018              :       }
    8019              : 
    8020              :   /* Now stitch things together.  */
    8021        25946 :   gimple_seq_add_seq (fork_seq, before_fork);
    8022        25946 :   if (private_marker)
    8023          259 :     gimple_seq_add_stmt (fork_seq, private_marker);
    8024        25946 :   if (fork)
    8025        16551 :     gimple_seq_add_stmt (fork_seq, fork);
    8026        25946 :   gimple_seq_add_seq (fork_seq, after_fork);
    8027              : 
    8028        25946 :   gimple_seq_add_seq (join_seq, before_join);
    8029        25946 :   if (join)
    8030        16551 :     gimple_seq_add_stmt (join_seq, join);
    8031        25946 :   gimple_seq_add_seq (join_seq, after_join);
    8032        25946 : }
    8033              : 
    8034              : /* Generate code to implement the REDUCTION clauses, append it
    8035              :    to STMT_SEQP.  CLIST if non-NULL is a pointer to a sequence
    8036              :    that should be emitted also inside of the critical section,
    8037              :    in that case clear *CLIST afterwards, otherwise leave it as is
    8038              :    and let the caller emit it itself.  */
    8039              : 
    8040              : static void
    8041        76155 : lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp,
    8042              :                          gimple_seq *clist, omp_context *ctx)
    8043              : {
    8044        76155 :   gimple_seq sub_seq = NULL;
    8045        76155 :   gimple *stmt;
    8046        76155 :   tree x, c;
    8047        76155 :   int count = 0;
    8048              : 
    8049              :   /* OpenACC loop reductions are handled elsewhere.  */
    8050        76155 :   if (is_gimple_omp_oacc (ctx->stmt))
    8051        74997 :     return;
    8052              : 
    8053              :   /* SIMD reductions are handled in lower_rec_input_clauses.  */
    8054        64830 :   if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
    8055        64830 :       && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
    8056              :     return;
    8057              : 
    8058              :   /* inscan reductions are handled elsewhere.  */
    8059        54402 :   if (ctx->scan_inclusive || ctx->scan_exclusive)
    8060              :     return;
    8061              : 
    8062              :   /* First see if there is exactly one reduction clause.  Use OMP_ATOMIC
    8063              :      update in that case, otherwise use a lock.  */
    8064       257414 :   for (c = clauses; c && count < 2; c = OMP_CLAUSE_CHAIN (c))
    8065       204152 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
    8066       204152 :         && !OMP_CLAUSE_REDUCTION_TASK (c))
    8067              :       {
    8068         5173 :         if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
    8069         5173 :             || TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
    8070              :           {
    8071              :             /* Never use OMP_ATOMIC for array reductions or UDRs.  */
    8072              :             count = -1;
    8073              :             break;
    8074              :           }
    8075         4220 :         count++;
    8076              :       }
    8077              : 
    8078        54215 :   if (count == 0)
    8079              :     return;
    8080              : 
    8081        16312 :   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
    8082              :     {
    8083        15154 :       tree var, ref, new_var, orig_var;
    8084        15154 :       enum tree_code code;
    8085        15154 :       location_t clause_loc = OMP_CLAUSE_LOCATION (c);
    8086              : 
    8087        24323 :       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
    8088        15154 :           || OMP_CLAUSE_REDUCTION_TASK (c))
    8089         9169 :         continue;
    8090              : 
    8091         5985 :       enum omp_clause_code ccode = OMP_CLAUSE_REDUCTION;
    8092         5985 :       orig_var = var = OMP_CLAUSE_DECL (c);
    8093         5985 :       if (TREE_CODE (var) == MEM_REF)
    8094              :         {
    8095          772 :           var = TREE_OPERAND (var, 0);
    8096          772 :           if (TREE_CODE (var) == POINTER_PLUS_EXPR)
    8097           50 :             var = TREE_OPERAND (var, 0);
    8098          772 :           if (TREE_CODE (var) == ADDR_EXPR)
    8099          330 :             var = TREE_OPERAND (var, 0);
    8100              :           else
    8101              :             {
    8102              :               /* If this is a pointer or referenced based array
    8103              :                  section, the var could be private in the outer
    8104              :                  context e.g. on orphaned loop construct.  Pretend this
    8105              :                  is private variable's outer reference.  */
    8106          442 :               ccode = OMP_CLAUSE_PRIVATE;
    8107          442 :               if (INDIRECT_REF_P (var))
    8108           72 :                 var = TREE_OPERAND (var, 0);
    8109              :             }
    8110          772 :           orig_var = var;
    8111          772 :           if (is_variable_sized (var))
    8112              :             {
    8113           25 :               gcc_assert (DECL_HAS_VALUE_EXPR_P (var));
    8114           25 :               var = DECL_VALUE_EXPR (var);
    8115           25 :               gcc_assert (INDIRECT_REF_P (var));
    8116           25 :               var = TREE_OPERAND (var, 0);
    8117           25 :               gcc_assert (DECL_P (var));
    8118              :             }
    8119              :         }
    8120         5985 :       new_var = lookup_decl (var, ctx);
    8121         5985 :       if (var == OMP_CLAUSE_DECL (c)
    8122         5985 :           && omp_privatize_by_reference (var))
    8123          161 :         new_var = build_simple_mem_ref_loc (clause_loc, new_var);
    8124         5985 :       ref = build_outer_var_ref (var, ctx, ccode);
    8125         5985 :       code = OMP_CLAUSE_REDUCTION_CODE (c);
    8126              : 
    8127              :       /* reduction(-:var) sums up the partial results, so it acts
    8128              :          identically to reduction(+:var).  */
    8129         5985 :       if (code == MINUS_EXPR)
    8130           97 :         code = PLUS_EXPR;
    8131              : 
    8132         5985 :       bool is_truth_op = (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR);
    8133         5985 :       if (count == 1)
    8134              :         {
    8135         3755 :           tree addr = build_fold_addr_expr_loc (clause_loc, ref);
    8136              : 
    8137         3755 :           addr = save_expr (addr);
    8138         3755 :           ref = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (addr)), addr);
    8139         3755 :           tree new_var2 = new_var;
    8140         3755 :           tree ref2 = ref;
    8141         3755 :           if (is_truth_op)
    8142              :             {
    8143          864 :               tree zero = build_zero_cst (TREE_TYPE (new_var));
    8144          864 :               new_var2 = fold_build2_loc (clause_loc, NE_EXPR,
    8145              :                                           boolean_type_node, new_var, zero);
    8146          864 :               ref2 = fold_build2_loc (clause_loc, NE_EXPR, boolean_type_node,
    8147              :                                       ref, zero);
    8148              :             }
    8149         3755 :           x = fold_build2_loc (clause_loc, code, TREE_TYPE (new_var2), ref2,
    8150              :                                new_var2);
    8151         3755 :           if (is_truth_op)
    8152          864 :             x = fold_convert (TREE_TYPE (new_var), x);
    8153         3755 :           x = build2 (OMP_ATOMIC, void_type_node, addr, x);
    8154         3755 :           OMP_ATOMIC_MEMORY_ORDER (x) = OMP_MEMORY_ORDER_RELAXED;
    8155         3755 :           gimplify_and_add (x, stmt_seqp);
    8156         3755 :           return;
    8157              :         }
    8158         2230 :       else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
    8159              :         {
    8160          772 :           tree d = OMP_CLAUSE_DECL (c);
    8161          772 :           tree type = TREE_TYPE (d);
    8162          772 :           tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    8163          772 :           tree i = create_tmp_var (TREE_TYPE (v));
    8164          772 :           tree ptype = build_pointer_type (TREE_TYPE (type));
    8165          772 :           tree bias = TREE_OPERAND (d, 1);
    8166          772 :           d = TREE_OPERAND (d, 0);
    8167          772 :           if (TREE_CODE (d) == POINTER_PLUS_EXPR)
    8168              :             {
    8169           50 :               tree b = TREE_OPERAND (d, 1);
    8170           50 :               b = maybe_lookup_decl (b, ctx);
    8171           50 :               if (b == NULL)
    8172              :                 {
    8173            2 :                   b = TREE_OPERAND (d, 1);
    8174            2 :                   b = maybe_lookup_decl_in_outer_ctx (b, ctx);
    8175              :                 }
    8176           50 :               if (integer_zerop (bias))
    8177              :                 bias = b;
    8178              :               else
    8179              :                 {
    8180            0 :                   bias = fold_convert_loc (clause_loc, TREE_TYPE (b), bias);
    8181            0 :                   bias = fold_build2_loc (clause_loc, PLUS_EXPR,
    8182            0 :                                           TREE_TYPE (b), b, bias);
    8183              :                 }
    8184           50 :               d = TREE_OPERAND (d, 0);
    8185              :             }
    8186              :           /* For ref build_outer_var_ref already performs this, so
    8187              :              only new_var needs a dereference.  */
    8188          772 :           if (INDIRECT_REF_P (d))
    8189              :             {
    8190           72 :               new_var = build_simple_mem_ref_loc (clause_loc, new_var);
    8191           72 :               gcc_assert (omp_privatize_by_reference (var)
    8192              :                           && var == orig_var);
    8193              :             }
    8194          700 :           else if (TREE_CODE (d) == ADDR_EXPR)
    8195              :             {
    8196          330 :               if (orig_var == var)
    8197              :                 {
    8198          305 :                   new_var = build_fold_addr_expr (new_var);
    8199          305 :                   ref = build_fold_addr_expr (ref);
    8200              :                 }
    8201              :             }
    8202              :           else
    8203              :             {
    8204          370 :               gcc_assert (orig_var == var);
    8205          370 :               if (omp_privatize_by_reference (var))
    8206          106 :                 ref = build_fold_addr_expr (ref);
    8207              :             }
    8208          772 :           if (DECL_P (v))
    8209              :             {
    8210           93 :               tree t = maybe_lookup_decl (v, ctx);
    8211           93 :               if (t)
    8212              :                 v = t;
    8213              :               else
    8214            5 :                 v = maybe_lookup_decl_in_outer_ctx (v, ctx);
    8215           93 :               gimplify_expr (&v, stmt_seqp, NULL, is_gimple_val, fb_rvalue);
    8216              :             }
    8217          772 :           if (!integer_zerop (bias))
    8218              :             {
    8219          432 :               bias = fold_convert_loc (clause_loc, sizetype, bias);
    8220          864 :               new_var = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
    8221          432 :                                          TREE_TYPE (new_var), new_var,
    8222              :                                          unshare_expr (bias));
    8223          432 :               ref = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
    8224          432 :                                          TREE_TYPE (ref), ref, bias);
    8225              :             }
    8226          772 :           new_var = fold_convert_loc (clause_loc, ptype, new_var);
    8227          772 :           ref = fold_convert_loc (clause_loc, ptype, ref);
    8228          772 :           tree m = create_tmp_var (ptype);
    8229          772 :           gimplify_assign (m, new_var, stmt_seqp);
    8230          772 :           new_var = m;
    8231          772 :           m = create_tmp_var (ptype);
    8232          772 :           gimplify_assign (m, ref, stmt_seqp);
    8233          772 :           ref = m;
    8234          772 :           gimplify_assign (i, build_int_cst (TREE_TYPE (v), 0), stmt_seqp);
    8235          772 :           tree body = create_artificial_label (UNKNOWN_LOCATION);
    8236          772 :           tree end = create_artificial_label (UNKNOWN_LOCATION);
    8237          772 :           gimple_seq_add_stmt (&sub_seq, gimple_build_label (body));
    8238          772 :           tree priv = build_simple_mem_ref_loc (clause_loc, new_var);
    8239          772 :           tree out = build_simple_mem_ref_loc (clause_loc, ref);
    8240          772 :           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    8241              :             {
    8242           72 :               tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
    8243           72 :               tree decl_placeholder
    8244           72 :                 = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c);
    8245           72 :               SET_DECL_VALUE_EXPR (placeholder, out);
    8246           72 :               DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
    8247           72 :               SET_DECL_VALUE_EXPR (decl_placeholder, priv);
    8248           72 :               DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 1;
    8249           72 :               lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
    8250           72 :               gimple_seq_add_seq (&sub_seq,
    8251           72 :                                   OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
    8252           72 :               OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
    8253           72 :               OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
    8254           72 :               OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = NULL;
    8255              :             }
    8256              :           else
    8257              :             {
    8258          700 :               tree out2 = out;
    8259          700 :               tree priv2 = priv;
    8260          700 :               if (is_truth_op)
    8261              :                 {
    8262            0 :                   tree zero = build_zero_cst (TREE_TYPE (out));
    8263            0 :                   out2 = fold_build2_loc (clause_loc, NE_EXPR,
    8264              :                                           boolean_type_node, out, zero);
    8265            0 :                   priv2 = fold_build2_loc (clause_loc, NE_EXPR,
    8266              :                                            boolean_type_node, priv, zero);
    8267              :                 }
    8268          700 :               x = build2 (code, TREE_TYPE (out2), out2, priv2);
    8269          700 :               if (is_truth_op)
    8270            0 :                 x = fold_convert (TREE_TYPE (out), x);
    8271          700 :               out = unshare_expr (out);
    8272          700 :               gimplify_assign (out, x, &sub_seq);
    8273              :             }
    8274          772 :           gimple *g = gimple_build_assign (new_var, POINTER_PLUS_EXPR, new_var,
    8275          772 :                                            TYPE_SIZE_UNIT (TREE_TYPE (type)));
    8276          772 :           gimple_seq_add_stmt (&sub_seq, g);
    8277          772 :           g = gimple_build_assign (ref, POINTER_PLUS_EXPR, ref,
    8278          772 :                                    TYPE_SIZE_UNIT (TREE_TYPE (type)));
    8279          772 :           gimple_seq_add_stmt (&sub_seq, g);
    8280          772 :           g = gimple_build_assign (i, PLUS_EXPR, i,
    8281          772 :                                    build_int_cst (TREE_TYPE (i), 1));
    8282          772 :           gimple_seq_add_stmt (&sub_seq, g);
    8283          772 :           g = gimple_build_cond (LE_EXPR, i, v, body, end);
    8284          772 :           gimple_seq_add_stmt (&sub_seq, g);
    8285          772 :           gimple_seq_add_stmt (&sub_seq, gimple_build_label (end));
    8286              :         }
    8287         1458 :       else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    8288              :         {
    8289          796 :           tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
    8290              : 
    8291          796 :           if (omp_privatize_by_reference (var)
    8292          853 :               && !useless_type_conversion_p (TREE_TYPE (placeholder),
    8293           57 :                                              TREE_TYPE (ref)))
    8294           49 :             ref = build_fold_addr_expr_loc (clause_loc, ref);
    8295          796 :           SET_DECL_VALUE_EXPR (placeholder, ref);
    8296          796 :           DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
    8297          796 :           lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
    8298          796 :           gimple_seq_add_seq (&sub_seq, OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
    8299          796 :           OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
    8300          796 :           OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
    8301              :         }
    8302              :       else
    8303              :         {
    8304          662 :           tree new_var2 = new_var;
    8305          662 :           tree ref2 = ref;
    8306          662 :           if (is_truth_op)
    8307              :             {
    8308           78 :               tree zero = build_zero_cst (TREE_TYPE (new_var));
    8309           78 :               new_var2 = fold_build2_loc (clause_loc, NE_EXPR,
    8310              :                                           boolean_type_node, new_var, zero);
    8311           78 :               ref2 = fold_build2_loc (clause_loc, NE_EXPR, boolean_type_node,
    8312              :                                       ref, zero);
    8313              :             }
    8314          662 :           x = build2 (code, TREE_TYPE (ref), ref2, new_var2);
    8315          662 :           if (is_truth_op)
    8316           78 :             x = fold_convert (TREE_TYPE (new_var), x);
    8317          662 :           ref = build_outer_var_ref (var, ctx);
    8318          662 :           gimplify_assign (ref, x, &sub_seq);
    8319              :         }
    8320              :     }
    8321              : 
    8322         1158 :   stmt
    8323         1158 :     = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_REDUCTION_START),
    8324              :                          0);
    8325         1158 :   gimple_seq_add_stmt (stmt_seqp, stmt);
    8326              : 
    8327         1158 :   gimple_seq_add_seq (stmt_seqp, sub_seq);
    8328              : 
    8329         1158 :   if (clist)
    8330              :     {
    8331          173 :       gimple_seq_add_seq (stmt_seqp, *clist);
    8332          173 :       *clist = NULL;
    8333              :     }
    8334              : 
    8335         1158 :   stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_REDUCTION_END),
    8336              :                             0);
    8337         1158 :   gimple_seq_add_stmt (stmt_seqp, stmt);
    8338              : }
    8339              : 
    8340              : 
    8341              : /* Generate code to implement the COPYPRIVATE clauses.  */
    8342              : 
    8343              : static void
    8344          118 : lower_copyprivate_clauses (tree clauses, gimple_seq *slist, gimple_seq *rlist,
    8345              :                             omp_context *ctx)
    8346              : {
    8347          118 :   tree c;
    8348              : 
    8349          469 :   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
    8350              :     {
    8351          351 :       tree var, new_var, ref, x;
    8352          351 :       bool by_ref;
    8353          351 :       location_t clause_loc = OMP_CLAUSE_LOCATION (c);
    8354              : 
    8355          351 :       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_COPYPRIVATE)
    8356            6 :         continue;
    8357              : 
    8358          345 :       var = OMP_CLAUSE_DECL (c);
    8359          345 :       by_ref = use_pointer_for_field (var, NULL);
    8360              : 
    8361          345 :       ref = build_sender_ref (var, ctx);
    8362          345 :       x = new_var = lookup_decl_in_outer_ctx (var, ctx);
    8363          345 :       if (by_ref)
    8364              :         {
    8365           74 :           x = build_fold_addr_expr_loc (clause_loc, new_var);
    8366           74 :           x = fold_convert_loc (clause_loc, TREE_TYPE (ref), x);
    8367              :         }
    8368          345 :       gimplify_assign (ref, x, slist);
    8369              : 
    8370          345 :       ref = build_receiver_ref (var, false, ctx);
    8371          345 :       if (by_ref)
    8372              :         {
    8373           74 :           ref = fold_convert_loc (clause_loc,
    8374           74 :                                   build_pointer_type (TREE_TYPE (new_var)),
    8375              :                                   ref);
    8376           74 :           ref = build_fold_indirect_ref_loc (clause_loc, ref);
    8377              :         }
    8378          345 :       if (omp_privatize_by_reference (var))
    8379              :         {
    8380          171 :           ref = fold_convert_loc (clause_loc, TREE_TYPE (new_var), ref);
    8381          171 :           ref = build_simple_mem_ref_loc (clause_loc, ref);
    8382          171 :           new_var = build_simple_mem_ref_loc (clause_loc, new_var);
    8383              :         }
    8384          345 :       x = lang_hooks.decls.omp_clause_assign_op (c, new_var, ref);
    8385          345 :       gimplify_and_add (x, rlist);
    8386              :     }
    8387          118 : }
    8388              : 
    8389              : 
    8390              : /* Generate code to implement the clauses, FIRSTPRIVATE, COPYIN, LASTPRIVATE,
    8391              :    and REDUCTION from the sender (aka parent) side.  */
    8392              : 
    8393              : static void
    8394        23436 : lower_send_clauses (tree clauses, gimple_seq *ilist, gimple_seq *olist,
    8395              :                     omp_context *ctx)
    8396              : {
    8397        23436 :   tree c, t;
    8398        23436 :   int ignored_looptemp = 0;
    8399        23436 :   bool is_taskloop = false;
    8400              : 
    8401              :   /* For taskloop, ignore first two _looptemp_ clauses, those are initialized
    8402              :      by GOMP_taskloop.  */
    8403        23436 :   if (is_task_ctx (ctx) && gimple_omp_task_taskloop_p (ctx->stmt))
    8404              :     {
    8405              :       ignored_looptemp = 2;
    8406              :       is_taskloop = true;
    8407              :     }
    8408              : 
    8409       122023 :   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
    8410              :     {
    8411        98587 :       tree val, ref, x, var;
    8412        98587 :       bool by_ref, do_in = false, do_out = false;
    8413        98587 :       location_t clause_loc = OMP_CLAUSE_LOCATION (c);
    8414              : 
    8415        98587 :       switch (OMP_CLAUSE_CODE (c))
    8416              :         {
    8417         4726 :         case OMP_CLAUSE_PRIVATE:
    8418         4726 :           if (OMP_CLAUSE_PRIVATE_OUTER_REF (c))
    8419              :             break;
    8420         4571 :           continue;
    8421              :         case OMP_CLAUSE_FIRSTPRIVATE:
    8422              :         case OMP_CLAUSE_COPYIN:
    8423              :         case OMP_CLAUSE_LASTPRIVATE:
    8424              :         case OMP_CLAUSE_IN_REDUCTION:
    8425              :         case OMP_CLAUSE__REDUCTEMP_:
    8426              :           break;
    8427         5136 :         case OMP_CLAUSE_REDUCTION:
    8428         5136 :           if (is_task_ctx (ctx) || OMP_CLAUSE_REDUCTION_TASK (c))
    8429          753 :             continue;
    8430              :           break;
    8431        18469 :         case OMP_CLAUSE_SHARED:
    8432        18469 :           if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
    8433              :             break;
    8434        18157 :           continue;
    8435        20425 :         case OMP_CLAUSE__LOOPTEMP_:
    8436        20425 :           if (ignored_looptemp)
    8437              :             {
    8438         2674 :               ignored_looptemp--;
    8439         2674 :               continue;
    8440              :             }
    8441              :           break;
    8442        12170 :         default:
    8443        12170 :           continue;
    8444              :         }
    8445              : 
    8446        60262 :       val = OMP_CLAUSE_DECL (c);
    8447        60262 :       if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
    8448        55879 :            || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
    8449        61927 :           && TREE_CODE (val) == MEM_REF)
    8450              :         {
    8451         1657 :           val = TREE_OPERAND (val, 0);
    8452         1657 :           if (TREE_CODE (val) == POINTER_PLUS_EXPR)
    8453          351 :             val = TREE_OPERAND (val, 0);
    8454         1657 :           if (INDIRECT_REF_P (val)
    8455         1555 :               || TREE_CODE (val) == ADDR_EXPR)
    8456          946 :             val = TREE_OPERAND (val, 0);
    8457         1657 :           if (is_variable_sized (val))
    8458           56 :             continue;
    8459              :         }
    8460              : 
    8461              :       /* For OMP_CLAUSE_SHARED_FIRSTPRIVATE, look beyond the
    8462              :          outer taskloop region.  */
    8463        60206 :       omp_context *ctx_for_o = ctx;
    8464        60206 :       if (is_taskloop
    8465         3919 :           && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
    8466        60518 :           && OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
    8467          312 :         ctx_for_o = ctx->outer;
    8468              : 
    8469        60206 :       var = lookup_decl_in_outer_ctx (val, ctx_for_o);
    8470              : 
    8471        60206 :       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_COPYIN
    8472        59692 :           && is_global_var (var)
    8473        62527 :           && (val == OMP_CLAUSE_DECL (c)
    8474          273 :               || !is_task_ctx (ctx)
    8475          241 :               || (TREE_CODE (TREE_TYPE (val)) != POINTER_TYPE
    8476          187 :                   && (TREE_CODE (TREE_TYPE (val)) != REFERENCE_TYPE
    8477            0 :                       || (TREE_CODE (TREE_TYPE (TREE_TYPE (val)))
    8478              :                           != POINTER_TYPE)))))
    8479         2267 :         continue;
    8480              : 
    8481        57939 :       t = omp_member_access_dummy_var (var);
    8482        57939 :       if (t)
    8483              :         {
    8484          249 :           var = DECL_VALUE_EXPR (var);
    8485          249 :           tree o = maybe_lookup_decl_in_outer_ctx (t, ctx_for_o);
    8486          249 :           if (o != t)
    8487           69 :             var = unshare_and_remap (var, t, o);
    8488              :           else
    8489          180 :             var = unshare_expr (var);
    8490              :         }
    8491              : 
    8492        57939 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
    8493              :         {
    8494              :           /* Handle taskloop firstprivate/lastprivate, where the
    8495              :              lastprivate on GIMPLE_OMP_TASK is represented as
    8496              :              OMP_CLAUSE_SHARED_FIRSTPRIVATE.  */
    8497           79 :           tree f = lookup_sfield ((splay_tree_key) &DECL_UID (val), ctx);
    8498           79 :           x = omp_build_component_ref (ctx->sender_decl, f);
    8499           79 :           if (use_pointer_for_field (val, ctx))
    8500           59 :             var = build_fold_addr_expr (var);
    8501           79 :           gimplify_assign (x, var, ilist);
    8502           79 :           DECL_ABSTRACT_ORIGIN (f) = NULL;
    8503           79 :           continue;
    8504           79 :         }
    8505              : 
    8506        57918 :       if (((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
    8507        54222 :             && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION)
    8508         4795 :            || val == OMP_CLAUSE_DECL (c))
    8509       114338 :           && is_variable_sized (val))
    8510           58 :         continue;
    8511        57802 :       by_ref = use_pointer_for_field (val, NULL);
    8512              : 
    8513        57802 :       switch (OMP_CLAUSE_CODE (c))
    8514              :         {
    8515        27292 :         case OMP_CLAUSE_FIRSTPRIVATE:
    8516        27292 :           if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
    8517         7254 :               && !by_ref
    8518        34492 :               && is_task_ctx (ctx))
    8519         2333 :             suppress_warning (var);
    8520              :           do_in = true;
    8521              :           break;
    8522              : 
    8523              :         case OMP_CLAUSE_PRIVATE:
    8524              :         case OMP_CLAUSE_COPYIN:
    8525              :         case OMP_CLAUSE__LOOPTEMP_:
    8526              :         case OMP_CLAUSE__REDUCTEMP_:
    8527              :           do_in = true;
    8528              :           break;
    8529              : 
    8530         6737 :         case OMP_CLAUSE_LASTPRIVATE:
    8531         6737 :           if (by_ref || omp_privatize_by_reference (val))
    8532              :             {
    8533          384 :               if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
    8534          239 :                 continue;
    8535              :               do_in = true;
    8536              :             }
    8537              :           else
    8538              :             {
    8539         6353 :               do_out = true;
    8540         6353 :               if (lang_hooks.decls.omp_private_outer_ref (val))
    8541              :                 do_in = true;
    8542              :             }
    8543              :           break;
    8544              : 
    8545         4795 :         case OMP_CLAUSE_REDUCTION:
    8546         4795 :         case OMP_CLAUSE_IN_REDUCTION:
    8547         4795 :           do_in = true;
    8548         4795 :           if (val == OMP_CLAUSE_DECL (c))
    8549              :             {
    8550         3413 :               if (is_task_ctx (ctx))
    8551          322 :                 by_ref = use_pointer_for_field (val, ctx);
    8552              :               else
    8553         3091 :                 do_out = !(by_ref || omp_privatize_by_reference (val));
    8554              :             }
    8555              :           else
    8556         1382 :             by_ref = TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE;
    8557              :           break;
    8558              : 
    8559            0 :         default:
    8560            0 :           gcc_unreachable ();
    8561              :         }
    8562              : 
    8563         4037 :       if (do_in)
    8564              :         {
    8565        51234 :           ref = build_sender_ref (val, ctx);
    8566        51234 :           x = by_ref ? build_fold_addr_expr_loc (clause_loc, var) : var;
    8567        51234 :           gimplify_assign (ref, x, ilist);
    8568        51234 :           if (is_task_ctx (ctx))
    8569         6270 :             DECL_ABSTRACT_ORIGIN (TREE_OPERAND (ref, 1)) = NULL;
    8570              :         }
    8571              : 
    8572        57563 :       if (do_out)
    8573              :         {
    8574         8892 :           ref = build_sender_ref (val, ctx);
    8575         8892 :           gimplify_assign (var, ref, olist);
    8576              :         }
    8577              :     }
    8578        23436 : }
    8579              : 
    8580              : /* Generate code to implement SHARED from the sender (aka parent)
    8581              :    side.  This is trickier, since GIMPLE_OMP_PARALLEL_CLAUSES doesn't
    8582              :    list things that got automatically shared.  */
    8583              : 
    8584              : static void
    8585        23436 : lower_send_shared_vars (gimple_seq *ilist, gimple_seq *olist, omp_context *ctx)
    8586              : {
    8587        23436 :   tree var, ovar, nvar, t, f, x, record_type;
    8588              : 
    8589        23436 :   if (ctx->record_type == NULL)
    8590         4540 :     return;
    8591              : 
    8592        18896 :   record_type = ctx->srecord_type ? ctx->srecord_type : ctx->record_type;
    8593        91218 :   for (f = TYPE_FIELDS (record_type); f ; f = DECL_CHAIN (f))
    8594              :     {
    8595        72322 :       ovar = DECL_ABSTRACT_ORIGIN (f);
    8596        72322 :       if (!ovar || TREE_CODE (ovar) == FIELD_DECL)
    8597         6349 :         continue;
    8598              : 
    8599        65973 :       nvar = maybe_lookup_decl (ovar, ctx);
    8600       119580 :       if (!nvar
    8601        65560 :           || !DECL_HAS_VALUE_EXPR_P (nvar)
    8602        78560 :           || (ctx->allocate_map
    8603          317 :               && ctx->allocate_map->get (ovar)))
    8604        53607 :         continue;
    8605              : 
    8606              :       /* If CTX is a nested parallel directive.  Find the immediately
    8607              :          enclosing parallel or workshare construct that contains a
    8608              :          mapping for OVAR.  */
    8609        12366 :       var = lookup_decl_in_outer_ctx (ovar, ctx);
    8610              : 
    8611        12366 :       t = omp_member_access_dummy_var (var);
    8612        12366 :       if (t)
    8613              :         {
    8614           49 :           var = DECL_VALUE_EXPR (var);
    8615           49 :           tree o = maybe_lookup_decl_in_outer_ctx (t, ctx);
    8616           49 :           if (o != t)
    8617           18 :             var = unshare_and_remap (var, t, o);
    8618              :           else
    8619           31 :             var = unshare_expr (var);
    8620              :         }
    8621              : 
    8622        12366 :       if (use_pointer_for_field (ovar, ctx))
    8623              :         {
    8624         7030 :           x = build_sender_ref (ovar, ctx);
    8625         7030 :           if (TREE_CODE (TREE_TYPE (f)) == ARRAY_TYPE
    8626         7030 :               && TREE_TYPE (f) == TREE_TYPE (ovar))
    8627              :             {
    8628          107 :               gcc_assert (is_parallel_ctx (ctx)
    8629              :                           && DECL_ARTIFICIAL (ovar));
    8630              :               /* _condtemp_ clause.  */
    8631          107 :               var = build_constructor (TREE_TYPE (x), NULL);
    8632              :             }
    8633              :           else
    8634         6923 :             var = build_fold_addr_expr (var);
    8635         7030 :           gimplify_assign (x, var, ilist);
    8636              :         }
    8637              :       else
    8638              :         {
    8639         5336 :           x = build_sender_ref (ovar, ctx);
    8640         5336 :           gimplify_assign (x, var, ilist);
    8641              : 
    8642         5336 :           if (!TREE_READONLY (var)
    8643              :               /* We don't need to receive a new reference to a result
    8644              :                  or parm decl.  In fact we may not store to it as we will
    8645              :                  invalidate any pending RSO and generate wrong gimple
    8646              :                  during inlining.  */
    8647         5336 :               && !((TREE_CODE (var) == RESULT_DECL
    8648         3386 :                     || TREE_CODE (var) == PARM_DECL)
    8649          182 :                    && DECL_BY_REFERENCE (var)))
    8650              :             {
    8651         3315 :               x = build_sender_ref (ovar, ctx);
    8652         3315 :               gimplify_assign (var, x, olist);
    8653              :             }
    8654              :         }
    8655              :     }
    8656              : }
    8657              : 
    8658              : /* Emit an OpenACC head marker call, encapulating the partitioning and
    8659              :    other information that must be processed by the target compiler.
    8660              :    Return the maximum number of dimensions the associated loop might
    8661              :    be partitioned over.  */
    8662              : 
    8663              : static unsigned
    8664         9665 : lower_oacc_head_mark (location_t loc, tree ddvar, tree clauses,
    8665              :                       gimple_seq *seq, omp_context *ctx)
    8666              : {
    8667         9665 :   unsigned levels = 0;
    8668         9665 :   unsigned tag = 0;
    8669         9665 :   tree gang_static = NULL_TREE;
    8670         9665 :   auto_vec<tree, 5> args;
    8671              : 
    8672        19330 :   args.quick_push (build_int_cst
    8673         9665 :                    (integer_type_node, IFN_UNIQUE_OACC_HEAD_MARK));
    8674         9665 :   args.quick_push (ddvar);
    8675        40519 :   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    8676              :     {
    8677        21189 :       switch (OMP_CLAUSE_CODE (c))
    8678              :         {
    8679         1856 :         case OMP_CLAUSE_GANG:
    8680         1856 :           tag |= OLF_DIM_GANG;
    8681         1856 :           gang_static = OMP_CLAUSE_GANG_STATIC_EXPR (c);
    8682              :           /* static:* is represented by -1, and we can ignore it, as
    8683              :              scheduling is always static.  */
    8684         1856 :           if (gang_static && integer_minus_onep (gang_static))
    8685           70 :             gang_static = NULL_TREE;
    8686         1856 :           levels++;
    8687         1856 :           break;
    8688              : 
    8689         1330 :         case OMP_CLAUSE_WORKER:
    8690         1330 :           tag |= OLF_DIM_WORKER;
    8691         1330 :           levels++;
    8692         1330 :           break;
    8693              : 
    8694         1523 :         case OMP_CLAUSE_VECTOR:
    8695         1523 :           tag |= OLF_DIM_VECTOR;
    8696         1523 :           levels++;
    8697         1523 :           break;
    8698              : 
    8699          314 :         case OMP_CLAUSE_SEQ:
    8700          314 :           tag |= OLF_SEQ;
    8701          314 :           break;
    8702              : 
    8703          409 :         case OMP_CLAUSE_AUTO:
    8704          409 :           tag |= OLF_AUTO;
    8705          409 :           break;
    8706              : 
    8707          260 :         case OMP_CLAUSE_INDEPENDENT:
    8708          260 :           tag |= OLF_INDEPENDENT;
    8709          260 :           break;
    8710              : 
    8711          136 :         case OMP_CLAUSE_TILE:
    8712          136 :           tag |= OLF_TILE;
    8713          136 :           break;
    8714              : 
    8715         3986 :         case OMP_CLAUSE_REDUCTION:
    8716         3986 :           tag |= OLF_REDUCTION;
    8717         3986 :           break;
    8718              : 
    8719        11375 :         default:
    8720        11375 :           continue;
    8721              :         }
    8722              :     }
    8723              : 
    8724         9665 :   if (gang_static)
    8725              :     {
    8726          146 :       if (DECL_P (gang_static))
    8727           16 :         gang_static = build_outer_var_ref (gang_static, ctx);
    8728          146 :       tag |= OLF_GANG_STATIC;
    8729              :     }
    8730              : 
    8731         9665 :   omp_context *tgt = enclosing_target_ctx (ctx);
    8732         9665 :   if (!tgt || is_oacc_parallel_or_serial (tgt))
    8733              :     ;
    8734           62 :   else if (is_oacc_kernels (tgt))
    8735              :     /* Not using this loops handling inside OpenACC 'kernels' regions.  */
    8736            0 :     gcc_unreachable ();
    8737           62 :   else if (is_oacc_kernels_decomposed_part (tgt))
    8738              :     ;
    8739              :   else
    8740            0 :     gcc_unreachable ();
    8741              : 
    8742              :   /* In a parallel region, loops are implicitly INDEPENDENT.  */
    8743         9665 :   if (!tgt || is_oacc_parallel_or_serial (tgt))
    8744         9603 :     tag |= OLF_INDEPENDENT;
    8745              : 
    8746              :   /* Loops inside OpenACC 'kernels' decomposed parts' regions are expected to
    8747              :      have an explicit 'seq' or 'independent' clause, and no 'auto' clause.  */
    8748         9665 :   if (tgt && is_oacc_kernels_decomposed_part (tgt))
    8749              :     {
    8750           62 :       gcc_assert (tag & (OLF_SEQ | OLF_INDEPENDENT));
    8751           62 :       gcc_assert (!(tag & OLF_AUTO));
    8752              :     }
    8753              : 
    8754         9665 :   if (tag & OLF_TILE)
    8755              :     /* Tiling could use all 3 levels.  */
    8756              :     levels = 3;
    8757              :   else
    8758              :     {
    8759              :       /* A loop lacking SEQ, GANG, WORKER and/or VECTOR could be AUTO.
    8760              :          Ensure at least one level, or 2 for possible auto
    8761              :          partitioning */
    8762         9529 :       bool maybe_auto = !(tag & (((GOMP_DIM_MASK (GOMP_DIM_MAX) - 1)
    8763              :                                   << OLF_DIM_BASE) | OLF_SEQ));
    8764              : 
    8765         9529 :       if (levels < 1u + maybe_auto)
    8766              :         levels = 1u + maybe_auto;
    8767              :     }
    8768              : 
    8769         9665 :   args.quick_push (build_int_cst (integer_type_node, levels));
    8770         9665 :   args.quick_push (build_int_cst (integer_type_node, tag));
    8771         9665 :   if (gang_static)
    8772          146 :     args.quick_push (gang_static);
    8773              : 
    8774         9665 :   gcall *call = gimple_build_call_internal_vec (IFN_UNIQUE, args);
    8775         9665 :   gimple_set_location (call, loc);
    8776         9665 :   gimple_set_lhs (call, ddvar);
    8777         9665 :   gimple_seq_add_stmt (seq, call);
    8778              : 
    8779         9665 :   return levels;
    8780         9665 : }
    8781              : 
    8782              : /* Emit an OpenACC lopp head or tail marker to SEQ.  LEVEL is the
    8783              :    partitioning level of the enclosed region.  */
    8784              : 
    8785              : static void
    8786        42767 : lower_oacc_loop_marker (location_t loc, tree ddvar, bool head,
    8787              :                         tree tofollow, gimple_seq *seq)
    8788              : {
    8789        42767 :   int marker_kind = (head ? IFN_UNIQUE_OACC_HEAD_MARK
    8790              :                      : IFN_UNIQUE_OACC_TAIL_MARK);
    8791        42767 :   tree marker = build_int_cst (integer_type_node, marker_kind);
    8792        42767 :   int nargs = 2 + (tofollow != NULL_TREE);
    8793        42767 :   gcall *call = gimple_build_call_internal (IFN_UNIQUE, nargs,
    8794              :                                             marker, ddvar, tofollow);
    8795        42767 :   gimple_set_location (call, loc);
    8796        42767 :   gimple_set_lhs (call, ddvar);
    8797        42767 :   gimple_seq_add_stmt (seq, call);
    8798        42767 : }
    8799              : 
    8800              : /* Generate the before and after OpenACC loop sequences.  CLAUSES are
    8801              :    the loop clauses, from which we extract reductions.  Initialize
    8802              :    HEAD and TAIL.  */
    8803              : 
    8804              : static void
    8805         9665 : lower_oacc_head_tail (location_t loc, tree clauses, gcall *private_marker,
    8806              :                       gimple_seq *head, gimple_seq *tail, omp_context *ctx)
    8807              : {
    8808         9665 :   bool inner = false;
    8809         9665 :   tree ddvar = create_tmp_var (integer_type_node, ".data_dep");
    8810         9665 :   gimple_seq_add_stmt (head, gimple_build_assign (ddvar, integer_zero_node));
    8811              : 
    8812         9665 :   unsigned count = lower_oacc_head_mark (loc, ddvar, clauses, head, ctx);
    8813              : 
    8814         9665 :   if (private_marker)
    8815              :     {
    8816          229 :       gimple_set_location (private_marker, loc);
    8817          229 :       gimple_call_set_lhs (private_marker, ddvar);
    8818          229 :       gimple_call_set_arg (private_marker, 1, ddvar);
    8819              :     }
    8820              : 
    8821         9665 :   tree fork_kind = build_int_cst (unsigned_type_node, IFN_UNIQUE_OACC_FORK);
    8822         9665 :   tree join_kind = build_int_cst (unsigned_type_node, IFN_UNIQUE_OACC_JOIN);
    8823              : 
    8824         9665 :   gcc_assert (count);
    8825        26216 :   for (unsigned done = 1; count; count--, done++)
    8826              :     {
    8827        16551 :       gimple_seq fork_seq = NULL;
    8828        16551 :       gimple_seq join_seq = NULL;
    8829              : 
    8830        16551 :       tree place = build_int_cst (integer_type_node, -1);
    8831        16551 :       gcall *fork = gimple_build_call_internal (IFN_UNIQUE, 3,
    8832              :                                                 fork_kind, ddvar, place);
    8833        16551 :       gimple_set_location (fork, loc);
    8834        16551 :       gimple_set_lhs (fork, ddvar);
    8835              : 
    8836        16551 :       gcall *join = gimple_build_call_internal (IFN_UNIQUE, 3,
    8837              :                                                 join_kind, ddvar, place);
    8838        16551 :       gimple_set_location (join, loc);
    8839        16551 :       gimple_set_lhs (join, ddvar);
    8840              : 
    8841              :       /* Mark the beginning of this level sequence.  */
    8842        16551 :       if (inner)
    8843         6886 :         lower_oacc_loop_marker (loc, ddvar, true,
    8844         6886 :                                 build_int_cst (integer_type_node, count),
    8845              :                                 &fork_seq);
    8846        16551 :       lower_oacc_loop_marker (loc, ddvar, false,
    8847        16551 :                               build_int_cst (integer_type_node, done),
    8848              :                               &join_seq);
    8849              : 
    8850        23437 :       lower_oacc_reductions (loc, clauses, place, inner,
    8851              :                              fork, (count == 1) ? private_marker : NULL,
    8852              :                              join, &fork_seq, &join_seq,  ctx);
    8853              : 
    8854              :       /* Append this level to head. */
    8855        16551 :       gimple_seq_add_seq (head, fork_seq);
    8856              :       /* Prepend it to tail.  */
    8857        16551 :       gimple_seq_add_seq (&join_seq, *tail);
    8858        16551 :       *tail = join_seq;
    8859              : 
    8860        16551 :       inner = true;
    8861              :     }
    8862              : 
    8863              :   /* Mark the end of the sequence.  */
    8864         9665 :   lower_oacc_loop_marker (loc, ddvar, true, NULL_TREE, head);
    8865         9665 :   lower_oacc_loop_marker (loc, ddvar, false, NULL_TREE, tail);
    8866         9665 : }
    8867              : 
    8868              : /* If exceptions are enabled, wrap the statements in BODY in a MUST_NOT_THROW
    8869              :    catch handler and return it.  This prevents programs from violating the
    8870              :    structured block semantics with throws.  */
    8871              : 
    8872              : static gimple_seq
    8873        99895 : maybe_catch_exception (gimple_seq body)
    8874              : {
    8875        99895 :   gimple *g;
    8876        99895 :   tree decl;
    8877              : 
    8878        99895 :   if (!flag_exceptions)
    8879              :     return body;
    8880              : 
    8881        44844 :   if (lang_hooks.eh_protect_cleanup_actions != NULL)
    8882        44816 :     decl = lang_hooks.eh_protect_cleanup_actions ();
    8883              :   else
    8884           28 :     decl = builtin_decl_explicit (BUILT_IN_TRAP);
    8885              : 
    8886        44844 :   g = gimple_build_eh_must_not_throw (decl);
    8887        44844 :   g = gimple_build_try (body, gimple_seq_alloc_with_stmt (g),
    8888              :                         GIMPLE_TRY_CATCH);
    8889              : 
    8890        44844 :  return gimple_seq_alloc_with_stmt (g);
    8891              : }
    8892              : 
    8893              : 
    8894              : /* Routines to lower OMP directives into OMP-GIMPLE.  */
    8895              : 
    8896              : /* If ctx is a worksharing context inside of a cancellable parallel
    8897              :    region and it isn't nowait, add lhs to its GIMPLE_OMP_RETURN
    8898              :    and conditional branch to parallel's cancel_label to handle
    8899              :    cancellation in the implicit barrier.  */
    8900              : 
    8901              : static void
    8902        51151 : maybe_add_implicit_barrier_cancel (omp_context *ctx, gimple *omp_return,
    8903              :                                    gimple_seq *body)
    8904              : {
    8905        51151 :   gcc_assert (gimple_code (omp_return) == GIMPLE_OMP_RETURN);
    8906        51151 :   if (gimple_omp_return_nowait_p (omp_return))
    8907              :     return;
    8908        19691 :   for (omp_context *outer = ctx->outer; outer; outer = outer->outer)
    8909        16057 :     if (gimple_code (outer->stmt) == GIMPLE_OMP_PARALLEL
    8910        16057 :         && outer->cancellable)
    8911              :       {
    8912           65 :         tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
    8913           65 :         tree c_bool_type = TREE_TYPE (TREE_TYPE (fndecl));
    8914           65 :         tree lhs = create_tmp_var (c_bool_type);
    8915           65 :         gimple_omp_return_set_lhs (omp_return, lhs);
    8916           65 :         tree fallthru_label = create_artificial_label (UNKNOWN_LOCATION);
    8917           65 :         gimple *g = gimple_build_cond (NE_EXPR, lhs,
    8918              :                                        fold_convert (c_bool_type,
    8919              :                                                      boolean_false_node),
    8920              :                                        outer->cancel_label, fallthru_label);
    8921           65 :         gimple_seq_add_stmt (body, g);
    8922           65 :         gimple_seq_add_stmt (body, gimple_build_label (fallthru_label));
    8923              :       }
    8924        15992 :     else if (gimple_code (outer->stmt) != GIMPLE_OMP_TASKGROUP
    8925        15992 :              && gimple_code (outer->stmt) != GIMPLE_OMP_SCOPE)
    8926              :       return;
    8927              : }
    8928              : 
    8929              : /* Find the first task_reduction or reduction clause or return NULL
    8930              :    if there are none.  */
    8931              : 
    8932              : static inline tree
    8933        51390 : omp_task_reductions_find_first (tree clauses, enum tree_code code,
    8934              :                                 enum omp_clause_code ccode)
    8935              : {
    8936        67288 :   while (1)
    8937              :     {
    8938        59339 :       clauses = omp_find_clause (clauses, ccode);
    8939        59339 :       if (clauses == NULL_TREE)
    8940              :         return NULL_TREE;
    8941         9415 :       if (ccode != OMP_CLAUSE_REDUCTION
    8942         9415 :           || code == OMP_TASKLOOP
    8943         9415 :           || OMP_CLAUSE_REDUCTION_TASK (clauses))
    8944              :         return clauses;
    8945         7949 :       clauses = OMP_CLAUSE_CHAIN (clauses);
    8946              :     }
    8947              : }
    8948              : 
    8949              : static void lower_omp_task_reductions (omp_context *, enum tree_code, tree,
    8950              :                                        gimple_seq *, gimple_seq *);
    8951              : 
    8952              : /* Lower the OpenMP sections directive in the current statement in GSI_P.
    8953              :    CTX is the enclosing OMP context for the current statement.  */
    8954              : 
    8955              : static void
    8956          386 : lower_omp_sections (gimple_stmt_iterator *gsi_p, omp_context *ctx)
    8957              : {
    8958          386 :   tree block, control;
    8959          386 :   gimple_stmt_iterator tgsi;
    8960          386 :   gomp_sections *stmt;
    8961          386 :   gimple *t;
    8962          386 :   gbind *new_stmt, *bind;
    8963          386 :   gimple_seq ilist, dlist, olist, tred_dlist = NULL, clist = NULL, new_body;
    8964              : 
    8965          386 :   stmt = as_a <gomp_sections *> (gsi_stmt (*gsi_p));
    8966              : 
    8967          386 :   push_gimplify_context ();
    8968              : 
    8969          386 :   dlist = NULL;
    8970          386 :   ilist = NULL;
    8971              : 
    8972          386 :   tree rclauses
    8973          386 :     = omp_task_reductions_find_first (gimple_omp_sections_clauses (stmt),
    8974              :                                       OMP_SECTIONS, OMP_CLAUSE_REDUCTION);
    8975          386 :   tree rtmp = NULL_TREE;
    8976          386 :   if (rclauses)
    8977              :     {
    8978            8 :       tree type = build_pointer_type (pointer_sized_int_node);
    8979            8 :       tree temp = create_tmp_var (type);
    8980            8 :       tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
    8981            8 :       OMP_CLAUSE_DECL (c) = temp;
    8982            8 :       OMP_CLAUSE_CHAIN (c) = gimple_omp_sections_clauses (stmt);
    8983            8 :       gimple_omp_sections_set_clauses (stmt, c);
    8984            8 :       lower_omp_task_reductions (ctx, OMP_SECTIONS,
    8985              :                                  gimple_omp_sections_clauses (stmt),
    8986              :                                  &ilist, &tred_dlist);
    8987            8 :       rclauses = c;
    8988            8 :       rtmp = make_ssa_name (type);
    8989            8 :       gimple_seq_add_stmt (&ilist, gimple_build_assign (rtmp, temp));
    8990              :     }
    8991              : 
    8992          386 :   tree *clauses_ptr = gimple_omp_sections_clauses_ptr (stmt);
    8993          386 :   lower_lastprivate_conditional_clauses (clauses_ptr, ctx);
    8994              : 
    8995          386 :   lower_rec_input_clauses (gimple_omp_sections_clauses (stmt),
    8996              :                            &ilist, &dlist, ctx, NULL);
    8997              : 
    8998          386 :   control = create_tmp_var (unsigned_type_node, ".section");
    8999          386 :   gimple_omp_sections_set_control (stmt, control);
    9000              : 
    9001          386 :   new_body = gimple_omp_body (stmt);
    9002          386 :   gimple_omp_set_body (stmt, NULL);
    9003          386 :   tgsi = gsi_start (new_body);
    9004         1249 :   for (; !gsi_end_p (tgsi); gsi_next (&tgsi))
    9005              :     {
    9006          863 :       omp_context *sctx;
    9007          863 :       gimple *sec_start;
    9008              : 
    9009          863 :       sec_start = gsi_stmt (tgsi);
    9010          863 :       sctx = maybe_lookup_ctx (sec_start);
    9011          863 :       gcc_assert (sctx);
    9012              : 
    9013          863 :       lower_omp (gimple_omp_body_ptr (sec_start), sctx);
    9014          863 :       gsi_insert_seq_after (&tgsi, gimple_omp_body (sec_start),
    9015              :                             GSI_CONTINUE_LINKING);
    9016          863 :       gimple_omp_set_body (sec_start, NULL);
    9017              : 
    9018          863 :       if (gsi_one_before_end_p (tgsi))
    9019              :         {
    9020          386 :           gimple_seq l = NULL;
    9021          386 :           lower_lastprivate_clauses (gimple_omp_sections_clauses (stmt), NULL,
    9022              :                                      &ilist, &l, &clist, ctx);
    9023          386 :           gsi_insert_seq_after (&tgsi, l, GSI_CONTINUE_LINKING);
    9024          386 :           gimple_omp_section_set_last (sec_start);
    9025              :         }
    9026              : 
    9027          863 :       gsi_insert_after (&tgsi, gimple_build_omp_return (false),
    9028              :                         GSI_CONTINUE_LINKING);
    9029              :     }
    9030              : 
    9031          386 :   block = make_node (BLOCK);
    9032          386 :   bind = gimple_build_bind (NULL, new_body, block);
    9033              : 
    9034          386 :   olist = NULL;
    9035          386 :   lower_reduction_clauses (gimple_omp_sections_clauses (stmt), &olist,
    9036              :                            &clist, ctx);
    9037          386 :   if (clist)
    9038              :     {
    9039           10 :       tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_REDUCTION_START);
    9040           10 :       gcall *g = gimple_build_call (fndecl, 0);
    9041           10 :       gimple_seq_add_stmt (&olist, g);
    9042           10 :       gimple_seq_add_seq (&olist, clist);
    9043           10 :       fndecl = builtin_decl_explicit (BUILT_IN_GOMP_REDUCTION_END);
    9044           10 :       g = gimple_build_call (fndecl, 0);
    9045           10 :       gimple_seq_add_stmt (&olist, g);
    9046              :     }
    9047              : 
    9048          386 :   block = make_node (BLOCK);
    9049          386 :   new_stmt = gimple_build_bind (NULL, NULL, block);
    9050          386 :   gsi_replace (gsi_p, new_stmt, true);
    9051              : 
    9052          386 :   pop_gimplify_context (new_stmt);
    9053          386 :   gimple_bind_append_vars (new_stmt, ctx->block_vars);
    9054          386 :   BLOCK_VARS (block) = gimple_bind_vars (bind);
    9055          386 :   if (BLOCK_VARS (block))
    9056            0 :     TREE_USED (block) = 1;
    9057              : 
    9058          386 :   new_body = NULL;
    9059          386 :   gimple_seq_add_seq (&new_body, ilist);
    9060          386 :   gimple_seq_add_stmt (&new_body, stmt);
    9061          386 :   gimple_seq_add_stmt (&new_body, gimple_build_omp_sections_switch ());
    9062          386 :   gimple_seq_add_stmt (&new_body, bind);
    9063              : 
    9064          386 :   t = gimple_build_omp_continue (control, control);
    9065          386 :   gimple_seq_add_stmt (&new_body, t);
    9066              : 
    9067          386 :   gimple_seq_add_seq (&new_body, olist);
    9068          386 :   if (ctx->cancellable)
    9069           19 :     gimple_seq_add_stmt (&new_body, gimple_build_label (ctx->cancel_label));
    9070          386 :   gimple_seq_add_seq (&new_body, dlist);
    9071              : 
    9072          386 :   new_body = maybe_catch_exception (new_body);
    9073              : 
    9074          386 :   bool nowait = omp_find_clause (gimple_omp_sections_clauses (stmt),
    9075          386 :                                  OMP_CLAUSE_NOWAIT) != NULL_TREE;
    9076          386 :   t = gimple_build_omp_return (nowait);
    9077          386 :   gimple_seq_add_stmt (&new_body, t);
    9078          386 :   gimple_seq_add_seq (&new_body, tred_dlist);
    9079          386 :   maybe_add_implicit_barrier_cancel (ctx, t, &new_body);
    9080              : 
    9081          386 :   if (rclauses)
    9082            8 :     OMP_CLAUSE_DECL (rclauses) = rtmp;
    9083              : 
    9084          386 :   gimple_bind_set_body (new_stmt, new_body);
    9085          386 : }
    9086              : 
    9087              : 
    9088              : /* A subroutine of lower_omp_single.  Expand the simple form of
    9089              :    a GIMPLE_OMP_SINGLE, without a copyprivate clause:
    9090              : 
    9091              :         if (GOMP_single_start ())
    9092              :           BODY;
    9093              :         [ GOMP_barrier (); ]    -> unless 'nowait' is present.
    9094              : 
    9095              :   FIXME.  It may be better to delay expanding the logic of this until
    9096              :   pass_expand_omp.  The expanded logic may make the job more difficult
    9097              :   to a synchronization analysis pass.  */
    9098              : 
    9099              : static void
    9100         1028 : lower_omp_single_simple (gomp_single *single_stmt, gimple_seq *pre_p)
    9101              : {
    9102         1028 :   location_t loc = gimple_location (single_stmt);
    9103         1028 :   tree tlabel = create_artificial_label (loc);
    9104         1028 :   tree flabel = create_artificial_label (loc);
    9105         1028 :   gimple *call, *cond;
    9106         1028 :   tree lhs, decl;
    9107              : 
    9108         2052 :   decl = builtin_decl_explicit (flag_openmp_ompt
    9109              :                                   ? BUILT_IN_GOMP_SINGLE_START_WITH_END
    9110              :                                   : BUILT_IN_GOMP_SINGLE_START);
    9111         1028 :   lhs = create_tmp_var (TREE_TYPE (TREE_TYPE (decl)));
    9112         1028 :   call = gimple_build_call (decl, 0);
    9113         1028 :   gimple_call_set_lhs (call, lhs);
    9114         1028 :   gimple_seq_add_stmt (pre_p, call);
    9115              : 
    9116         1028 :   cond = gimple_build_cond (EQ_EXPR, lhs,
    9117         1028 :                             fold_convert_loc (loc, TREE_TYPE (lhs),
    9118              :                                               boolean_true_node),
    9119              :                             tlabel, flabel);
    9120         1028 :   gimple_seq_add_stmt (pre_p, cond);
    9121         1028 :   gimple_seq_add_stmt (pre_p, gimple_build_label (tlabel));
    9122         1028 :   gimple_seq_add_seq (pre_p, gimple_omp_body (single_stmt));
    9123         1028 :   gimple_seq_add_stmt (pre_p, gimple_build_label (flabel));
    9124         1028 : }
    9125              : 
    9126              : 
    9127              : /* A subroutine of lower_omp_single.  Expand the simple form of
    9128              :    a GIMPLE_OMP_SINGLE, with a copyprivate clause:
    9129              : 
    9130              :         #pragma omp single copyprivate (a, b, c)
    9131              : 
    9132              :    Create a new structure to hold copies of 'a', 'b' and 'c' and emit:
    9133              : 
    9134              :       {
    9135              :         if ((copyout_p = GOMP_single_copy_start ()) == NULL)
    9136              :           {
    9137              :             BODY;
    9138              :             copyout.a = a;
    9139              :             copyout.b = b;
    9140              :             copyout.c = c;
    9141              :             GOMP_single_copy_end (&copyout);
    9142              :           }
    9143              :         else
    9144              :           {
    9145              :             a = copyout_p->a;
    9146              :             b = copyout_p->b;
    9147              :             c = copyout_p->c;
    9148              :           }
    9149              :         GOMP_barrier ();
    9150              :       }
    9151              : 
    9152              :   FIXME.  It may be better to delay expanding the logic of this until
    9153              :   pass_expand_omp.  The expanded logic may make the job more difficult
    9154              :   to a synchronization analysis pass.  */
    9155              : 
    9156              : static void
    9157          118 : lower_omp_single_copy (gomp_single *single_stmt, gimple_seq *pre_p,
    9158              :                        omp_context *ctx)
    9159              : {
    9160          118 :   tree ptr_type, t, l0, l1, l2, bfn_decl;
    9161          118 :   gimple_seq copyin_seq;
    9162          118 :   location_t loc = gimple_location (single_stmt);
    9163              : 
    9164          118 :   ctx->sender_decl = create_tmp_var (ctx->record_type, ".omp_copy_o");
    9165              : 
    9166          118 :   ptr_type = build_pointer_type (ctx->record_type);
    9167          118 :   ctx->receiver_decl = create_tmp_var (ptr_type, ".omp_copy_i");
    9168              : 
    9169          118 :   l0 = create_artificial_label (loc);
    9170          118 :   l1 = create_artificial_label (loc);
    9171          118 :   l2 = create_artificial_label (loc);
    9172              : 
    9173          118 :   bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_COPY_START);
    9174          118 :   t = build_call_expr_loc (loc, bfn_decl, 0);
    9175          118 :   t = fold_convert_loc (loc, ptr_type, t);
    9176          118 :   gimplify_assign (ctx->receiver_decl, t, pre_p);
    9177              : 
    9178          118 :   t = build2 (EQ_EXPR, boolean_type_node, ctx->receiver_decl,
    9179              :               build_int_cst (ptr_type, 0));
    9180          118 :   t = build3 (COND_EXPR, void_type_node, t,
    9181              :               build_and_jump (&l0), build_and_jump (&l1));
    9182          118 :   gimplify_and_add (t, pre_p);
    9183              : 
    9184          118 :   gimple_seq_add_stmt (pre_p, gimple_build_label (l0));
    9185              : 
    9186          118 :   gimple_seq_add_seq (pre_p, gimple_omp_body (single_stmt));
    9187              : 
    9188          118 :   copyin_seq = NULL;
    9189          118 :   lower_copyprivate_clauses (gimple_omp_single_clauses (single_stmt), pre_p,
    9190              :                               &copyin_seq, ctx);
    9191              : 
    9192          118 :   t = build_fold_addr_expr_loc (loc, ctx->sender_decl);
    9193          118 :   bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_COPY_END);
    9194          118 :   t = build_call_expr_loc (loc, bfn_decl, 1, t);
    9195          118 :   gimplify_and_add (t, pre_p);
    9196              : 
    9197          118 :   t = build_and_jump (&l2);
    9198          118 :   gimplify_and_add (t, pre_p);
    9199              : 
    9200          118 :   gimple_seq_add_stmt (pre_p, gimple_build_label (l1));
    9201              : 
    9202          118 :   gimple_seq_add_seq (pre_p, copyin_seq);
    9203              : 
    9204          118 :   gimple_seq_add_stmt (pre_p, gimple_build_label (l2));
    9205          118 : }
    9206              : 
    9207              : 
    9208              : /* Expand code for an OpenMP single directive.  */
    9209              : 
    9210              : static void
    9211         1146 : lower_omp_single (gimple_stmt_iterator *gsi_p, omp_context *ctx)
    9212              : {
    9213         1146 :   tree block;
    9214         1146 :   gomp_single *single_stmt = as_a <gomp_single *> (gsi_stmt (*gsi_p));
    9215         1146 :   gbind *bind;
    9216         1146 :   gimple_seq bind_body, bind_body_tail = NULL, dlist;
    9217              : 
    9218         1146 :   push_gimplify_context ();
    9219              : 
    9220         1146 :   block = make_node (BLOCK);
    9221         1146 :   bind = gimple_build_bind (NULL, NULL, block);
    9222         1146 :   gsi_replace (gsi_p, bind, true);
    9223         1146 :   bind_body = NULL;
    9224         1146 :   dlist = NULL;
    9225         1146 :   lower_rec_input_clauses (gimple_omp_single_clauses (single_stmt),
    9226              :                            &bind_body, &dlist, ctx, NULL);
    9227         1146 :   lower_omp (gimple_omp_body_ptr (single_stmt), ctx);
    9228              : 
    9229         1146 :   gimple_seq_add_stmt (&bind_body, single_stmt);
    9230              : 
    9231         1146 :   if (ctx->record_type)
    9232          118 :     lower_omp_single_copy (single_stmt, &bind_body, ctx);
    9233              :   else
    9234         1028 :     lower_omp_single_simple (single_stmt, &bind_body);
    9235              : 
    9236         1146 :   gimple_omp_set_body (single_stmt, NULL);
    9237              : 
    9238         1146 :   gimple_seq_add_seq (&bind_body, dlist);
    9239              : 
    9240         1146 :   bind_body = maybe_catch_exception (bind_body);
    9241              : 
    9242         1146 :   bool nowait = omp_find_clause (gimple_omp_single_clauses (single_stmt),
    9243         1146 :                                  OMP_CLAUSE_NOWAIT) != NULL_TREE;
    9244         1146 :   gimple *g = gimple_build_omp_return (nowait);
    9245         1146 :   gimple_seq_add_stmt (&bind_body_tail, g);
    9246         1146 :   maybe_add_implicit_barrier_cancel (ctx, g, &bind_body_tail);
    9247              : 
    9248         1146 :   if (flag_openmp_ompt && !ctx->record_type)
    9249              :     {
    9250              :       /* Insert call to GOMP_single_end.  */
    9251            4 :       tree decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_END);
    9252            4 :       gimple *stmt = gimple_build_call (decl, 0);
    9253            4 :       gimple_seq_add_stmt (&bind_body_tail, stmt);
    9254              :     }
    9255              : 
    9256         1146 :   if (ctx->record_type)
    9257              :     {
    9258          118 :       gimple_stmt_iterator gsi = gsi_start (bind_body_tail);
    9259          118 :       tree clobber = build_clobber (ctx->record_type);
    9260          118 :       gsi_insert_after (&gsi, gimple_build_assign (ctx->sender_decl,
    9261              :                                                    clobber), GSI_SAME_STMT);
    9262              :     }
    9263         1146 :   gimple_seq_add_seq (&bind_body, bind_body_tail);
    9264         1146 :   gimple_bind_set_body (bind, bind_body);
    9265              : 
    9266         1146 :   pop_gimplify_context (bind);
    9267              : 
    9268         1146 :   gimple_bind_append_vars (bind, ctx->block_vars);
    9269         1146 :   BLOCK_VARS (block) = ctx->block_vars;
    9270         1146 :   if (BLOCK_VARS (block))
    9271           26 :     TREE_USED (block) = 1;
    9272         1146 : }
    9273              : 
    9274              : 
    9275              : /* Lower code for an OMP scope directive.  */
    9276              : 
    9277              : static void
    9278          161 : lower_omp_scope (gimple_stmt_iterator *gsi_p, omp_context *ctx)
    9279              : {
    9280          161 :   tree block;
    9281          161 :   gimple *scope_stmt = gsi_stmt (*gsi_p);
    9282          161 :   gbind *bind;
    9283          161 :   gimple_seq bind_body, bind_body_tail = NULL, dlist;
    9284          161 :   gimple_seq tred_dlist = NULL;
    9285              : 
    9286          161 :   push_gimplify_context ();
    9287              : 
    9288          161 :   block = make_node (BLOCK);
    9289          161 :   bind = gimple_build_bind (NULL, NULL, block);
    9290          161 :   gsi_replace (gsi_p, bind, true);
    9291          161 :   bind_body = NULL;
    9292          161 :   dlist = NULL;
    9293              : 
    9294          161 :   tree rclauses
    9295          161 :     = omp_task_reductions_find_first (gimple_omp_scope_clauses (scope_stmt),
    9296              :                                       OMP_SCOPE, OMP_CLAUSE_REDUCTION);
    9297          161 :   if (rclauses)
    9298              :     {
    9299           54 :       tree type = build_pointer_type (pointer_sized_int_node);
    9300           54 :       tree temp = create_tmp_var (type);
    9301           54 :       tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
    9302           54 :       OMP_CLAUSE_DECL (c) = temp;
    9303           54 :       OMP_CLAUSE_CHAIN (c) = gimple_omp_scope_clauses (scope_stmt);
    9304           54 :       gimple_omp_scope_set_clauses (scope_stmt, c);
    9305           54 :       lower_omp_task_reductions (ctx, OMP_SCOPE,
    9306              :                                  gimple_omp_scope_clauses (scope_stmt),
    9307              :                                  &bind_body, &tred_dlist);
    9308           54 :       rclauses = c;
    9309           54 :       tree fndecl = builtin_decl_explicit (
    9310           54 :         flag_openmp_ompt ? BUILT_IN_GOMP_SCOPE_START_WITH_END
    9311              :                          : BUILT_IN_GOMP_SCOPE_START);
    9312           54 :       gimple *stmt = gimple_build_call (fndecl, 1, temp);
    9313           54 :       gimple_seq_add_stmt (&bind_body, stmt);
    9314              :     }
    9315          107 :   else if (flag_openmp_ompt)
    9316              :     {
    9317            4 :       tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_SCOPE_START_WITH_END);
    9318            4 :       gimple *stmt = gimple_build_call (fndecl, 1, null_pointer_node);
    9319            4 :       gimple_seq_add_stmt (&bind_body, stmt);
    9320              :     }
    9321              : 
    9322          161 :   lower_rec_input_clauses (gimple_omp_scope_clauses (scope_stmt),
    9323              :                            &bind_body, &dlist, ctx, NULL);
    9324          161 :   lower_omp (gimple_omp_body_ptr (scope_stmt), ctx);
    9325              : 
    9326          161 :   gimple_seq_add_stmt (&bind_body, scope_stmt);
    9327              : 
    9328          161 :   gimple_seq_add_seq (&bind_body, gimple_omp_body (scope_stmt));
    9329              : 
    9330          161 :   gimple_omp_set_body (scope_stmt, NULL);
    9331              : 
    9332          161 :   gimple_seq clist = NULL;
    9333          161 :   lower_reduction_clauses (gimple_omp_scope_clauses (scope_stmt),
    9334              :                            &bind_body, &clist, ctx);
    9335          161 :   if (clist)
    9336              :     {
    9337            0 :       tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_REDUCTION_START);
    9338            0 :       gcall *g = gimple_build_call (fndecl, 0);
    9339            0 :       gimple_seq_add_stmt (&bind_body, g);
    9340            0 :       gimple_seq_add_seq (&bind_body, clist);
    9341            0 :       fndecl = builtin_decl_explicit (BUILT_IN_GOMP_REDUCTION_END);
    9342            0 :       g = gimple_build_call (fndecl, 0);
    9343            0 :       gimple_seq_add_stmt (&bind_body, g);
    9344              :     }
    9345              : 
    9346          161 :   gimple_seq_add_seq (&bind_body, dlist);
    9347              : 
    9348          161 :   bind_body = maybe_catch_exception (bind_body);
    9349              : 
    9350          161 :   if (flag_openmp_ompt)
    9351              :     {
    9352            8 :       tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_SCOPE_END);
    9353            8 :       gcall *g = gimple_build_call (fndecl, 0);
    9354            8 :       gimple_seq_add_stmt (&bind_body_tail, g);
    9355              :     }
    9356              : 
    9357          161 :   bool nowait = omp_find_clause (gimple_omp_scope_clauses (scope_stmt),
    9358          161 :                                  OMP_CLAUSE_NOWAIT) != NULL_TREE;
    9359          161 :   gimple *g = gimple_build_omp_return (nowait);
    9360          161 :   gimple_seq_add_stmt (&bind_body_tail, g);
    9361          161 :   gimple_seq_add_seq (&bind_body_tail, tred_dlist);
    9362          161 :   maybe_add_implicit_barrier_cancel (ctx, g, &bind_body_tail);
    9363          161 :   if (ctx->record_type)
    9364              :     {
    9365            0 :       gimple_stmt_iterator gsi = gsi_start (bind_body_tail);
    9366            0 :       tree clobber = build_clobber (ctx->record_type);
    9367            0 :       gsi_insert_after (&gsi, gimple_build_assign (ctx->sender_decl,
    9368              :                                                    clobber), GSI_SAME_STMT);
    9369              :     }
    9370          161 :   gimple_seq_add_seq (&bind_body, bind_body_tail);
    9371              : 
    9372          161 :   gimple_bind_set_body (bind, bind_body);
    9373              : 
    9374          161 :   pop_gimplify_context (bind);
    9375              : 
    9376          161 :   gimple_bind_append_vars (bind, ctx->block_vars);
    9377          161 :   BLOCK_VARS (block) = ctx->block_vars;
    9378          161 :   if (BLOCK_VARS (block))
    9379          121 :     TREE_USED (block) = 1;
    9380          161 : }
    9381              : 
    9382              : /* Lower code for an OMP dispatch directive.  */
    9383              : 
    9384              : static void
    9385          659 : lower_omp_dispatch (gimple_stmt_iterator *gsi_p, omp_context *ctx)
    9386              : {
    9387          659 :   tree block;
    9388          659 :   gimple *stmt = gsi_stmt (*gsi_p);
    9389          659 :   gbind *bind;
    9390              : 
    9391          659 :   push_gimplify_context ();
    9392              : 
    9393          659 :   block = make_node (BLOCK);
    9394          659 :   bind = gimple_build_bind (NULL, NULL, block);
    9395          659 :   gsi_replace (gsi_p, bind, true);
    9396              : 
    9397          659 :   lower_omp (gimple_omp_body_ptr (stmt), ctx);
    9398          659 :   gimple_bind_set_body (bind, maybe_catch_exception (gimple_omp_body (stmt)));
    9399              : 
    9400          659 :   pop_gimplify_context (bind);
    9401              : 
    9402          659 :   gimple_bind_append_vars (bind, ctx->block_vars);
    9403          659 :   BLOCK_VARS (block) = ctx->block_vars;
    9404          659 : }
    9405              : 
    9406              : /* Expand code for an OpenMP master or masked directive.  */
    9407              : 
    9408              : static void
    9409         1127 : lower_omp_master (gimple_stmt_iterator *gsi_p, omp_context *ctx)
    9410              : {
    9411         1127 :   tree block, lab = NULL, x, bfn_decl;
    9412         1127 :   gimple *stmt = gsi_stmt (*gsi_p);
    9413         1127 :   gbind *bind;
    9414         1127 :   location_t loc = gimple_location (stmt);
    9415         1127 :   gimple_seq tseq;
    9416         1127 :   tree filter = integer_zero_node;
    9417              : 
    9418         1127 :   push_gimplify_context ();
    9419              : 
    9420         1127 :   if (gimple_code (stmt) == GIMPLE_OMP_MASKED)
    9421              :     {
    9422          393 :       filter = omp_find_clause (gimple_omp_masked_clauses (stmt),
    9423              :                                 OMP_CLAUSE_FILTER);
    9424          393 :       if (filter)
    9425          252 :         filter = fold_convert (integer_type_node,
    9426              :                                OMP_CLAUSE_FILTER_EXPR (filter));
    9427              :       else
    9428          141 :         filter = integer_zero_node;
    9429              :     }
    9430         1127 :   block = make_node (BLOCK);
    9431         1127 :   bind = gimple_build_bind (NULL, NULL, block);
    9432         1127 :   gsi_replace (gsi_p, bind, true);
    9433         1127 :   gimple_bind_add_stmt (bind, stmt);
    9434              : 
    9435         1127 :   bfn_decl = builtin_decl_explicit (
    9436         1127 :     flag_openmp_ompt ? BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM_WITH_END
    9437              :                      : BUILT_IN_GOMP_HAS_MASKED_THREAD_NUM);
    9438         1127 :   x = build_call_expr_loc (loc, bfn_decl, 1, filter);
    9439         1127 :   x = build3 (COND_EXPR, void_type_node, x, NULL, build_and_jump (&lab));
    9440         1127 :   tseq = NULL;
    9441         1127 :   gimplify_and_add (x, &tseq);
    9442         1127 :   gimple_bind_add_seq (bind, tseq);
    9443              : 
    9444         1127 :   lower_omp (gimple_omp_body_ptr (stmt), ctx);
    9445         1127 :   gimple_omp_set_body (stmt, maybe_catch_exception (gimple_omp_body (stmt)));
    9446         1127 :   gimple_bind_add_seq (bind, gimple_omp_body (stmt));
    9447         1127 :   gimple_omp_set_body (stmt, NULL);
    9448              : 
    9449         1127 :   if (flag_openmp_ompt)
    9450              :     {
    9451              :       /* Insert call to GOMP_masked_end at the end of the body.  */
    9452            4 :       tree decl = builtin_decl_explicit (BUILT_IN_GOMP_MASKED_END);
    9453            4 :       gcall *g = gimple_build_call (decl, 0);
    9454            4 :       gimple_bind_add_stmt (bind, g);
    9455              :     }
    9456              : 
    9457         1127 :   gimple_bind_add_stmt (bind, gimple_build_label (lab));
    9458              : 
    9459         1127 :   gimple_bind_add_stmt (bind, gimple_build_omp_return (true));
    9460              : 
    9461         1127 :   pop_gimplify_context (bind);
    9462              : 
    9463         1127 :   gimple_bind_append_vars (bind, ctx->block_vars);
    9464         1127 :   BLOCK_VARS (block) = ctx->block_vars;
    9465         1127 : }
    9466              : 
    9467              : /* Helper function for lower_omp_task_reductions.  For a specific PASS
    9468              :    find out the current clause it should be processed, or return false
    9469              :    if all have been processed already.  */
    9470              : 
    9471              : static inline bool
    9472         8030 : omp_task_reduction_iterate (int pass, enum tree_code code,
    9473              :                             enum omp_clause_code ccode, tree *c, tree *decl,
    9474              :                             tree *type, tree *next)
    9475              : {
    9476        11412 :   for (; *c; *c = omp_find_clause (OMP_CLAUSE_CHAIN (*c), ccode))
    9477              :     {
    9478         6764 :       if (ccode == OMP_CLAUSE_REDUCTION
    9479         6708 :           && code != OMP_TASKLOOP
    9480         6708 :           && !OMP_CLAUSE_REDUCTION_TASK (*c))
    9481           56 :         continue;
    9482         6652 :       *decl = OMP_CLAUSE_DECL (*c);
    9483         6652 :       *type = TREE_TYPE (*decl);
    9484         6652 :       if (TREE_CODE (*decl) == MEM_REF)
    9485              :         {
    9486         1940 :           if (pass != 1)
    9487          970 :             continue;
    9488              :         }
    9489              :       else
    9490              :         {
    9491         4712 :           if (omp_privatize_by_reference (*decl))
    9492          168 :             *type = TREE_TYPE (*type);
    9493         4712 :           if (pass != (!TREE_CONSTANT (TYPE_SIZE_UNIT (*type))))
    9494         2356 :             continue;
    9495              :         }
    9496         3326 :       *next = omp_find_clause (OMP_CLAUSE_CHAIN (*c), ccode);
    9497         3326 :       return true;
    9498              :     }
    9499         4704 :   *decl = NULL_TREE;
    9500         4704 :   *type = NULL_TREE;
    9501         4704 :   *next = NULL_TREE;
    9502         4704 :   return false;
    9503              : }
    9504              : 
    9505              : /* Lower task_reduction and reduction clauses (the latter unless CODE is
    9506              :    OMP_TASKGROUP only with task modifier).  Register mapping of those in
    9507              :    START sequence and reducing them and unregister them in the END sequence.  */
    9508              : 
    9509              : static void
    9510         1385 : lower_omp_task_reductions (omp_context *ctx, enum tree_code code, tree clauses,
    9511              :                            gimple_seq *start, gimple_seq *end)
    9512              : {
    9513          849 :   enum omp_clause_code ccode
    9514              :     = (code == OMP_TASKGROUP
    9515         1385 :        ? OMP_CLAUSE_TASK_REDUCTION : OMP_CLAUSE_REDUCTION);
    9516         1385 :   tree cancellable = NULL_TREE;
    9517         1385 :   clauses = omp_task_reductions_find_first (clauses, code, ccode);
    9518         1385 :   if (clauses == NULL_TREE)
    9519          209 :     return;
    9520         1176 :   if (code == OMP_FOR || code == OMP_SECTIONS || code == OMP_SCOPE)
    9521              :     {
    9522          325 :       for (omp_context *outer = ctx->outer; outer; outer = outer->outer)
    9523          239 :         if (gimple_code (outer->stmt) == GIMPLE_OMP_PARALLEL
    9524          239 :             && outer->cancellable)
    9525              :           {
    9526           14 :             cancellable = error_mark_node;
    9527           14 :             break;
    9528              :           }
    9529          225 :         else if (gimple_code (outer->stmt) != GIMPLE_OMP_TASKGROUP
    9530          225 :                  && gimple_code (outer->stmt) != GIMPLE_OMP_SCOPE)
    9531              :           break;
    9532              :     }
    9533         1176 :   tree record_type = lang_hooks.types.make_type (RECORD_TYPE);
    9534         1176 :   tree *last = &TYPE_FIELDS (record_type);
    9535         1176 :   unsigned cnt = 0;
    9536         1176 :   if (cancellable)
    9537              :     {
    9538           14 :       tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
    9539              :                                ptr_type_node);
    9540           14 :       tree ifield = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
    9541              :                                 integer_type_node);
    9542           14 :       *last = field;
    9543           14 :       DECL_CHAIN (field) = ifield;
    9544           14 :       last = &DECL_CHAIN (ifield);
    9545           14 :       DECL_CONTEXT (field) = record_type;
    9546           14 :       if (TYPE_ALIGN (record_type) < DECL_ALIGN (field))
    9547            0 :         SET_TYPE_ALIGN (record_type, DECL_ALIGN (field));
    9548           14 :       DECL_CONTEXT (ifield) = record_type;
    9549           14 :       if (TYPE_ALIGN (record_type) < DECL_ALIGN (ifield))
    9550            0 :         SET_TYPE_ALIGN (record_type, DECL_ALIGN (ifield));
    9551              :     }
    9552         3528 :   for (int pass = 0; pass < 2; pass++)
    9553              :     {
    9554         2352 :       tree decl, type, next;
    9555         2352 :       for (tree c = clauses;
    9556         4015 :            omp_task_reduction_iterate (pass, code, ccode,
    9557         1663 :                                        &c, &decl, &type, &next); c = next)
    9558              :         {
    9559         1663 :           ++cnt;
    9560         1663 :           tree new_type = type;
    9561         1663 :           if (ctx->outer)
    9562         1166 :             new_type = remap_type (type, &ctx->outer->cb);
    9563         1663 :           tree field
    9564         1663 :             = build_decl (OMP_CLAUSE_LOCATION (c), FIELD_DECL,
    9565         1663 :                           DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE,
    9566              :                           new_type);
    9567         1663 :           if (DECL_P (decl) && type == TREE_TYPE (decl))
    9568              :             {
    9569         1136 :               SET_DECL_ALIGN (field, DECL_ALIGN (decl));
    9570         1136 :               DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
    9571         1136 :               TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
    9572              :             }
    9573              :           else
    9574          527 :             SET_DECL_ALIGN (field, TYPE_ALIGN (type));
    9575         1663 :           DECL_CONTEXT (field) = record_type;
    9576         1663 :           if (TYPE_ALIGN (record_type) < DECL_ALIGN (field))
    9577         1200 :             SET_TYPE_ALIGN (record_type, DECL_ALIGN (field));
    9578         1663 :           *last = field;
    9579         1663 :           last = &DECL_CHAIN (field);
    9580         1663 :           tree bfield
    9581         1663 :             = build_decl (OMP_CLAUSE_LOCATION (c), FIELD_DECL, NULL_TREE,
    9582              :                           boolean_type_node);
    9583         1663 :           DECL_CONTEXT (bfield) = record_type;
    9584         1663 :           if (TYPE_ALIGN (record_type) < DECL_ALIGN (bfield))
    9585            0 :             SET_TYPE_ALIGN (record_type, DECL_ALIGN (bfield));
    9586         1663 :           *last = bfield;
    9587         1663 :           last = &DECL_CHAIN (bfield);
    9588              :         }
    9589              :     }
    9590         1176 :   *last = NULL_TREE;
    9591         1176 :   layout_type (record_type);
    9592              : 
    9593              :   /* Build up an array which registers with the runtime all the reductions
    9594              :      and deregisters them at the end.  Format documented in libgomp/task.c.  */
    9595         1176 :   tree atype = build_array_type_nelts (pointer_sized_int_node, 7 + cnt * 3);
    9596         1176 :   tree avar = create_tmp_var_raw (atype);
    9597         1176 :   gimple_add_tmp_var (avar);
    9598         1176 :   TREE_ADDRESSABLE (avar) = 1;
    9599         1176 :   tree r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_zero_node,
    9600              :                    NULL_TREE, NULL_TREE);
    9601         1176 :   tree t = build_int_cst (pointer_sized_int_node, cnt);
    9602         1176 :   gimple_seq_add_stmt (start, gimple_build_assign (r, t));
    9603         1176 :   gimple_seq seq = NULL;
    9604         1176 :   tree sz = fold_convert (pointer_sized_int_node,
    9605              :                           TYPE_SIZE_UNIT (record_type));
    9606         1176 :   int cachesz = 64;
    9607         1176 :   sz = fold_build2 (PLUS_EXPR, pointer_sized_int_node, sz,
    9608              :                     build_int_cst (pointer_sized_int_node, cachesz - 1));
    9609         1176 :   sz = fold_build2 (BIT_AND_EXPR, pointer_sized_int_node, sz,
    9610              :                     build_int_cst (pointer_sized_int_node, ~(cachesz - 1)));
    9611         1176 :   ctx->task_reductions.create (1 + cnt);
    9612         1176 :   ctx->task_reduction_map = new hash_map<tree, unsigned>;
    9613         2352 :   ctx->task_reductions.quick_push (TREE_CODE (sz) == INTEGER_CST
    9614         1216 :                                    ? sz : NULL_TREE);
    9615         1176 :   sz = force_gimple_operand (sz, &seq, true, NULL_TREE);
    9616         1176 :   gimple_seq_add_seq (start, seq);
    9617         1176 :   r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_one_node,
    9618              :               NULL_TREE, NULL_TREE);
    9619         1176 :   gimple_seq_add_stmt (start, gimple_build_assign (r, sz));
    9620         1176 :   r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (2),
    9621              :               NULL_TREE, NULL_TREE);
    9622         1176 :   t = build_int_cst (pointer_sized_int_node,
    9623         1176 :                      MAX (TYPE_ALIGN_UNIT (record_type), (unsigned) cachesz));
    9624         1176 :   gimple_seq_add_stmt (start, gimple_build_assign (r, t));
    9625         1176 :   r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (3),
    9626              :               NULL_TREE, NULL_TREE);
    9627         1176 :   t = build_int_cst (pointer_sized_int_node, -1);
    9628         1176 :   gimple_seq_add_stmt (start, gimple_build_assign (r, t));
    9629         1176 :   r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (4),
    9630              :               NULL_TREE, NULL_TREE);
    9631         1176 :   t = build_int_cst (pointer_sized_int_node, 0);
    9632         1176 :   gimple_seq_add_stmt (start, gimple_build_assign (r, t));
    9633              : 
    9634              :   /* In end, build a loop that iterates from 0 to < omp_get_num_threads ()
    9635              :      and for each task reduction checks a bool right after the private variable
    9636              :      within that thread's chunk; if the bool is clear, it hasn't been
    9637              :      initialized and thus isn't going to be reduced nor destructed, otherwise
    9638              :      reduce and destruct it.  */
    9639         1176 :   tree idx = create_tmp_var (size_type_node);
    9640         1176 :   gimple_seq_add_stmt (end, gimple_build_assign (idx, size_zero_node));
    9641         1176 :   tree num_thr_sz = create_tmp_var (size_type_node);
    9642         1176 :   tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
    9643         1176 :   tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
    9644         1176 :   tree lab3 = NULL_TREE, lab7 = NULL_TREE;
    9645         1176 :   gimple *g;
    9646         1176 :   if (code == OMP_FOR || code == OMP_SECTIONS || code == OMP_SCOPE)
    9647              :     {
    9648              :       /* For worksharing constructs or scope, only perform it in the master
    9649              :          thread, with the exception of cancelled implicit barriers - then only
    9650              :          handle the current thread.  */
    9651          290 :       tree lab4 = create_artificial_label (UNKNOWN_LOCATION);
    9652          290 :       t = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
    9653          290 :       tree thr_num = create_tmp_var (integer_type_node);
    9654          290 :       g = gimple_build_call (t, 0);
    9655          290 :       gimple_call_set_lhs (g, thr_num);
    9656          290 :       gimple_seq_add_stmt (end, g);
    9657          290 :       if (cancellable)
    9658              :         {
    9659           14 :           tree c;
    9660           14 :           tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
    9661           14 :           tree lab6 = create_artificial_label (UNKNOWN_LOCATION);
    9662           14 :           lab3 = create_artificial_label (UNKNOWN_LOCATION);
    9663           14 :           if (code == OMP_FOR)
    9664            4 :             c = gimple_omp_for_clauses (ctx->stmt);
    9665           10 :           else if (code == OMP_SECTIONS)
    9666            0 :             c = gimple_omp_sections_clauses (ctx->stmt);
    9667              :           else /* if (code == OMP_SCOPE) */
    9668           10 :             c = gimple_omp_scope_clauses (ctx->stmt);
    9669           14 :           c = OMP_CLAUSE_DECL (omp_find_clause (c, OMP_CLAUSE__REDUCTEMP_));
    9670           14 :           cancellable = c;
    9671           14 :           g = gimple_build_cond (NE_EXPR, c, build_zero_cst (TREE_TYPE (c)),
    9672              :                                  lab5, lab6);
    9673           14 :           gimple_seq_add_stmt (end, g);
    9674           14 :           gimple_seq_add_stmt (end, gimple_build_label (lab5));
    9675           14 :           g = gimple_build_assign (idx, NOP_EXPR, thr_num);
    9676           14 :           gimple_seq_add_stmt (end, g);
    9677           14 :           g = gimple_build_assign (num_thr_sz, PLUS_EXPR, idx,
    9678           14 :                                    build_one_cst (TREE_TYPE (idx)));
    9679           14 :           gimple_seq_add_stmt (end, g);
    9680           14 :           gimple_seq_add_stmt (end, gimple_build_goto (lab3));
    9681           14 :           gimple_seq_add_stmt (end, gimple_build_label (lab6));
    9682              :         }
    9683          290 :       g = gimple_build_cond (NE_EXPR, thr_num, integer_zero_node, lab2, lab4);
    9684          290 :       gimple_seq_add_stmt (end, g);
    9685          290 :       gimple_seq_add_stmt (end, gimple_build_label (lab4));
    9686              :     }
    9687         1176 :   if (code != OMP_PARALLEL)
    9688              :     {
    9689         1114 :       t = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
    9690         1114 :       tree num_thr = create_tmp_var (integer_type_node);
    9691         1114 :       g = gimple_build_call (t, 0);
    9692         1114 :       gimple_call_set_lhs (g, num_thr);
    9693         1114 :       gimple_seq_add_stmt (end, g);
    9694         1114 :       g = gimple_build_assign (num_thr_sz, NOP_EXPR, num_thr);
    9695         1114 :       gimple_seq_add_stmt (end, g);
    9696         1114 :       if (cancellable)
    9697           14 :         gimple_seq_add_stmt (end, gimple_build_label (lab3));
    9698              :     }
    9699              :   else
    9700              :     {
    9701           62 :       tree c = omp_find_clause (gimple_omp_parallel_clauses (ctx->stmt),
    9702              :                                 OMP_CLAUSE__REDUCTEMP_);
    9703           62 :       t = fold_convert (pointer_sized_int_node, OMP_CLAUSE_DECL (c));
    9704           62 :       t = fold_convert (size_type_node, t);
    9705           62 :       gimplify_assign (num_thr_sz, t, end);
    9706              :     }
    9707         1176 :   t = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (2),
    9708              :               NULL_TREE, NULL_TREE);
    9709         1176 :   tree data = create_tmp_var (pointer_sized_int_node);
    9710         1176 :   gimple_seq_add_stmt (end, gimple_build_assign (data, t));
    9711         1176 :   if (code == OMP_TASKLOOP)
    9712              :     {
    9713          497 :       lab7 = create_artificial_label (UNKNOWN_LOCATION);
    9714          497 :       g = gimple_build_cond (NE_EXPR, data,
    9715              :                              build_zero_cst (pointer_sized_int_node),
    9716              :                              lab1, lab7);
    9717          497 :       gimple_seq_add_stmt (end, g);
    9718              :     }
    9719         1176 :   gimple_seq_add_stmt (end, gimple_build_label (lab1));
    9720         1176 :   tree ptr;
    9721         1176 :   if (TREE_CODE (TYPE_SIZE_UNIT (record_type)) == INTEGER_CST)
    9722         1136 :     ptr = create_tmp_var (build_pointer_type (record_type));
    9723              :   else
    9724           40 :     ptr = create_tmp_var (ptr_type_node);
    9725         1176 :   gimple_seq_add_stmt (end, gimple_build_assign (ptr, NOP_EXPR, data));
    9726              : 
    9727         1176 :   tree field = TYPE_FIELDS (record_type);
    9728         1176 :   cnt = 0;
    9729         1176 :   if (cancellable)
    9730           14 :     field = DECL_CHAIN (DECL_CHAIN (field));
    9731         3528 :   for (int pass = 0; pass < 2; pass++)
    9732              :     {
    9733         2352 :       tree decl, type, next;
    9734         2352 :       for (tree c = clauses;
    9735         4015 :            omp_task_reduction_iterate (pass, code, ccode,
    9736         1663 :                                        &c, &decl, &type, &next); c = next)
    9737              :         {
    9738         1663 :           tree var = decl, ref;
    9739         1663 :           if (TREE_CODE (decl) == MEM_REF)
    9740              :             {
    9741          485 :               var = TREE_OPERAND (var, 0);
    9742          485 :               if (TREE_CODE (var) == POINTER_PLUS_EXPR)
    9743          108 :                 var = TREE_OPERAND (var, 0);
    9744          485 :               tree v = var;
    9745          485 :               if (TREE_CODE (var) == ADDR_EXPR)
    9746          310 :                 var = TREE_OPERAND (var, 0);
    9747          175 :               else if (INDIRECT_REF_P (var))
    9748            4 :                 var = TREE_OPERAND (var, 0);
    9749          485 :               tree orig_var = var;
    9750          485 :               if (is_variable_sized (var))
    9751              :                 {
    9752           31 :                   gcc_assert (DECL_HAS_VALUE_EXPR_P (var));
    9753           31 :                   var = DECL_VALUE_EXPR (var);
    9754           31 :                   gcc_assert (INDIRECT_REF_P (var));
    9755           31 :                   var = TREE_OPERAND (var, 0);
    9756           31 :                   gcc_assert (DECL_P (var));
    9757              :                 }
    9758          485 :               t = ref = maybe_lookup_decl_in_outer_ctx (var, ctx);
    9759          485 :               if (orig_var != var)
    9760           31 :                 gcc_assert (TREE_CODE (v) == ADDR_EXPR);
    9761          454 :               else if (TREE_CODE (v) == ADDR_EXPR)
    9762          279 :                 t = build_fold_addr_expr (t);
    9763          175 :               else if (INDIRECT_REF_P (v))
    9764            4 :                 t = build_fold_indirect_ref (t);
    9765          485 :               if (TREE_CODE (TREE_OPERAND (decl, 0)) == POINTER_PLUS_EXPR)
    9766              :                 {
    9767          108 :                   tree b = TREE_OPERAND (TREE_OPERAND (decl, 0), 1);
    9768          108 :                   b = maybe_lookup_decl_in_outer_ctx (b, ctx);
    9769          108 :                   t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t, b);
    9770              :                 }
    9771          485 :               if (!integer_zerop (TREE_OPERAND (decl, 1)))
    9772          219 :                 t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
    9773              :                                  fold_convert (size_type_node,
    9774              :                                                TREE_OPERAND (decl, 1)));
    9775              :             }
    9776              :           else
    9777              :             {
    9778         1178 :               t = ref = maybe_lookup_decl_in_outer_ctx (var, ctx);
    9779         1178 :               if (!omp_privatize_by_reference (decl))
    9780         1136 :                 t = build_fold_addr_expr (t);
    9781              :             }
    9782         1663 :           t = fold_convert (pointer_sized_int_node, t);
    9783         1663 :           seq = NULL;
    9784         1663 :           t = force_gimple_operand (t, &seq, true, NULL_TREE);
    9785         1663 :           gimple_seq_add_seq (start, seq);
    9786         1663 :           r = build4 (ARRAY_REF, pointer_sized_int_node, avar,
    9787         1663 :                       size_int (7 + cnt * 3), NULL_TREE, NULL_TREE);
    9788         1663 :           gimple_seq_add_stmt (start, gimple_build_assign (r, t));
    9789         1663 :           t = unshare_expr (byte_position (field));
    9790         1663 :           t = fold_convert (pointer_sized_int_node, t);
    9791         1663 :           ctx->task_reduction_map->put (c, cnt);
    9792         3326 :           ctx->task_reductions.quick_push (TREE_CODE (t) == INTEGER_CST
    9793         2035 :                                            ? t : NULL_TREE);
    9794         1663 :           seq = NULL;
    9795         1663 :           t = force_gimple_operand (t, &seq, true, NULL_TREE);
    9796         1663 :           gimple_seq_add_seq (start, seq);
    9797         1663 :           r = build4 (ARRAY_REF, pointer_sized_int_node, avar,
    9798         1663 :                       size_int (7 + cnt * 3 + 1), NULL_TREE, NULL_TREE);
    9799         1663 :           gimple_seq_add_stmt (start, gimple_build_assign (r, t));
    9800              : 
    9801         1663 :           tree bfield = DECL_CHAIN (field);
    9802         1663 :           tree cond;
    9803         1663 :           if (code == OMP_PARALLEL
    9804         1663 :               || code == OMP_FOR
    9805              :               || code == OMP_SECTIONS
    9806              :               || code == OMP_SCOPE)
    9807              :             /* In parallel, worksharing or scope all threads unconditionally
    9808              :                initialize all their task reduction private variables.  */
    9809          565 :             cond = boolean_true_node;
    9810         1098 :           else if (TREE_TYPE (ptr) == ptr_type_node)
    9811              :             {
    9812          261 :               cond = build2 (POINTER_PLUS_EXPR, ptr_type_node, ptr,
    9813              :                              unshare_expr (byte_position (bfield)));
    9814          261 :               seq = NULL;
    9815          261 :               cond = force_gimple_operand (cond, &seq, true, NULL_TREE);
    9816          261 :               gimple_seq_add_seq (end, seq);
    9817          261 :               tree pbool = build_pointer_type (TREE_TYPE (bfield));
    9818          261 :               cond = build2 (MEM_REF, TREE_TYPE (bfield), cond,
    9819              :                              build_int_cst (pbool, 0));
    9820              :             }
    9821              :           else
    9822          837 :             cond = build3 (COMPONENT_REF, TREE_TYPE (bfield),
    9823              :                            build_simple_mem_ref (ptr), bfield, NULL_TREE);
    9824         1663 :           tree lab3 = create_artificial_label (UNKNOWN_LOCATION);
    9825         1663 :           tree lab4 = create_artificial_label (UNKNOWN_LOCATION);
    9826         1663 :           tree condv = create_tmp_var (boolean_type_node);
    9827         1663 :           gimple_seq_add_stmt (end, gimple_build_assign (condv, cond));
    9828         1663 :           g = gimple_build_cond (NE_EXPR, condv, boolean_false_node,
    9829              :                                  lab3, lab4);
    9830         1663 :           gimple_seq_add_stmt (end, g);
    9831         1663 :           gimple_seq_add_stmt (end, gimple_build_label (lab3));
    9832         1663 :           if (cancellable && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE)
    9833              :             {
    9834              :               /* If this reduction doesn't need destruction and parallel
    9835              :                  has been cancelled, there is nothing to do for this
    9836              :                  reduction, so jump around the merge operation.  */
    9837           18 :               tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
    9838           18 :               g = gimple_build_cond (NE_EXPR, cancellable,
    9839           18 :                                      build_zero_cst (TREE_TYPE (cancellable)),
    9840              :                                      lab4, lab5);
    9841           18 :               gimple_seq_add_stmt (end, g);
    9842           18 :               gimple_seq_add_stmt (end, gimple_build_label (lab5));
    9843              :             }
    9844              : 
    9845         1663 :           tree new_var;
    9846         1663 :           if (TREE_TYPE (ptr) == ptr_type_node)
    9847              :             {
    9848          428 :               new_var = build2 (POINTER_PLUS_EXPR, ptr_type_node, ptr,
    9849              :                                 unshare_expr (byte_position (field)));
    9850          428 :               seq = NULL;
    9851          428 :               new_var = force_gimple_operand (new_var, &seq, true, NULL_TREE);
    9852          428 :               gimple_seq_add_seq (end, seq);
    9853          428 :               tree pbool = build_pointer_type (TREE_TYPE (field));
    9854          428 :               new_var = build2 (MEM_REF, TREE_TYPE (field), new_var,
    9855              :                                 build_int_cst (pbool, 0));
    9856              :             }
    9857              :           else
    9858         1235 :             new_var = build3 (COMPONENT_REF, TREE_TYPE (field),
    9859              :                               build_simple_mem_ref (ptr), field, NULL_TREE);
    9860              : 
    9861         1663 :           enum tree_code rcode = OMP_CLAUSE_REDUCTION_CODE (c);
    9862         1663 :           if (TREE_CODE (decl) != MEM_REF
    9863         1663 :               && omp_privatize_by_reference (decl))
    9864           42 :             ref = build_simple_mem_ref (ref);
    9865              :           /* reduction(-:var) sums up the partial results, so it acts
    9866              :              identically to reduction(+:var).  */
    9867         1663 :           if (rcode == MINUS_EXPR)
    9868            0 :             rcode = PLUS_EXPR;
    9869         1663 :           if (TREE_CODE (decl) == MEM_REF)
    9870              :             {
    9871          485 :               tree type = TREE_TYPE (new_var);
    9872          485 :               tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    9873          485 :               tree i = create_tmp_var (TREE_TYPE (v));
    9874          485 :               tree ptype = build_pointer_type (TREE_TYPE (type));
    9875          485 :               if (DECL_P (v))
    9876              :                 {
    9877          142 :                   v = maybe_lookup_decl_in_outer_ctx (v, ctx);
    9878          142 :                   tree vv = create_tmp_var (TREE_TYPE (v));
    9879          142 :                   gimplify_assign (vv, v, start);
    9880          142 :                   v = vv;
    9881              :                 }
    9882          485 :               ref = build4 (ARRAY_REF, pointer_sized_int_node, avar,
    9883          485 :                             size_int (7 + cnt * 3), NULL_TREE, NULL_TREE);
    9884          485 :               new_var = build_fold_addr_expr (new_var);
    9885          485 :               new_var = fold_convert (ptype, new_var);
    9886          485 :               ref = fold_convert (ptype, ref);
    9887          485 :               tree m = create_tmp_var (ptype);
    9888          485 :               gimplify_assign (m, new_var, end);
    9889          485 :               new_var = m;
    9890          485 :               m = create_tmp_var (ptype);
    9891          485 :               gimplify_assign (m, ref, end);
    9892          485 :               ref = m;
    9893          485 :               gimplify_assign (i, build_int_cst (TREE_TYPE (v), 0), end);
    9894          485 :               tree body = create_artificial_label (UNKNOWN_LOCATION);
    9895          485 :               tree endl = create_artificial_label (UNKNOWN_LOCATION);
    9896          485 :               gimple_seq_add_stmt (end, gimple_build_label (body));
    9897          485 :               tree priv = build_simple_mem_ref (new_var);
    9898          485 :               tree out = build_simple_mem_ref (ref);
    9899          485 :               if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    9900              :                 {
    9901          161 :                   tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
    9902          161 :                   tree decl_placeholder
    9903          161 :                     = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c);
    9904          161 :                   tree lab6 = NULL_TREE;
    9905          161 :                   if (cancellable)
    9906              :                     {
    9907              :                       /* If this reduction needs destruction and parallel
    9908              :                          has been cancelled, jump around the merge operation
    9909              :                          to the destruction.  */
    9910            4 :                       tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
    9911            4 :                       lab6 = create_artificial_label (UNKNOWN_LOCATION);
    9912            4 :                       tree zero = build_zero_cst (TREE_TYPE (cancellable));
    9913            4 :                       g = gimple_build_cond (NE_EXPR, cancellable, zero,
    9914              :                                              lab6, lab5);
    9915            4 :                       gimple_seq_add_stmt (end, g);
    9916            4 :                       gimple_seq_add_stmt (end, gimple_build_label (lab5));
    9917              :                     }
    9918          161 :                   SET_DECL_VALUE_EXPR (placeholder, out);
    9919          161 :                   DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
    9920          161 :                   SET_DECL_VALUE_EXPR (decl_placeholder, priv);
    9921          161 :                   DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 1;
    9922          161 :                   lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
    9923          322 :                   gimple_seq_add_seq (end,
    9924          161 :                                       OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
    9925          161 :                   OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
    9926          161 :                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
    9927              :                     {
    9928           56 :                       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
    9929           56 :                       OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = NULL;
    9930              :                     }
    9931          161 :                   if (cancellable)
    9932            4 :                     gimple_seq_add_stmt (end, gimple_build_label (lab6));
    9933          161 :                   tree x = lang_hooks.decls.omp_clause_dtor (c, priv);
    9934          161 :                   if (x)
    9935              :                     {
    9936          137 :                       gimple_seq tseq = NULL;
    9937          137 :                       gimplify_stmt (&x, &tseq);
    9938          137 :                       gimple_seq_add_seq (end, tseq);
    9939              :                     }
    9940              :                 }
    9941              :               else
    9942              :                 {
    9943          324 :                   tree x = build2 (rcode, TREE_TYPE (out), out, priv);
    9944          324 :                   out = unshare_expr (out);
    9945          324 :                   gimplify_assign (out, x, end);
    9946              :                 }
    9947          485 :               gimple *g
    9948          485 :                 = gimple_build_assign (new_var, POINTER_PLUS_EXPR, new_var,
    9949          485 :                                        TYPE_SIZE_UNIT (TREE_TYPE (type)));
    9950          485 :               gimple_seq_add_stmt (end, g);
    9951          485 :               g = gimple_build_assign (ref, POINTER_PLUS_EXPR, ref,
    9952          485 :                                        TYPE_SIZE_UNIT (TREE_TYPE (type)));
    9953          485 :               gimple_seq_add_stmt (end, g);
    9954          485 :               g = gimple_build_assign (i, PLUS_EXPR, i,
    9955          485 :                                        build_int_cst (TREE_TYPE (i), 1));
    9956          485 :               gimple_seq_add_stmt (end, g);
    9957          485 :               g = gimple_build_cond (LE_EXPR, i, v, body, endl);
    9958          485 :               gimple_seq_add_stmt (end, g);
    9959          485 :               gimple_seq_add_stmt (end, gimple_build_label (endl));
    9960              :             }
    9961         1178 :           else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
    9962              :             {
    9963          135 :               tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
    9964          135 :               tree oldv = NULL_TREE;
    9965          135 :               tree lab6 = NULL_TREE;
    9966          135 :               if (cancellable)
    9967              :                 {
    9968              :                   /* If this reduction needs destruction and parallel
    9969              :                      has been cancelled, jump around the merge operation
    9970              :                      to the destruction.  */
    9971            4 :                   tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
    9972            4 :                   lab6 = create_artificial_label (UNKNOWN_LOCATION);
    9973            4 :                   tree zero = build_zero_cst (TREE_TYPE (cancellable));
    9974            4 :                   g = gimple_build_cond (NE_EXPR, cancellable, zero,
    9975              :                                          lab6, lab5);
    9976            4 :                   gimple_seq_add_stmt (end, g);
    9977            4 :                   gimple_seq_add_stmt (end, gimple_build_label (lab5));
    9978              :                 }
    9979          135 :               if (omp_privatize_by_reference (decl)
    9980          149 :                   && !useless_type_conversion_p (TREE_TYPE (placeholder),
    9981           14 :                                                  TREE_TYPE (ref)))
    9982            0 :                 ref = build_fold_addr_expr_loc (OMP_CLAUSE_LOCATION (c), ref);
    9983          135 :               ref = build_fold_addr_expr_loc (OMP_CLAUSE_LOCATION (c), ref);
    9984          135 :               tree refv = create_tmp_var (TREE_TYPE (ref));
    9985          135 :               gimplify_assign (refv, ref, end);
    9986          135 :               ref = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), refv);
    9987          135 :               SET_DECL_VALUE_EXPR (placeholder, ref);
    9988          135 :               DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
    9989          135 :               tree d = maybe_lookup_decl (decl, ctx);
    9990          135 :               gcc_assert (d);
    9991          135 :               if (DECL_HAS_VALUE_EXPR_P (d))
    9992            7 :                 oldv = DECL_VALUE_EXPR (d);
    9993          135 :               if (omp_privatize_by_reference (var))
    9994              :                 {
    9995           14 :                   tree v = fold_convert (TREE_TYPE (d),
    9996              :                                          build_fold_addr_expr (new_var));
    9997           14 :                   SET_DECL_VALUE_EXPR (d, v);
    9998              :                 }
    9999              :               else
   10000          121 :                 SET_DECL_VALUE_EXPR (d, new_var);
   10001          135 :               DECL_HAS_VALUE_EXPR_P (d) = 1;
   10002          135 :               lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
   10003          135 :               if (oldv)
   10004            7 :                 SET_DECL_VALUE_EXPR (d, oldv);
   10005              :               else
   10006              :                 {
   10007          128 :                   SET_DECL_VALUE_EXPR (d, NULL_TREE);
   10008          128 :                   DECL_HAS_VALUE_EXPR_P (d) = 0;
   10009              :                 }
   10010          135 :               gimple_seq_add_seq (end, OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
   10011          135 :               OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
   10012          135 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
   10013           24 :                 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
   10014          135 :               if (cancellable)
   10015            4 :                 gimple_seq_add_stmt (end, gimple_build_label (lab6));
   10016          135 :               tree x = lang_hooks.decls.omp_clause_dtor (c, new_var);
   10017          135 :               if (x)
   10018              :                 {
   10019           37 :                   gimple_seq tseq = NULL;
   10020           37 :                   gimplify_stmt (&x, &tseq);
   10021           37 :                   gimple_seq_add_seq (end, tseq);
   10022              :                 }
   10023              :             }
   10024              :           else
   10025              :             {
   10026         1043 :               tree x = build2 (rcode, TREE_TYPE (ref), ref, new_var);
   10027         1043 :               ref = unshare_expr (ref);
   10028         1043 :               gimplify_assign (ref, x, end);
   10029              :             }
   10030         1663 :           gimple_seq_add_stmt (end, gimple_build_label (lab4));
   10031         1663 :           ++cnt;
   10032         1663 :           field = DECL_CHAIN (bfield);
   10033              :         }
   10034              :     }
   10035              : 
   10036         1176 :   if (code == OMP_TASKGROUP)
   10037              :     {
   10038          327 :       t = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_REDUCTION_REGISTER);
   10039          327 :       g = gimple_build_call (t, 1, build_fold_addr_expr (avar));
   10040          327 :       gimple_seq_add_stmt (start, g);
   10041              :     }
   10042              :   else
   10043              :     {
   10044          849 :       tree c;
   10045          849 :       if (code == OMP_FOR)
   10046          228 :         c = gimple_omp_for_clauses (ctx->stmt);
   10047          621 :       else if (code == OMP_SECTIONS)
   10048            8 :         c = gimple_omp_sections_clauses (ctx->stmt);
   10049          613 :       else if (code == OMP_SCOPE)
   10050           54 :         c = gimple_omp_scope_clauses (ctx->stmt);
   10051              :       else
   10052          559 :         c = gimple_omp_taskreg_clauses (ctx->stmt);
   10053          849 :       c = omp_find_clause (c, OMP_CLAUSE__REDUCTEMP_);
   10054          849 :       t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)),
   10055              :                         build_fold_addr_expr (avar));
   10056          849 :       gimplify_assign (OMP_CLAUSE_DECL (c), t, start);
   10057              :     }
   10058              : 
   10059         1176 :   gimple_seq_add_stmt (end, gimple_build_assign (data, PLUS_EXPR, data, sz));
   10060         1176 :   gimple_seq_add_stmt (end, gimple_build_assign (idx, PLUS_EXPR, idx,
   10061              :                                                  size_one_node));
   10062         1176 :   g = gimple_build_cond (NE_EXPR, idx, num_thr_sz, lab1, lab2);
   10063         1176 :   gimple_seq_add_stmt (end, g);
   10064         1176 :   gimple_seq_add_stmt (end, gimple_build_label (lab2));
   10065         1176 :   if (code == OMP_FOR || code == OMP_SECTIONS || code == OMP_SCOPE)
   10066              :     {
   10067          290 :       enum built_in_function bfn
   10068              :         = BUILT_IN_GOMP_WORKSHARE_TASK_REDUCTION_UNREGISTER;
   10069          290 :       t = builtin_decl_explicit (bfn);
   10070          290 :       tree c_bool_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t)));
   10071          290 :       tree arg;
   10072          290 :       if (cancellable)
   10073              :         {
   10074           14 :           arg = create_tmp_var (c_bool_type);
   10075           14 :           gimple_seq_add_stmt (end, gimple_build_assign (arg, NOP_EXPR,
   10076              :                                                          cancellable));
   10077              :         }
   10078              :       else
   10079          276 :         arg = build_int_cst (c_bool_type, 0);
   10080          290 :       g = gimple_build_call (t, 1, arg);
   10081          290 :     }
   10082              :   else
   10083              :     {
   10084          886 :       t = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_REDUCTION_UNREGISTER);
   10085          886 :       g = gimple_build_call (t, 1, build_fold_addr_expr (avar));
   10086              :     }
   10087         1176 :   gimple_seq_add_stmt (end, g);
   10088         1176 :   if (lab7)
   10089          497 :     gimple_seq_add_stmt (end, gimple_build_label (lab7));
   10090         1176 :   t = build_constructor (atype, NULL);
   10091         1176 :   TREE_THIS_VOLATILE (t) = 1;
   10092         1176 :   gimple_seq_add_stmt (end, gimple_build_assign (avar, t));
   10093              : }
   10094              : 
   10095              : /* Expand code for an OpenMP taskgroup directive.  */
   10096              : 
   10097              : static void
   10098          536 : lower_omp_taskgroup (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   10099              : {
   10100          536 :   gimple *stmt = gsi_stmt (*gsi_p);
   10101          536 :   gcall *x;
   10102          536 :   gbind *bind;
   10103          536 :   gimple_seq dseq = NULL;
   10104          536 :   tree block = make_node (BLOCK);
   10105              : 
   10106          536 :   bind = gimple_build_bind (NULL, NULL, block);
   10107          536 :   gsi_replace (gsi_p, bind, true);
   10108          536 :   gimple_bind_add_stmt (bind, stmt);
   10109              : 
   10110          536 :   push_gimplify_context ();
   10111              : 
   10112          536 :   x = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_START),
   10113              :                          0);
   10114          536 :   gimple_bind_add_stmt (bind, x);
   10115              : 
   10116          536 :   lower_omp_task_reductions (ctx, OMP_TASKGROUP,
   10117              :                              gimple_omp_taskgroup_clauses (stmt),
   10118              :                              gimple_bind_body_ptr (bind), &dseq);
   10119              : 
   10120          536 :   lower_omp (gimple_omp_body_ptr (stmt), ctx);
   10121          536 :   gimple_bind_add_seq (bind, gimple_omp_body (stmt));
   10122          536 :   gimple_omp_set_body (stmt, NULL);
   10123              : 
   10124          536 :   gimple_bind_add_seq (bind, dseq);
   10125              : 
   10126          536 :   pop_gimplify_context (bind);
   10127              : 
   10128          536 :   gimple_bind_append_vars (bind, ctx->block_vars);
   10129          536 :   BLOCK_VARS (block) = ctx->block_vars;
   10130          536 : }
   10131              : 
   10132              : 
   10133              : /* Fold the OMP_ORDERED_CLAUSES for the OMP_ORDERED in STMT if possible.  */
   10134              : 
   10135              : static void
   10136            0 : lower_omp_ordered_clauses (gimple_stmt_iterator *gsi_p, gomp_ordered *ord_stmt,
   10137              :                            omp_context *ctx)
   10138              : {
   10139            0 :   struct omp_for_data fd;
   10140            0 :   if (!ctx->outer || gimple_code (ctx->outer->stmt) != GIMPLE_OMP_FOR)
   10141            0 :     return;
   10142              : 
   10143            0 :   unsigned int len = gimple_omp_for_collapse (ctx->outer->stmt);
   10144            0 :   struct omp_for_data_loop *loops = XALLOCAVEC (struct omp_for_data_loop, len);
   10145            0 :   omp_extract_for_data (as_a <gomp_for *> (ctx->outer->stmt), &fd, loops);
   10146            0 :   if (!fd.ordered)
   10147              :     return;
   10148              : 
   10149            0 :   tree *list_p = gimple_omp_ordered_clauses_ptr (ord_stmt);
   10150            0 :   tree c = gimple_omp_ordered_clauses (ord_stmt);
   10151            0 :   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
   10152            0 :       && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
   10153              :     {
   10154              :       /* Merge depend clauses from multiple adjacent
   10155              :          #pragma omp ordered depend(sink:...) constructs
   10156              :          into one #pragma omp ordered depend(sink:...), so that
   10157              :          we can optimize them together.  */
   10158            0 :       gimple_stmt_iterator gsi = *gsi_p;
   10159            0 :       gsi_next (&gsi);
   10160            0 :       while (!gsi_end_p (gsi))
   10161              :         {
   10162            0 :           gimple *stmt = gsi_stmt (gsi);
   10163            0 :           if (is_gimple_debug (stmt)
   10164            0 :               || gimple_code (stmt) == GIMPLE_NOP)
   10165              :             {
   10166            0 :               gsi_next (&gsi);
   10167            0 :               continue;
   10168              :             }
   10169            0 :           if (gimple_code (stmt) != GIMPLE_OMP_ORDERED)
   10170              :             break;
   10171            0 :           gomp_ordered *ord_stmt2 = as_a <gomp_ordered *> (stmt);
   10172            0 :           c = gimple_omp_ordered_clauses (ord_stmt2);
   10173            0 :           if (c == NULL_TREE
   10174            0 :               || OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DOACROSS
   10175            0 :               || OMP_CLAUSE_DOACROSS_KIND (c) != OMP_CLAUSE_DOACROSS_SINK)
   10176              :             break;
   10177            0 :           while (*list_p)
   10178            0 :             list_p = &OMP_CLAUSE_CHAIN (*list_p);
   10179            0 :           *list_p = c;
   10180            0 :           gsi_remove (&gsi, true);
   10181              :         }
   10182              :     }
   10183              : 
   10184              :   /* Canonicalize sink dependence clauses into one folded clause if
   10185              :      possible.
   10186              : 
   10187              :      The basic algorithm is to create a sink vector whose first
   10188              :      element is the GCD of all the first elements, and whose remaining
   10189              :      elements are the minimum of the subsequent columns.
   10190              : 
   10191              :      We ignore dependence vectors whose first element is zero because
   10192              :      such dependencies are known to be executed by the same thread.
   10193              : 
   10194              :      We take into account the direction of the loop, so a minimum
   10195              :      becomes a maximum if the loop is iterating forwards.  We also
   10196              :      ignore sink clauses where the loop direction is unknown, or where
   10197              :      the offsets are clearly invalid because they are not a multiple
   10198              :      of the loop increment.
   10199              : 
   10200              :      For example:
   10201              : 
   10202              :         #pragma omp for ordered(2)
   10203              :         for (i=0; i < N; ++i)
   10204              :           for (j=0; j < M; ++j)
   10205              :             {
   10206              :               #pragma omp ordered \
   10207              :                 depend(sink:i-8,j-2) \
   10208              :                 depend(sink:i,j-1) \    // Completely ignored because i+0.
   10209              :                 depend(sink:i-4,j-3) \
   10210              :                 depend(sink:i-6,j-4)
   10211              :               #pragma omp ordered depend(source)
   10212              :             }
   10213              : 
   10214              :      Folded clause is:
   10215              : 
   10216              :         depend(sink:-gcd(8,4,6),-min(2,3,4))
   10217              :           -or-
   10218              :         depend(sink:-2,-2)
   10219              :   */
   10220              : 
   10221              :   /* FIXME: Computing GCD's where the first element is zero is
   10222              :      non-trivial in the presence of collapsed loops.  Do this later.  */
   10223            0 :   if (fd.collapse > 1)
   10224              :     return;
   10225              : 
   10226            0 :   wide_int *folded_deps = XALLOCAVEC (wide_int, 2 * len - 1);
   10227              : 
   10228              :   /* wide_int is not a POD so it must be default-constructed.  */
   10229            0 :   for (unsigned i = 0; i != 2 * len - 1; ++i)
   10230            0 :     new (static_cast<void*>(folded_deps + i)) wide_int ();
   10231              : 
   10232              :   tree folded_dep = NULL_TREE;
   10233              :   /* TRUE if the first dimension's offset is negative.  */
   10234              :   bool neg_offset_p = false;
   10235              : 
   10236              :   list_p = gimple_omp_ordered_clauses_ptr (ord_stmt);
   10237              :   unsigned int i;
   10238            0 :   while ((c = *list_p) != NULL)
   10239              :     {
   10240            0 :       bool remove = false;
   10241              : 
   10242            0 :       gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS);
   10243            0 :       if (OMP_CLAUSE_DOACROSS_KIND (c) != OMP_CLAUSE_DOACROSS_SINK)
   10244            0 :         goto next_ordered_clause;
   10245              : 
   10246            0 :       tree vec;
   10247            0 :       for (vec = OMP_CLAUSE_DECL (c), i = 0;
   10248            0 :            vec && TREE_CODE (vec) == TREE_LIST;
   10249            0 :            vec = TREE_CHAIN (vec), ++i)
   10250              :         {
   10251            0 :           gcc_assert (i < len);
   10252              : 
   10253              :           /* omp_extract_for_data has canonicalized the condition.  */
   10254            0 :           gcc_assert (fd.loops[i].cond_code == LT_EXPR
   10255              :                       || fd.loops[i].cond_code == GT_EXPR);
   10256            0 :           bool forward = fd.loops[i].cond_code == LT_EXPR;
   10257            0 :           bool maybe_lexically_later = true;
   10258              : 
   10259              :           /* While the committee makes up its mind, bail if we have any
   10260              :              non-constant steps.  */
   10261            0 :           if (TREE_CODE (fd.loops[i].step) != INTEGER_CST)
   10262            0 :             goto lower_omp_ordered_ret;
   10263              : 
   10264            0 :           tree itype = TREE_TYPE (TREE_VALUE (vec));
   10265            0 :           if (POINTER_TYPE_P (itype))
   10266            0 :             itype = sizetype;
   10267            0 :           wide_int offset = wide_int::from (wi::to_wide (TREE_PURPOSE (vec)),
   10268            0 :                                             TYPE_PRECISION (itype),
   10269            0 :                                             TYPE_SIGN (itype));
   10270              : 
   10271              :           /* Ignore invalid offsets that are not multiples of the step.  */
   10272            0 :           if (!wi::multiple_of_p (wi::abs (offset),
   10273            0 :                                   wi::abs (wi::to_wide (fd.loops[i].step)),
   10274              :                                   UNSIGNED))
   10275              :             {
   10276            0 :               warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
   10277              :                           "ignoring %<sink%> clause with offset that is not "
   10278              :                           "a multiple of the loop step");
   10279            0 :               remove = true;
   10280            0 :               goto next_ordered_clause;
   10281              :             }
   10282              : 
   10283              :           /* Calculate the first dimension.  The first dimension of
   10284              :              the folded dependency vector is the GCD of the first
   10285              :              elements, while ignoring any first elements whose offset
   10286              :              is 0.  */
   10287            0 :           if (i == 0)
   10288              :             {
   10289              :               /* Ignore dependence vectors whose first dimension is 0.  */
   10290            0 :               if (offset == 0)
   10291              :                 {
   10292            0 :                   remove = true;
   10293            0 :                   goto next_ordered_clause;
   10294              :                 }
   10295              :               else
   10296              :                 {
   10297            0 :                   if (!TYPE_UNSIGNED (itype) && (forward ^ wi::neg_p (offset)))
   10298              :                     {
   10299            0 :                       error_at (OMP_CLAUSE_LOCATION (c),
   10300              :                                 "first offset must be in opposite direction "
   10301              :                                 "of loop iterations");
   10302            0 :                       goto lower_omp_ordered_ret;
   10303              :                     }
   10304            0 :                   if (forward)
   10305            0 :                     offset = -offset;
   10306            0 :                   neg_offset_p = forward;
   10307              :                   /* Initialize the first time around.  */
   10308            0 :                   if (folded_dep == NULL_TREE)
   10309              :                     {
   10310            0 :                       folded_dep = c;
   10311            0 :                       folded_deps[0] = offset;
   10312              :                     }
   10313              :                   else
   10314            0 :                     folded_deps[0] = wi::gcd (folded_deps[0],
   10315            0 :                                               offset, UNSIGNED);
   10316              :                 }
   10317              :             }
   10318              :           /* Calculate minimum for the remaining dimensions.  */
   10319              :           else
   10320              :             {
   10321            0 :               folded_deps[len + i - 1] = offset;
   10322            0 :               if (folded_dep == c)
   10323            0 :                 folded_deps[i] = offset;
   10324            0 :               else if (maybe_lexically_later
   10325            0 :                        && !wi::eq_p (folded_deps[i], offset))
   10326              :                 {
   10327            0 :                   if (forward ^ wi::gts_p (folded_deps[i], offset))
   10328              :                     {
   10329              :                       unsigned int j;
   10330            0 :                       folded_dep = c;
   10331            0 :                       for (j = 1; j <= i; j++)
   10332            0 :                         folded_deps[j] = folded_deps[len + j - 1];
   10333              :                     }
   10334              :                   else
   10335            0 :                     maybe_lexically_later = false;
   10336              :                 }
   10337              :             }
   10338            0 :         }
   10339            0 :       gcc_assert (i == len);
   10340              : 
   10341              :       remove = true;
   10342              : 
   10343            0 :     next_ordered_clause:
   10344            0 :       if (remove)
   10345            0 :         *list_p = OMP_CLAUSE_CHAIN (c);
   10346              :       else
   10347            0 :         list_p = &OMP_CLAUSE_CHAIN (c);
   10348              :     }
   10349              : 
   10350            0 :   if (folded_dep)
   10351              :     {
   10352            0 :       if (neg_offset_p)
   10353            0 :         folded_deps[0] = -folded_deps[0];
   10354              : 
   10355            0 :       tree itype = TREE_TYPE (TREE_VALUE (OMP_CLAUSE_DECL (folded_dep)));
   10356            0 :       if (POINTER_TYPE_P (itype))
   10357            0 :         itype = sizetype;
   10358              : 
   10359            0 :       TREE_PURPOSE (OMP_CLAUSE_DECL (folded_dep))
   10360            0 :         = wide_int_to_tree (itype, folded_deps[0]);
   10361            0 :       OMP_CLAUSE_CHAIN (folded_dep) = gimple_omp_ordered_clauses (ord_stmt);
   10362            0 :       *gimple_omp_ordered_clauses_ptr (ord_stmt) = folded_dep;
   10363              :     }
   10364              : 
   10365            0 :  lower_omp_ordered_ret:
   10366              : 
   10367              :   /* Ordered without clauses is #pragma omp threads, while we want
   10368              :      a nop instead if we remove all clauses.  */
   10369            0 :   if (gimple_omp_ordered_clauses (ord_stmt) == NULL_TREE)
   10370            0 :     gsi_replace (gsi_p, gimple_build_nop (), true);
   10371              : }
   10372              : 
   10373              : 
   10374              : /* Expand code for an OpenMP ordered directive.  */
   10375              : 
   10376              : static void
   10377         1128 : lower_omp_ordered (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   10378              : {
   10379         1128 :   tree block;
   10380         1128 :   gimple *stmt = gsi_stmt (*gsi_p), *g;
   10381         1128 :   gomp_ordered *ord_stmt = as_a <gomp_ordered *> (stmt);
   10382         1128 :   gcall *x;
   10383         1128 :   gbind *bind;
   10384         1128 :   bool simd = omp_find_clause (gimple_omp_ordered_clauses (ord_stmt),
   10385         1128 :                                OMP_CLAUSE_SIMD);
   10386              :   /* FIXME: this should check presence of OMP_CLAUSE__SIMT_ on the enclosing
   10387              :      loop.  */
   10388         1128 :   bool maybe_simt
   10389         1128 :     = simd && omp_maybe_offloaded_ctx (ctx) && omp_max_simt_vf () > 1;
   10390         1128 :   bool threads = omp_find_clause (gimple_omp_ordered_clauses (ord_stmt),
   10391         1128 :                                   OMP_CLAUSE_THREADS);
   10392              : 
   10393         1128 :   if (gimple_omp_ordered_standalone_p (ord_stmt))
   10394              :     {
   10395              :       /* FIXME: This is needs to be moved to the expansion to verify various
   10396              :          conditions only testable on cfg with dominators computed, and also
   10397              :          all the depend clauses to be merged still might need to be available
   10398              :          for the runtime checks.  */
   10399              :       if (0)
   10400              :         lower_omp_ordered_clauses (gsi_p, ord_stmt, ctx);
   10401         1128 :       return;
   10402              :     }
   10403              : 
   10404          415 :   push_gimplify_context ();
   10405              : 
   10406          415 :   block = make_node (BLOCK);
   10407          415 :   bind = gimple_build_bind (NULL, NULL, block);
   10408          415 :   gsi_replace (gsi_p, bind, true);
   10409          415 :   gimple_bind_add_stmt (bind, stmt);
   10410              : 
   10411          415 :   if (simd)
   10412              :     {
   10413           81 :       x = gimple_build_call_internal (IFN_GOMP_SIMD_ORDERED_START, 1,
   10414           81 :                                       build_int_cst (integer_type_node, threads));
   10415           81 :       cfun->has_simduid_loops = true;
   10416              :     }
   10417              :   else
   10418          334 :     x = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_ORDERED_START),
   10419              :                            0);
   10420          415 :   gimple_bind_add_stmt (bind, x);
   10421              : 
   10422          415 :   tree counter = NULL_TREE, test = NULL_TREE, body = NULL_TREE;
   10423          415 :   if (maybe_simt)
   10424              :     {
   10425            0 :       counter = create_tmp_var (integer_type_node);
   10426            0 :       g = gimple_build_call_internal (IFN_GOMP_SIMT_LANE, 0);
   10427            0 :       gimple_call_set_lhs (g, counter);
   10428            0 :       gimple_bind_add_stmt (bind, g);
   10429              : 
   10430            0 :       body = create_artificial_label (UNKNOWN_LOCATION);
   10431            0 :       test = create_artificial_label (UNKNOWN_LOCATION);
   10432            0 :       gimple_bind_add_stmt (bind, gimple_build_label (body));
   10433              : 
   10434            0 :       tree simt_pred = create_tmp_var (integer_type_node);
   10435            0 :       g = gimple_build_call_internal (IFN_GOMP_SIMT_ORDERED_PRED, 1, counter);
   10436            0 :       gimple_call_set_lhs (g, simt_pred);
   10437            0 :       gimple_bind_add_stmt (bind, g);
   10438              : 
   10439            0 :       tree t = create_artificial_label (UNKNOWN_LOCATION);
   10440            0 :       g = gimple_build_cond (EQ_EXPR, simt_pred, integer_zero_node, t, test);
   10441            0 :       gimple_bind_add_stmt (bind, g);
   10442              : 
   10443            0 :       gimple_bind_add_stmt (bind, gimple_build_label (t));
   10444              :     }
   10445          415 :   lower_omp (gimple_omp_body_ptr (stmt), ctx);
   10446          415 :   gimple_omp_set_body (stmt, maybe_catch_exception (gimple_omp_body (stmt)));
   10447          415 :   gimple_bind_add_seq (bind, gimple_omp_body (stmt));
   10448          415 :   gimple_omp_set_body (stmt, NULL);
   10449              : 
   10450          415 :   if (maybe_simt)
   10451              :     {
   10452            0 :       gimple_bind_add_stmt (bind, gimple_build_label (test));
   10453            0 :       g = gimple_build_assign (counter, MINUS_EXPR, counter, integer_one_node);
   10454            0 :       gimple_bind_add_stmt (bind, g);
   10455              : 
   10456            0 :       tree c = build2 (GE_EXPR, boolean_type_node, counter, integer_zero_node);
   10457            0 :       tree nonneg = create_tmp_var (integer_type_node);
   10458            0 :       gimple_seq tseq = NULL;
   10459            0 :       gimplify_assign (nonneg, fold_convert (integer_type_node, c), &tseq);
   10460            0 :       gimple_bind_add_seq (bind, tseq);
   10461              : 
   10462            0 :       g = gimple_build_call_internal (IFN_GOMP_SIMT_VOTE_ANY, 1, nonneg);
   10463            0 :       gimple_call_set_lhs (g, nonneg);
   10464            0 :       gimple_bind_add_stmt (bind, g);
   10465              : 
   10466            0 :       tree end = create_artificial_label (UNKNOWN_LOCATION);
   10467            0 :       g = gimple_build_cond (NE_EXPR, nonneg, integer_zero_node, body, end);
   10468            0 :       gimple_bind_add_stmt (bind, g);
   10469              : 
   10470            0 :       gimple_bind_add_stmt (bind, gimple_build_label (end));
   10471              :     }
   10472          415 :   if (simd)
   10473           81 :     x = gimple_build_call_internal (IFN_GOMP_SIMD_ORDERED_END, 1,
   10474           81 :                                     build_int_cst (integer_type_node, threads));
   10475              :   else
   10476          334 :     x = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_ORDERED_END),
   10477              :                            0);
   10478          415 :   gimple_bind_add_stmt (bind, x);
   10479              : 
   10480          415 :   gimple_bind_add_stmt (bind, gimple_build_omp_return (true));
   10481              : 
   10482          415 :   pop_gimplify_context (bind);
   10483              : 
   10484          415 :   gimple_bind_append_vars (bind, ctx->block_vars);
   10485          415 :   BLOCK_VARS (block) = gimple_bind_vars (bind);
   10486              : }
   10487              : 
   10488              : 
   10489              : /* Expand code for an OpenMP scan directive and the structured block
   10490              :    before the scan directive.  */
   10491              : 
   10492              : static void
   10493         1604 : lower_omp_scan (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   10494              : {
   10495         1604 :   gimple *stmt = gsi_stmt (*gsi_p);
   10496         1604 :   bool has_clauses
   10497         1604 :     = gimple_omp_scan_clauses (as_a <gomp_scan *> (stmt)) != NULL;
   10498         1604 :   tree lane = NULL_TREE;
   10499         1604 :   gimple_seq before = NULL;
   10500         1604 :   omp_context *octx = ctx->outer;
   10501         1604 :   gcc_assert (octx);
   10502         1604 :   if (octx->scan_exclusive && !has_clauses)
   10503              :     {
   10504          536 :       gimple_stmt_iterator gsi2 = *gsi_p;
   10505          536 :       gsi_next (&gsi2);
   10506          536 :       gimple *stmt2 = gsi_stmt (gsi2);
   10507              :       /* For exclusive scan, swap GIMPLE_OMP_SCAN without clauses
   10508              :          with following GIMPLE_OMP_SCAN with clauses, so that input_phase,
   10509              :          the one with exclusive clause(s), comes first.  */
   10510          536 :       if (stmt2
   10511          274 :           && gimple_code (stmt2) == GIMPLE_OMP_SCAN
   10512          804 :           && gimple_omp_scan_clauses (as_a <gomp_scan *> (stmt2)) != NULL)
   10513              :         {
   10514          268 :           gsi_remove (gsi_p, false);
   10515          268 :           gsi_insert_after (gsi_p, stmt, GSI_SAME_STMT);
   10516          268 :           ctx = maybe_lookup_ctx (stmt2);
   10517          268 :           gcc_assert (ctx);
   10518          268 :           lower_omp_scan (gsi_p, ctx);
   10519          268 :           return;
   10520              :         }
   10521              :     }
   10522              : 
   10523         1336 :   bool input_phase = has_clauses ^ octx->scan_inclusive;
   10524         1336 :   bool is_simd = (gimple_code (octx->stmt) == GIMPLE_OMP_FOR
   10525         1336 :                   && gimple_omp_for_kind (octx->stmt) == GF_OMP_FOR_KIND_SIMD);
   10526         1336 :   bool is_for = (gimple_code (octx->stmt) == GIMPLE_OMP_FOR
   10527         1336 :                  && gimple_omp_for_kind (octx->stmt) == GF_OMP_FOR_KIND_FOR
   10528         1710 :                  && !gimple_omp_for_combined_p (octx->stmt));
   10529         1336 :   bool is_for_simd = is_simd && gimple_omp_for_combined_into_p (octx->stmt);
   10530          372 :   if (is_for_simd && octx->for_simd_scan_phase)
   10531              :     is_simd = false;
   10532         1150 :   if (is_simd)
   10533          776 :     if (tree c = omp_find_clause (gimple_omp_for_clauses (octx->stmt),
   10534              :                                   OMP_CLAUSE__SIMDUID_))
   10535              :       {
   10536          372 :         tree uid = OMP_CLAUSE__SIMDUID__DECL (c);
   10537          372 :         lane = create_tmp_var (unsigned_type_node);
   10538          372 :         tree t = build_int_cst (integer_type_node,
   10539          558 :                                 input_phase ? 1
   10540          186 :                                 : octx->scan_inclusive ? 2 : 3);
   10541          372 :         gimple *g
   10542          372 :           = gimple_build_call_internal (IFN_GOMP_SIMD_LANE, 2, uid, t);
   10543          372 :         gimple_call_set_lhs (g, lane);
   10544          372 :         gimple_seq_add_stmt (&before, g);
   10545              :       }
   10546              : 
   10547         1336 :   if (is_simd || is_for)
   10548              :     {
   10549         5144 :       for (tree c = gimple_omp_for_clauses (octx->stmt);
   10550         5144 :            c; c = OMP_CLAUSE_CHAIN (c))
   10551         4180 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   10552         4180 :             && OMP_CLAUSE_REDUCTION_INSCAN (c))
   10553              :           {
   10554         1188 :             location_t clause_loc = OMP_CLAUSE_LOCATION (c);
   10555         1188 :             tree var = OMP_CLAUSE_DECL (c);
   10556         1188 :             tree new_var = lookup_decl (var, octx);
   10557         1188 :             tree val = new_var;
   10558         1188 :             tree var2 = NULL_TREE;
   10559         1188 :             tree var3 = NULL_TREE;
   10560         1188 :             tree var4 = NULL_TREE;
   10561         1188 :             tree lane0 = NULL_TREE;
   10562         1188 :             tree new_vard = new_var;
   10563         1188 :             if (omp_privatize_by_reference (var))
   10564              :               {
   10565          184 :                 new_var = build_simple_mem_ref_loc (clause_loc, new_var);
   10566          184 :                 val = new_var;
   10567              :               }
   10568         1188 :             if (DECL_HAS_VALUE_EXPR_P (new_vard))
   10569              :               {
   10570          468 :                 val = DECL_VALUE_EXPR (new_vard);
   10571          468 :                 if (new_vard != new_var)
   10572              :                   {
   10573           76 :                     gcc_assert (TREE_CODE (val) == ADDR_EXPR);
   10574           76 :                     val = TREE_OPERAND (val, 0);
   10575              :                   }
   10576          468 :                 if (TREE_CODE (val) == ARRAY_REF
   10577          468 :                     && VAR_P (TREE_OPERAND (val, 0)))
   10578              :                   {
   10579          468 :                     tree v = TREE_OPERAND (val, 0);
   10580          468 :                     if (lookup_attribute ("omp simd array",
   10581          468 :                                           DECL_ATTRIBUTES (v)))
   10582              :                       {
   10583          468 :                         val = unshare_expr (val);
   10584          468 :                         lane0 = TREE_OPERAND (val, 1);
   10585          468 :                         TREE_OPERAND (val, 1) = lane;
   10586          468 :                         var2 = lookup_decl (v, octx);
   10587          468 :                         if (octx->scan_exclusive)
   10588          228 :                           var4 = lookup_decl (var2, octx);
   10589          468 :                         if (input_phase
   10590          468 :                             && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
   10591          120 :                           var3 = maybe_lookup_decl (var4 ? var4 : var2, octx);
   10592          468 :                         if (!input_phase)
   10593              :                           {
   10594          234 :                             var2 = build4 (ARRAY_REF, TREE_TYPE (val),
   10595              :                                            var2, lane, NULL_TREE, NULL_TREE);
   10596          234 :                             TREE_THIS_NOTRAP (var2) = 1;
   10597          234 :                             if (octx->scan_exclusive)
   10598              :                               {
   10599          114 :                                 var4 = build4 (ARRAY_REF, TREE_TYPE (val),
   10600              :                                                var4, lane, NULL_TREE,
   10601              :                                                NULL_TREE);
   10602          114 :                                 TREE_THIS_NOTRAP (var4) = 1;
   10603              :                               }
   10604              :                           }
   10605              :                         else
   10606              :                           var2 = val;
   10607              :                       }
   10608              :                   }
   10609          468 :                 gcc_assert (var2);
   10610              :               }
   10611              :             else
   10612              :               {
   10613          720 :                 var2 = build_outer_var_ref (var, octx);
   10614          720 :                 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
   10615              :                   {
   10616          220 :                     var3 = maybe_lookup_decl (new_vard, octx);
   10617          220 :                     if (var3 == new_vard || var3 == NULL_TREE)
   10618              :                       var3 = NULL_TREE;
   10619          108 :                     else if (is_simd && octx->scan_exclusive && !input_phase)
   10620              :                       {
   10621           16 :                         var4 = maybe_lookup_decl (var3, octx);
   10622           16 :                         if (var4 == var3 || var4 == NULL_TREE)
   10623              :                           {
   10624            0 :                             if (TREE_ADDRESSABLE (TREE_TYPE (new_var)))
   10625              :                               {
   10626              :                                 var4 = var3;
   10627              :                                 var3 = NULL_TREE;
   10628              :                               }
   10629              :                             else
   10630           16 :                               var4 = NULL_TREE;
   10631              :                           }
   10632              :                       }
   10633              :                   }
   10634          688 :                 if (is_simd
   10635          500 :                     && octx->scan_exclusive
   10636          244 :                     && !input_phase
   10637          244 :                     && var4 == NULL_TREE)
   10638          106 :                   var4 = create_tmp_var (TREE_TYPE (val));
   10639              :               }
   10640         1188 :             if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
   10641              :               {
   10642          376 :                 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
   10643          376 :                 if (input_phase)
   10644              :                   {
   10645          188 :                     if (var3)
   10646              :                       {
   10647              :                         /* If we've added a separate identity element
   10648              :                            variable, copy it over into val.  */
   10649           92 :                         tree x = lang_hooks.decls.omp_clause_assign_op (c, val,
   10650              :                                                                         var3);
   10651           92 :                         gimplify_and_add (x, &before);
   10652              :                       }
   10653           96 :                     else if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
   10654              :                       {
   10655              :                         /* Otherwise, assign to it the identity element.  */
   10656           96 :                         gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
   10657           96 :                         if (is_for)
   10658           16 :                           tseq = copy_gimple_seq_and_replace_locals (tseq);
   10659           96 :                         tree ref = build_outer_var_ref (var, octx);
   10660           96 :                         tree x = (DECL_HAS_VALUE_EXPR_P (new_vard)
   10661           96 :                                   ? DECL_VALUE_EXPR (new_vard) : NULL_TREE);
   10662           40 :                         if (x)
   10663              :                           {
   10664           40 :                             if (new_vard != new_var)
   10665            8 :                               val = build_fold_addr_expr_loc (clause_loc, val);
   10666           40 :                             SET_DECL_VALUE_EXPR (new_vard, val);
   10667              :                           }
   10668           96 :                         SET_DECL_VALUE_EXPR (placeholder, ref);
   10669           96 :                         DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
   10670           96 :                         lower_omp (&tseq, octx);
   10671           96 :                         if (x)
   10672           40 :                           SET_DECL_VALUE_EXPR (new_vard, x);
   10673           96 :                         SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
   10674           96 :                         DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
   10675           96 :                         gimple_seq_add_seq (&before, tseq);
   10676           96 :                         if (is_simd)
   10677           80 :                           OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
   10678              :                       }
   10679              :                   }
   10680          188 :                 else if (is_simd)
   10681              :                   {
   10682          156 :                     tree x;
   10683          156 :                     if (octx->scan_exclusive)
   10684              :                       {
   10685           72 :                         tree v4 = unshare_expr (var4);
   10686           72 :                         tree v2 = unshare_expr (var2);
   10687           72 :                         x = lang_hooks.decls.omp_clause_assign_op (c, v4, v2);
   10688           72 :                         gimplify_and_add (x, &before);
   10689              :                       }
   10690          156 :                     gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
   10691          156 :                     x = (DECL_HAS_VALUE_EXPR_P (new_vard)
   10692          156 :                          ? DECL_VALUE_EXPR (new_vard) : NULL_TREE);
   10693          156 :                     tree vexpr = val;
   10694          156 :                     if (x && new_vard != new_var)
   10695           30 :                       vexpr = build_fold_addr_expr_loc (clause_loc, val);
   10696          156 :                     if (x)
   10697           78 :                       SET_DECL_VALUE_EXPR (new_vard, vexpr);
   10698          156 :                     SET_DECL_VALUE_EXPR (placeholder, var2);
   10699          156 :                     DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
   10700          156 :                     lower_omp (&tseq, octx);
   10701          156 :                     gimple_seq_add_seq (&before, tseq);
   10702          156 :                     OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
   10703          156 :                     if (x)
   10704           78 :                       SET_DECL_VALUE_EXPR (new_vard, x);
   10705          156 :                     SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
   10706          156 :                     DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
   10707          156 :                     if (octx->scan_inclusive)
   10708              :                       {
   10709           84 :                         x = lang_hooks.decls.omp_clause_assign_op (c, val,
   10710              :                                                                    var2);
   10711           84 :                         gimplify_and_add (x, &before);
   10712              :                       }
   10713           72 :                     else if (lane0 == NULL_TREE)
   10714              :                       {
   10715           36 :                         x = lang_hooks.decls.omp_clause_assign_op (c, val,
   10716              :                                                                    var4);
   10717           36 :                         gimplify_and_add (x, &before);
   10718              :                       }
   10719              :                   }
   10720              :               }
   10721              :             else
   10722              :               {
   10723          812 :                 if (input_phase)
   10724              :                   {
   10725              :                     /* input phase.  Set val to initializer before
   10726              :                        the body.  */
   10727          406 :                     tree x = omp_reduction_init (c, TREE_TYPE (new_var));
   10728          406 :                     gimplify_assign (val, x, &before);
   10729              :                   }
   10730          406 :                 else if (is_simd)
   10731              :                   {
   10732              :                     /* scan phase.  */
   10733          328 :                     enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
   10734          328 :                     if (code == MINUS_EXPR)
   10735            0 :                       code = PLUS_EXPR;
   10736              : 
   10737          328 :                     tree x = build2 (code, TREE_TYPE (var2),
   10738              :                                      unshare_expr (var2), unshare_expr (val));
   10739          328 :                     if (octx->scan_inclusive)
   10740              :                       {
   10741          164 :                         gimplify_assign (unshare_expr (var2), x, &before);
   10742          164 :                         gimplify_assign (val, var2, &before);
   10743              :                       }
   10744              :                     else
   10745              :                       {
   10746          164 :                         gimplify_assign (unshare_expr (var4),
   10747              :                                          unshare_expr (var2), &before);
   10748          164 :                         gimplify_assign (var2, x, &before);
   10749          164 :                         if (lane0 == NULL_TREE)
   10750           86 :                           gimplify_assign (val, var4, &before);
   10751              :                       }
   10752              :                   }
   10753              :               }
   10754         1188 :             if (octx->scan_exclusive && !input_phase && lane0)
   10755              :               {
   10756          114 :                 tree vexpr = unshare_expr (var4);
   10757          114 :                 TREE_OPERAND (vexpr, 1) = lane0;
   10758          114 :                 if (new_vard != new_var)
   10759           16 :                   vexpr = build_fold_addr_expr_loc (clause_loc, vexpr);
   10760          114 :                 SET_DECL_VALUE_EXPR (new_vard, vexpr);
   10761              :               }
   10762              :           }
   10763              :     }
   10764         1336 :   if (is_simd && !is_for_simd)
   10765              :     {
   10766          590 :       gsi_insert_seq_after (gsi_p, gimple_omp_body (stmt), GSI_SAME_STMT);
   10767          590 :       gsi_insert_seq_after (gsi_p, before, GSI_SAME_STMT);
   10768          590 :       gsi_replace (gsi_p, gimple_build_nop (), true);
   10769          590 :       return;
   10770              :     }
   10771          746 :   lower_omp (gimple_omp_body_ptr (stmt), octx);
   10772          746 :   if (before)
   10773              :     {
   10774          280 :       gimple_stmt_iterator gsi = gsi_start (*gimple_omp_body_ptr (stmt));
   10775          280 :       gsi_insert_seq_before (&gsi, before, GSI_SAME_STMT);
   10776              :     }
   10777              : }
   10778              : 
   10779              : 
   10780              : /* Gimplify a GIMPLE_OMP_CRITICAL statement.  This is a relatively simple
   10781              :    substitution of a couple of function calls.  But in the NAMED case,
   10782              :    requires that languages coordinate a symbol name.  It is therefore
   10783              :    best put here in common code.  */
   10784              : 
   10785              : static GTY(()) hash_map<tree, tree> *critical_name_mutexes;
   10786              : 
   10787              : static void
   10788          317 : lower_omp_critical (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   10789              : {
   10790          317 :   tree block;
   10791          317 :   tree name, lock, unlock;
   10792          317 :   gomp_critical *stmt = as_a <gomp_critical *> (gsi_stmt (*gsi_p));
   10793          317 :   gbind *bind;
   10794          317 :   location_t loc = gimple_location (stmt);
   10795          317 :   gimple_seq tbody;
   10796              : 
   10797          317 :   name = gimple_omp_critical_name (stmt);
   10798          317 :   if (name)
   10799              :     {
   10800           92 :       tree decl;
   10801              : 
   10802           92 :       if (!critical_name_mutexes)
   10803           50 :         critical_name_mutexes = hash_map<tree, tree>::create_ggc (10);
   10804              : 
   10805           92 :       tree *n = critical_name_mutexes->get (name);
   10806           92 :       if (n == NULL)
   10807              :         {
   10808           74 :           char *new_str;
   10809              : 
   10810           74 :           decl = create_tmp_var_raw (ptr_type_node);
   10811              : 
   10812           74 :           new_str = ACONCAT ((".gomp_critical_user_",
   10813              :                               IDENTIFIER_POINTER (name), NULL));
   10814           74 :           DECL_NAME (decl) = get_identifier (new_str);
   10815           74 :           TREE_PUBLIC (decl) = 1;
   10816           74 :           TREE_STATIC (decl) = 1;
   10817           74 :           DECL_COMMON (decl) = 1;
   10818           74 :           DECL_ARTIFICIAL (decl) = 1;
   10819           74 :           DECL_IGNORED_P (decl) = 1;
   10820              : 
   10821           74 :           varpool_node::finalize_decl (decl);
   10822              : 
   10823           74 :           critical_name_mutexes->put (name, decl);
   10824              :         }
   10825              :       else
   10826           18 :         decl = *n;
   10827              : 
   10828              :       /* If '#pragma omp critical' is inside offloaded region or
   10829              :          inside function marked as offloadable, the symbol must be
   10830              :          marked as offloadable too.  */
   10831           92 :       omp_context *octx;
   10832           92 :       if (cgraph_node::get (current_function_decl)->offloadable)
   10833            1 :         varpool_node::get_create (decl)->offloadable = 1;
   10834              :       else
   10835          181 :         for (octx = ctx->outer; octx; octx = octx->outer)
   10836           94 :           if (is_gimple_omp_offloaded (octx->stmt))
   10837              :             {
   10838            4 :               varpool_node::get_create (decl)->offloadable = 1;
   10839            4 :               break;
   10840              :             }
   10841              : 
   10842           92 :       lock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_NAME_START);
   10843           92 :       lock = build_call_expr_loc (loc, lock, 1,
   10844              :                                   build_fold_addr_expr_loc (loc, decl));
   10845              : 
   10846           92 :       unlock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_NAME_END);
   10847           92 :       unlock = build_call_expr_loc (loc, unlock, 1,
   10848              :                                 build_fold_addr_expr_loc (loc, decl));
   10849              :     }
   10850              :   else
   10851              :     {
   10852          225 :       lock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_START);
   10853          225 :       lock = build_call_expr_loc (loc, lock, 0);
   10854              : 
   10855          225 :       unlock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_END);
   10856          225 :       unlock = build_call_expr_loc (loc, unlock, 0);
   10857              :     }
   10858              : 
   10859          317 :   push_gimplify_context ();
   10860              : 
   10861          317 :   block = make_node (BLOCK);
   10862          317 :   bind = gimple_build_bind (NULL, NULL, block);
   10863          317 :   gsi_replace (gsi_p, bind, true);
   10864          317 :   gimple_bind_add_stmt (bind, stmt);
   10865              : 
   10866          317 :   tbody = gimple_bind_body (bind);
   10867          317 :   gimplify_and_add (lock, &tbody);
   10868          317 :   gimple_bind_set_body (bind, tbody);
   10869              : 
   10870          317 :   lower_omp (gimple_omp_body_ptr (stmt), ctx);
   10871          317 :   gimple_omp_set_body (stmt, maybe_catch_exception (gimple_omp_body (stmt)));
   10872          317 :   gimple_bind_add_seq (bind, gimple_omp_body (stmt));
   10873          317 :   gimple_omp_set_body (stmt, NULL);
   10874              : 
   10875          317 :   tbody = gimple_bind_body (bind);
   10876          317 :   gimplify_and_add (unlock, &tbody);
   10877          317 :   gimple_bind_set_body (bind, tbody);
   10878              : 
   10879          317 :   gimple_bind_add_stmt (bind, gimple_build_omp_return (true));
   10880              : 
   10881          317 :   pop_gimplify_context (bind);
   10882          317 :   gimple_bind_append_vars (bind, ctx->block_vars);
   10883          317 :   BLOCK_VARS (block) = gimple_bind_vars (bind);
   10884          317 : }
   10885              : 
   10886              : /* A subroutine of lower_omp_for.  Generate code to emit the predicate
   10887              :    for a lastprivate clause.  Given a loop control predicate of (V
   10888              :    cond N2), we gate the clause on (!(V cond N2)).  The lowered form
   10889              :    is appended to *DLIST, iterator initialization is appended to
   10890              :    *BODY_P.  *CLIST is for lastprivate(conditional:) code that needs
   10891              :    to be emitted in a critical section.  */
   10892              : 
   10893              : static void
   10894        49458 : lower_omp_for_lastprivate (struct omp_for_data *fd, gimple_seq *body_p,
   10895              :                            gimple_seq *dlist, gimple_seq *clist,
   10896              :                            struct omp_context *ctx)
   10897              : {
   10898        49458 :   tree clauses, cond, vinit;
   10899        49458 :   enum tree_code cond_code;
   10900        49458 :   gimple_seq stmts;
   10901              : 
   10902        49458 :   cond_code = fd->loop.cond_code;
   10903        49458 :   cond_code = cond_code == LT_EXPR ? GE_EXPR : LE_EXPR;
   10904              : 
   10905              :   /* When possible, use a strict equality expression.  This can let VRP
   10906              :      type optimizations deduce the value and remove a copy.  */
   10907        49458 :   if (tree_fits_shwi_p (fd->loop.step))
   10908              :     {
   10909        47219 :       HOST_WIDE_INT step = tree_to_shwi (fd->loop.step);
   10910        47219 :       if (step == 1 || step == -1)
   10911        49458 :         cond_code = EQ_EXPR;
   10912              :     }
   10913              : 
   10914        49458 :   tree n2 = fd->loop.n2;
   10915        49458 :   if (fd->collapse > 1
   10916        13544 :       && TREE_CODE (n2) != INTEGER_CST
   10917        56463 :       && gimple_omp_for_combined_into_p (fd->for_stmt))
   10918              :     {
   10919         3082 :       struct omp_context *taskreg_ctx = NULL;
   10920         3082 :       if (gimple_code (ctx->outer->stmt) == GIMPLE_OMP_FOR)
   10921              :         {
   10922         1448 :           gomp_for *gfor = as_a <gomp_for *> (ctx->outer->stmt);
   10923         1448 :           if (gimple_omp_for_kind (gfor) == GF_OMP_FOR_KIND_FOR
   10924         1448 :               || gimple_omp_for_kind (gfor) == GF_OMP_FOR_KIND_DISTRIBUTE)
   10925              :             {
   10926         1416 :               if (gimple_omp_for_combined_into_p (gfor))
   10927              :                 {
   10928          840 :                   gcc_assert (ctx->outer->outer
   10929              :                               && is_parallel_ctx (ctx->outer->outer));
   10930              :                   taskreg_ctx = ctx->outer->outer;
   10931              :                 }
   10932              :               else
   10933              :                 {
   10934          576 :                   struct omp_for_data outer_fd;
   10935          576 :                   omp_extract_for_data (gfor, &outer_fd, NULL);
   10936          576 :                   n2 = fold_convert (TREE_TYPE (n2), outer_fd.loop.n2);
   10937              :                 }
   10938              :             }
   10939           32 :           else if (gimple_omp_for_kind (gfor) == GF_OMP_FOR_KIND_TASKLOOP)
   10940           32 :             taskreg_ctx = ctx->outer->outer;
   10941              :         }
   10942         1634 :       else if (is_taskreg_ctx (ctx->outer))
   10943              :         taskreg_ctx = ctx->outer;
   10944         3082 :       if (taskreg_ctx)
   10945              :         {
   10946         2506 :           int i;
   10947         2506 :           tree taskreg_clauses
   10948         2506 :             = gimple_omp_taskreg_clauses (taskreg_ctx->stmt);
   10949         2506 :           tree innerc = omp_find_clause (taskreg_clauses,
   10950              :                                          OMP_CLAUSE__LOOPTEMP_);
   10951         2506 :           gcc_assert (innerc);
   10952         2506 :           int count = fd->collapse;
   10953         2506 :           if (fd->non_rect
   10954           24 :               && fd->last_nonrect == fd->first_nonrect + 1)
   10955           12 :             if (tree v = gimple_omp_for_index (fd->for_stmt, fd->last_nonrect))
   10956           12 :               if (!TYPE_UNSIGNED (TREE_TYPE (v)))
   10957           12 :                 count += 4;
   10958         9622 :           for (i = 0; i < count; i++)
   10959              :             {
   10960         7116 :               innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
   10961              :                                         OMP_CLAUSE__LOOPTEMP_);
   10962         7116 :               gcc_assert (innerc);
   10963              :             }
   10964         2506 :           innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
   10965              :                                     OMP_CLAUSE__LOOPTEMP_);
   10966         2506 :           if (innerc)
   10967         1667 :             n2 = fold_convert (TREE_TYPE (n2),
   10968              :                                lookup_decl (OMP_CLAUSE_DECL (innerc),
   10969              :                                             taskreg_ctx));
   10970              :         }
   10971              :     }
   10972        49458 :   cond = build2 (cond_code, boolean_type_node, fd->loop.v, n2);
   10973              : 
   10974        49458 :   clauses = gimple_omp_for_clauses (fd->for_stmt);
   10975        49458 :   stmts = NULL;
   10976        49458 :   lower_lastprivate_clauses (clauses, cond, body_p, &stmts, clist, ctx);
   10977        49458 :   if (!gimple_seq_empty_p (stmts))
   10978              :     {
   10979        17089 :       gimple_seq_add_seq (&stmts, *dlist);
   10980        17089 :       *dlist = stmts;
   10981              : 
   10982              :       /* Optimize: v = 0; is usually cheaper than v = some_other_constant.  */
   10983        17089 :       vinit = fd->loop.n1;
   10984        17089 :       if (cond_code == EQ_EXPR
   10985        15102 :           && tree_fits_shwi_p (fd->loop.n2)
   10986        26742 :           && ! integer_zerop (fd->loop.n2))
   10987         8437 :         vinit = build_int_cst (TREE_TYPE (fd->loop.v), 0);
   10988              :       else
   10989         8652 :         vinit = unshare_expr (vinit);
   10990              : 
   10991              :       /* Initialize the iterator variable, so that threads that don't execute
   10992              :          any iterations don't execute the lastprivate clauses by accident.  */
   10993        17089 :       gimplify_assign (fd->loop.v, vinit, body_p);
   10994              :     }
   10995        49458 : }
   10996              : 
   10997              : /* OpenACC privatization.
   10998              : 
   10999              :    Or, in other words, *sharing* at the respective OpenACC level of
   11000              :    parallelism.
   11001              : 
   11002              :    From a correctness perspective, a non-addressable variable can't be accessed
   11003              :    outside the current thread, so it can go in a (faster than shared memory)
   11004              :    register -- though that register may need to be broadcast in some
   11005              :    circumstances.  A variable can only meaningfully be "shared" across workers
   11006              :    or vector lanes if its address is taken, e.g. by a call to an atomic
   11007              :    builtin.
   11008              : 
   11009              :    From an optimisation perspective, the answer might be fuzzier: maybe
   11010              :    sometimes, using shared memory directly would be faster than
   11011              :    broadcasting.  */
   11012              : 
   11013              : static void
   11014        18997 : oacc_privatization_begin_diagnose_var (const dump_flags_t l_dump_flags,
   11015              :                                        const location_t loc, const tree c,
   11016              :                                        const tree decl)
   11017              : {
   11018        18997 :   const dump_user_location_t d_u_loc
   11019        18997 :     = dump_user_location_t::from_location_t (loc);
   11020              : /* PR100695 "Format decoder, quoting in 'dump_printf' etc." */
   11021              : #if __GNUC__ >= 10
   11022        18997 : # pragma GCC diagnostic push
   11023        18997 : # pragma GCC diagnostic ignored "-Wformat"
   11024              : #endif
   11025        18997 :   dump_printf_loc (l_dump_flags, d_u_loc,
   11026              :                    "variable %<%T%> ", decl);
   11027              : #if __GNUC__ >= 10
   11028        18997 : # pragma GCC diagnostic pop
   11029              : #endif
   11030        18997 :   if (c)
   11031         2797 :     dump_printf (l_dump_flags,
   11032              :                  "in %qs clause ",
   11033         2797 :                  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   11034              :   else
   11035        16200 :     dump_printf (l_dump_flags,
   11036              :                  "declared in block ");
   11037        18997 : }
   11038              : 
   11039              : static bool
   11040        41234 : oacc_privatization_candidate_p (const location_t loc, const tree c,
   11041              :                                 const tree decl)
   11042              : {
   11043        41234 :   dump_flags_t l_dump_flags = get_openacc_privatization_dump_flags ();
   11044              : 
   11045              :   /* There is some differentiation depending on block vs. clause.  */
   11046        41234 :   bool block = !c;
   11047              : 
   11048        41234 :   bool res = true;
   11049              : 
   11050        41234 :   if (res && !VAR_P (decl))
   11051              :     {
   11052              :       /* A PARM_DECL (appearing in a 'private' clause) is expected to have been
   11053              :          privatized into a new VAR_DECL.  */
   11054          712 :       gcc_checking_assert (TREE_CODE (decl) != PARM_DECL);
   11055              : 
   11056          712 :       res = false;
   11057              : 
   11058          712 :       if (dump_enabled_p ())
   11059              :         {
   11060          683 :           oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
   11061          683 :           dump_printf (l_dump_flags,
   11062              :                        "potentially has improper OpenACC privatization level: %qs\n",
   11063          683 :                        get_tree_code_name (TREE_CODE (decl)));
   11064              :         }
   11065              :     }
   11066              : 
   11067        41205 :   if (res && block && TREE_STATIC (decl))
   11068              :     {
   11069           53 :       res = false;
   11070              : 
   11071           53 :       if (dump_enabled_p ())
   11072              :         {
   11073           28 :           oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
   11074           28 :           dump_printf (l_dump_flags,
   11075              :                        "isn%'t candidate for adjusting OpenACC privatization level: %s\n",
   11076              :                        "static");
   11077              :         }
   11078              :     }
   11079              : 
   11080        41209 :   if (res && block && DECL_EXTERNAL (decl))
   11081              :     {
   11082           12 :       res = false;
   11083              : 
   11084           12 :       if (dump_enabled_p ())
   11085              :         {
   11086           12 :           oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
   11087           12 :           dump_printf (l_dump_flags,
   11088              :                        "isn%'t candidate for adjusting OpenACC privatization level: %s\n",
   11089              :                        "external");
   11090              :         }
   11091              :     }
   11092              : 
   11093        41234 :   if (res && !TREE_ADDRESSABLE (decl))
   11094              :     {
   11095        39365 :       res = false;
   11096              : 
   11097        39365 :       if (dump_enabled_p ())
   11098              :         {
   11099        17456 :           oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
   11100        17456 :           dump_printf (l_dump_flags,
   11101              :                        "isn%'t candidate for adjusting OpenACC privatization level: %s\n",
   11102              :                        "not addressable");
   11103              :         }
   11104              :     }
   11105              : 
   11106              :   /* If an artificial variable has been added to a bind, e.g.
   11107              :      a compiler-generated temporary structure used by the Fortran front-end, do
   11108              :      not consider it as a privatization candidate.  Note that variables on
   11109              :      the stack are private per-thread by default: making them "gang-private"
   11110              :      for OpenACC actually means to share a single instance of a variable
   11111              :      amongst all workers and threads spawned within each gang.
   11112              :      At present, no compiler-generated artificial variables require such
   11113              :      sharing semantics, so this is safe.  */
   11114              : 
   11115        18548 :   if (res && block && DECL_ARTIFICIAL (decl))
   11116              :     {
   11117          548 :       res = false;
   11118              : 
   11119          548 :       if (dump_enabled_p ())
   11120              :         {
   11121          360 :           oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
   11122          360 :           dump_printf (l_dump_flags,
   11123              :                        "isn%'t candidate for adjusting OpenACC privatization "
   11124              :                        "level: %s\n", "artificial");
   11125              :         }
   11126              :     }
   11127              : 
   11128        41046 :   if (res)
   11129              :     {
   11130          544 :       if (dump_enabled_p ())
   11131              :         {
   11132          458 :           oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
   11133          458 :           dump_printf (l_dump_flags,
   11134              :                        "is candidate for adjusting OpenACC privatization level\n");
   11135              :         }
   11136              :     }
   11137              : 
   11138        41234 :   if (dump_file && (dump_flags & TDF_DETAILS))
   11139              :     {
   11140            0 :       print_generic_decl (dump_file, decl, dump_flags);
   11141            0 :       fprintf (dump_file, "\n");
   11142              :     }
   11143              : 
   11144        41234 :   return res;
   11145              : }
   11146              : 
   11147              : /* Scan CLAUSES for candidates for adjusting OpenACC privatization level in
   11148              :    CTX.  */
   11149              : 
   11150              : static void
   11151        11325 : oacc_privatization_scan_clause_chain (omp_context *ctx, tree clauses)
   11152              : {
   11153        35223 :   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
   11154        23898 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE)
   11155              :       {
   11156        12986 :         tree decl = OMP_CLAUSE_DECL (c);
   11157              : 
   11158        12986 :         tree new_decl = lookup_decl (decl, ctx);
   11159              : 
   11160        12986 :         if (!oacc_privatization_candidate_p (OMP_CLAUSE_LOCATION (c), c,
   11161              :                                              new_decl))
   11162        12654 :           continue;
   11163              : 
   11164          332 :         gcc_checking_assert
   11165              :           (!ctx->oacc_privatization_candidates.contains (new_decl));
   11166          332 :         ctx->oacc_privatization_candidates.safe_push (new_decl);
   11167              :       }
   11168        11325 : }
   11169              : 
   11170              : /* Scan DECLS for candidates for adjusting OpenACC privatization level in
   11171              :    CTX.  */
   11172              : 
   11173              : static void
   11174        23066 : oacc_privatization_scan_decl_chain (omp_context *ctx, tree decls)
   11175              : {
   11176        51314 :   for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
   11177              :     {
   11178        28248 :       tree new_decl = lookup_decl (decl, ctx);
   11179        28248 :       gcc_checking_assert (new_decl == decl);
   11180              : 
   11181        28248 :       if (!oacc_privatization_candidate_p (gimple_location (ctx->stmt), NULL,
   11182              :                                            new_decl))
   11183        28036 :         continue;
   11184              : 
   11185          212 :       gcc_checking_assert
   11186              :         (!ctx->oacc_privatization_candidates.contains (new_decl));
   11187          212 :       ctx->oacc_privatization_candidates.safe_push (new_decl);
   11188              :     }
   11189        23066 : }
   11190              : 
   11191              : /* Callback for walk_gimple_seq.  Find #pragma omp scan statement.  */
   11192              : 
   11193              : static tree
   11194         2411 : omp_find_scan (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
   11195              :                struct walk_stmt_info *wi)
   11196              : {
   11197         2411 :   gimple *stmt = gsi_stmt (*gsi_p);
   11198              : 
   11199         2411 :   *handled_ops_p = true;
   11200         2411 :   switch (gimple_code (stmt))
   11201              :     {
   11202          472 :     WALK_SUBSTMTS;
   11203              : 
   11204          186 :     case GIMPLE_OMP_FOR:
   11205          186 :       if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_SIMD
   11206          186 :           && gimple_omp_for_combined_into_p (stmt))
   11207          186 :         *handled_ops_p = false;
   11208              :       break;
   11209              : 
   11210          746 :     case GIMPLE_OMP_SCAN:
   11211          746 :       *(gimple_stmt_iterator *) (wi->info) = *gsi_p;
   11212          746 :       return integer_zero_node;
   11213              :     default:
   11214              :       break;
   11215              :     }
   11216              :   return NULL;
   11217              : }
   11218              : 
   11219              : /* Helper function for lower_omp_for, add transformations for a worksharing
   11220              :    loop with scan directives inside of it.
   11221              :    For worksharing loop not combined with simd, transform:
   11222              :    #pragma omp for reduction(inscan,+:r) private(i)
   11223              :    for (i = 0; i < n; i = i + 1)
   11224              :      {
   11225              :        {
   11226              :          update (r);
   11227              :        }
   11228              :        #pragma omp scan inclusive(r)
   11229              :        {
   11230              :          use (r);
   11231              :        }
   11232              :      }
   11233              : 
   11234              :    into two worksharing loops + code to merge results:
   11235              : 
   11236              :    num_threads = omp_get_num_threads ();
   11237              :    thread_num = omp_get_thread_num ();
   11238              :    if (thread_num == 0) goto <D.2099>; else goto <D.2100>;
   11239              :    <D.2099>:
   11240              :    var2 = r;
   11241              :    goto <D.2101>;
   11242              :    <D.2100>:
   11243              :    // For UDRs this is UDR init, or if ctors are needed, copy from
   11244              :    // var3 that has been constructed to contain the neutral element.
   11245              :    var2 = 0;
   11246              :    <D.2101>:
   11247              :    ivar = 0;
   11248              :    // The _scantemp_ clauses will arrange for rpriva to be initialized to
   11249              :    // a shared array with num_threads elements and rprivb to a local array
   11250              :    // number of elements equal to the number of (contiguous) iterations the
   11251              :    // current thread will perform.  controlb and controlp variables are
   11252              :    // temporaries to handle deallocation of rprivb at the end of second
   11253              :    // GOMP_FOR.
   11254              :    #pragma omp for _scantemp_(rpriva) _scantemp_(rprivb) _scantemp_(controlb) \
   11255              :      _scantemp_(controlp) reduction(inscan,+:r) private(i) nowait
   11256              :    for (i = 0; i < n; i = i + 1)
   11257              :      {
   11258              :        {
   11259              :          // For UDRs this is UDR init or copy from var3.
   11260              :          r = 0;
   11261              :          // This is the input phase from user code.
   11262              :          update (r);
   11263              :        }
   11264              :        {
   11265              :          // For UDRs this is UDR merge.
   11266              :          var2 = var2 + r;
   11267              :          // Rather than handing it over to the user, save to local thread's
   11268              :          // array.
   11269              :          rprivb[ivar] = var2;
   11270              :          // For exclusive scan, the above two statements are swapped.
   11271              :          ivar = ivar + 1;
   11272              :        }
   11273              :      }
   11274              :    // And remember the final value from this thread's into the shared
   11275              :    // rpriva array.
   11276              :    rpriva[(sizetype) thread_num] = var2;
   11277              :    // If more than one thread, compute using Work-Efficient prefix sum
   11278              :    // the inclusive parallel scan of the rpriva array.
   11279              :    if (num_threads > 1) goto <D.2102>; else goto <D.2103>;
   11280              :    <D.2102>:
   11281              :    GOMP_barrier ();
   11282              :    down = 0;
   11283              :    k = 1;
   11284              :    num_threadsu = (unsigned int) num_threads;
   11285              :    thread_numup1 = (unsigned int) thread_num + 1;
   11286              :    <D.2108>:
   11287              :    twok = k << 1;
   11288              :    if (twok > num_threadsu) goto <D.2110>; else goto <D.2111>;
   11289              :    <D.2110>:
   11290              :    down = 4294967295;
   11291              :    k = k >> 1;
   11292              :    if (k == num_threadsu) goto <D.2112>; else goto <D.2111>;
   11293              :    <D.2112>:
   11294              :    k = k >> 1;
   11295              :    <D.2111>:
   11296              :    twok = k << 1;
   11297              :    cplx = .MUL_OVERFLOW (thread_nump1, twok);
   11298              :    mul = REALPART_EXPR <cplx>;
   11299              :    ovf = IMAGPART_EXPR <cplx>;
   11300              :    if (ovf == 0) goto <D.2116>; else goto <D.2117>;
   11301              :    <D.2116>:
   11302              :    andv = k & down;
   11303              :    andvm1 = andv + 4294967295;
   11304              :    l = mul + andvm1;
   11305              :    if (l < num_threadsu) goto <D.2120>; else goto <D.2117>;
   11306              :    <D.2120>:
   11307              :    // For UDRs this is UDR merge, performed using var2 variable as temporary,
   11308              :    // i.e. var2 = rpriva[l - k]; UDR merge (var2, rpriva[l]); rpriva[l] = var2;
   11309              :    rpriva[l] = rpriva[l - k] + rpriva[l];
   11310              :    <D.2117>:
   11311              :    if (down == 0) goto <D.2121>; else goto <D.2122>;
   11312              :    <D.2121>:
   11313              :    k = k << 1;
   11314              :    goto <D.2123>;
   11315              :    <D.2122>:
   11316              :    k = k >> 1;
   11317              :    <D.2123>:
   11318              :    GOMP_barrier ();
   11319              :    if (k != 0) goto <D.2108>; else goto <D.2103>;
   11320              :    <D.2103>:
   11321              :    if (thread_num == 0) goto <D.2124>; else goto <D.2125>;
   11322              :    <D.2124>:
   11323              :    // For UDRs this is UDR init or copy from var3.
   11324              :    var2 = 0;
   11325              :    goto <D.2126>;
   11326              :    <D.2125>:
   11327              :    var2 = rpriva[thread_num - 1];
   11328              :    <D.2126>:
   11329              :    ivar = 0;
   11330              :    #pragma omp for _scantemp_(controlb) _scantemp_(controlp) \
   11331              :      reduction(inscan,+:r) private(i)
   11332              :    for (i = 0; i < n; i = i + 1)
   11333              :      {
   11334              :        {
   11335              :          // For UDRs, this is r = var2; UDR merge (r, rprivb[ivar]);
   11336              :          r = var2 + rprivb[ivar];
   11337              :        }
   11338              :        {
   11339              :          // This is the scan phase from user code.
   11340              :          use (r);
   11341              :          // Plus a bump of the iterator.
   11342              :          ivar = ivar + 1;
   11343              :        }
   11344              :      }  */
   11345              : 
   11346              : static void
   11347          187 : lower_omp_for_scan (gimple_seq *body_p, gimple_seq *dlist, gomp_for *stmt,
   11348              :                     struct omp_for_data *fd, omp_context *ctx)
   11349              : {
   11350          187 :   bool is_for_simd = gimple_omp_for_combined_p (stmt);
   11351          187 :   gcc_assert (ctx->scan_inclusive || ctx->scan_exclusive);
   11352              : 
   11353          187 :   gimple_seq body = gimple_omp_body (stmt);
   11354          187 :   gimple_stmt_iterator input1_gsi = gsi_none ();
   11355          187 :   struct walk_stmt_info wi;
   11356          187 :   memset (&wi, 0, sizeof (wi));
   11357          187 :   wi.val_only = true;
   11358          187 :   wi.info = (void *) &input1_gsi;
   11359          187 :   walk_gimple_seq_mod (&body, omp_find_scan, NULL, &wi);
   11360          187 :   gcc_assert (!gsi_end_p (input1_gsi));
   11361              : 
   11362          187 :   gimple *input_stmt1 = gsi_stmt (input1_gsi);
   11363          187 :   gimple_stmt_iterator gsi = input1_gsi;
   11364          187 :   gsi_next (&gsi);
   11365          187 :   gimple_stmt_iterator scan1_gsi = gsi;
   11366          187 :   gimple *scan_stmt1 = gsi_stmt (gsi);
   11367          187 :   gcc_assert (scan_stmt1 && gimple_code (scan_stmt1) == GIMPLE_OMP_SCAN);
   11368              : 
   11369          187 :   gimple_seq input_body = gimple_omp_body (input_stmt1);
   11370          187 :   gimple_seq scan_body = gimple_omp_body (scan_stmt1);
   11371          187 :   gimple_omp_set_body (input_stmt1, NULL);
   11372          187 :   gimple_omp_set_body (scan_stmt1, NULL);
   11373          187 :   gimple_omp_set_body (stmt, NULL);
   11374              : 
   11375          187 :   gomp_for *new_stmt = as_a <gomp_for *> (gimple_copy (stmt));
   11376          187 :   gimple_seq new_body = copy_gimple_seq_and_replace_locals (body);
   11377          187 :   gimple_omp_set_body (stmt, body);
   11378          187 :   gimple_omp_set_body (input_stmt1, input_body);
   11379              : 
   11380          187 :   gimple_stmt_iterator input2_gsi = gsi_none ();
   11381          187 :   memset (&wi, 0, sizeof (wi));
   11382          187 :   wi.val_only = true;
   11383          187 :   wi.info = (void *) &input2_gsi;
   11384          187 :   walk_gimple_seq_mod (&new_body, omp_find_scan, NULL, &wi);
   11385          187 :   gcc_assert (!gsi_end_p (input2_gsi));
   11386              : 
   11387          187 :   gimple *input_stmt2 = gsi_stmt (input2_gsi);
   11388          187 :   gsi = input2_gsi;
   11389          187 :   gsi_next (&gsi);
   11390          187 :   gimple_stmt_iterator scan2_gsi = gsi;
   11391          187 :   gimple *scan_stmt2 = gsi_stmt (gsi);
   11392          187 :   gcc_assert (scan_stmt2 && gimple_code (scan_stmt2) == GIMPLE_OMP_SCAN);
   11393          187 :   gimple_omp_set_body (scan_stmt2, scan_body);
   11394              : 
   11395          187 :   gimple_stmt_iterator input3_gsi = gsi_none ();
   11396          187 :   gimple_stmt_iterator scan3_gsi = gsi_none ();
   11397          187 :   gimple_stmt_iterator input4_gsi = gsi_none ();
   11398          187 :   gimple_stmt_iterator scan4_gsi = gsi_none ();
   11399          187 :   gimple *input_stmt3 = NULL, *scan_stmt3 = NULL;
   11400          187 :   gimple *input_stmt4 = NULL, *scan_stmt4 = NULL;
   11401          187 :   omp_context *input_simd_ctx = NULL, *scan_simd_ctx = NULL;
   11402          187 :   if (is_for_simd)
   11403              :     {
   11404           93 :       memset (&wi, 0, sizeof (wi));
   11405           93 :       wi.val_only = true;
   11406           93 :       wi.info = (void *) &input3_gsi;
   11407           93 :       walk_gimple_seq_mod (&input_body, omp_find_scan, NULL, &wi);
   11408           93 :       gcc_assert (!gsi_end_p (input3_gsi));
   11409              : 
   11410           93 :       input_stmt3 = gsi_stmt (input3_gsi);
   11411           93 :       gsi = input3_gsi;
   11412           93 :       gsi_next (&gsi);
   11413           93 :       scan3_gsi = gsi;
   11414           93 :       scan_stmt3 = gsi_stmt (gsi);
   11415           93 :       gcc_assert (scan_stmt3 && gimple_code (scan_stmt3) == GIMPLE_OMP_SCAN);
   11416              : 
   11417           93 :       memset (&wi, 0, sizeof (wi));
   11418           93 :       wi.val_only = true;
   11419           93 :       wi.info = (void *) &input4_gsi;
   11420           93 :       walk_gimple_seq_mod (&scan_body, omp_find_scan, NULL, &wi);
   11421           93 :       gcc_assert (!gsi_end_p (input4_gsi));
   11422              : 
   11423           93 :       input_stmt4 = gsi_stmt (input4_gsi);
   11424           93 :       gsi = input4_gsi;
   11425           93 :       gsi_next (&gsi);
   11426           93 :       scan4_gsi = gsi;
   11427           93 :       scan_stmt4 = gsi_stmt (gsi);
   11428           93 :       gcc_assert (scan_stmt4 && gimple_code (scan_stmt4) == GIMPLE_OMP_SCAN);
   11429              : 
   11430           93 :       input_simd_ctx = maybe_lookup_ctx (input_stmt3)->outer;
   11431           93 :       scan_simd_ctx = maybe_lookup_ctx (input_stmt4)->outer;
   11432              :     }
   11433              : 
   11434          187 :   tree num_threads = create_tmp_var (integer_type_node);
   11435          187 :   tree thread_num = create_tmp_var (integer_type_node);
   11436          187 :   tree nthreads_decl = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
   11437          187 :   tree threadnum_decl = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
   11438          187 :   gimple *g = gimple_build_call (nthreads_decl, 0);
   11439          187 :   gimple_call_set_lhs (g, num_threads);
   11440          187 :   gimple_seq_add_stmt (body_p, g);
   11441          187 :   g = gimple_build_call (threadnum_decl, 0);
   11442          187 :   gimple_call_set_lhs (g, thread_num);
   11443          187 :   gimple_seq_add_stmt (body_p, g);
   11444              : 
   11445          187 :   tree ivar = create_tmp_var (sizetype);
   11446          187 :   tree new_clauses1 = NULL_TREE, new_clauses2 = NULL_TREE;
   11447          187 :   tree *cp1 = &new_clauses1, *cp2 = &new_clauses2;
   11448          187 :   tree k = create_tmp_var (unsigned_type_node);
   11449          187 :   tree l = create_tmp_var (unsigned_type_node);
   11450              : 
   11451          187 :   gimple_seq clist = NULL, mdlist = NULL;
   11452          187 :   gimple_seq thr01_list = NULL, thrn1_list = NULL;
   11453          187 :   gimple_seq thr02_list = NULL, thrn2_list = NULL;
   11454          187 :   gimple_seq scan1_list = NULL, input2_list = NULL;
   11455          187 :   gimple_seq last_list = NULL, reduc_list = NULL;
   11456          730 :   for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
   11457          543 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   11458          543 :         && OMP_CLAUSE_REDUCTION_INSCAN (c))
   11459              :       {
   11460          219 :         location_t clause_loc = OMP_CLAUSE_LOCATION (c);
   11461          219 :         tree var = OMP_CLAUSE_DECL (c);
   11462          219 :         tree new_var = lookup_decl (var, ctx);
   11463          219 :         tree var3 = NULL_TREE;
   11464          219 :         tree new_vard = new_var;
   11465          219 :         if (omp_privatize_by_reference (var))
   11466           32 :           new_var = build_simple_mem_ref_loc (clause_loc, new_var);
   11467          219 :         if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
   11468              :           {
   11469           64 :             var3 = maybe_lookup_decl (new_vard, ctx);
   11470           64 :             if (var3 == new_vard)
   11471          187 :               var3 = NULL_TREE;
   11472              :           }
   11473              : 
   11474          219 :         tree ptype = build_pointer_type (TREE_TYPE (new_var));
   11475          219 :         tree rpriva = create_tmp_var (ptype);
   11476          219 :         tree nc = build_omp_clause (clause_loc, OMP_CLAUSE__SCANTEMP_);
   11477          219 :         OMP_CLAUSE_DECL (nc) = rpriva;
   11478          219 :         *cp1 = nc;
   11479          219 :         cp1 = &OMP_CLAUSE_CHAIN (nc);
   11480              : 
   11481          219 :         tree rprivb = create_tmp_var (ptype);
   11482          219 :         nc = build_omp_clause (clause_loc, OMP_CLAUSE__SCANTEMP_);
   11483          219 :         OMP_CLAUSE_DECL (nc) = rprivb;
   11484          219 :         OMP_CLAUSE__SCANTEMP__ALLOC (nc) = 1;
   11485          219 :         *cp1 = nc;
   11486          219 :         cp1 = &OMP_CLAUSE_CHAIN (nc);
   11487              : 
   11488          219 :         tree var2 = create_tmp_var_raw (TREE_TYPE (new_var));
   11489          219 :         if (new_vard != new_var)
   11490           32 :           TREE_ADDRESSABLE (var2) = 1;
   11491          219 :         gimple_add_tmp_var (var2);
   11492              : 
   11493          219 :         tree x = fold_convert_loc (clause_loc, sizetype, thread_num);
   11494          219 :         x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
   11495          219 :                              TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
   11496          219 :         x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
   11497          219 :         tree rpriva_ref = build_simple_mem_ref_loc (clause_loc, x);
   11498              : 
   11499          219 :         x = fold_build2_loc (clause_loc, PLUS_EXPR, integer_type_node,
   11500              :                              thread_num, integer_minus_one_node);
   11501          219 :         x = fold_convert_loc (clause_loc, sizetype, x);
   11502          219 :         x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
   11503          219 :                              TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
   11504          219 :         x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
   11505          219 :         tree rprivam1_ref = build_simple_mem_ref_loc (clause_loc, x);
   11506              : 
   11507          219 :         x = fold_convert_loc (clause_loc, sizetype, l);
   11508          219 :         x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
   11509          219 :                              TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
   11510          219 :         x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
   11511          219 :         tree rprival_ref = build_simple_mem_ref_loc (clause_loc, x);
   11512              : 
   11513          219 :         x = fold_build2_loc (clause_loc, MINUS_EXPR, unsigned_type_node, l, k);
   11514          219 :         x = fold_convert_loc (clause_loc, sizetype, x);
   11515          219 :         x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
   11516          219 :                              TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
   11517          219 :         x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
   11518          219 :         tree rprivalmk_ref = build_simple_mem_ref_loc (clause_loc, x);
   11519              : 
   11520          219 :         x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, ivar,
   11521          219 :                              TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
   11522          219 :         x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rprivb), rprivb, x);
   11523          219 :         tree rprivb_ref = build_simple_mem_ref_loc (clause_loc, x);
   11524              : 
   11525          219 :         tree var4 = is_for_simd ? new_var : var2;
   11526          109 :         tree var5 = NULL_TREE, var6 = NULL_TREE;
   11527          109 :         if (is_for_simd)
   11528              :           {
   11529          109 :             var5 = lookup_decl (var, input_simd_ctx);
   11530          109 :             var6 = lookup_decl (var, scan_simd_ctx);
   11531          109 :             if (new_vard != new_var)
   11532              :               {
   11533           16 :                 var5 = build_simple_mem_ref_loc (clause_loc, var5);
   11534           16 :                 var6 = build_simple_mem_ref_loc (clause_loc, var6);
   11535              :               }
   11536              :           }
   11537          219 :         if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
   11538              :           {
   11539           64 :             tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
   11540           64 :             tree val = var2;
   11541              : 
   11542           64 :             x = lang_hooks.decls.omp_clause_default_ctor
   11543           64 :                     (c, var2, build_outer_var_ref (var, ctx));
   11544           64 :             if (x)
   11545           32 :               gimplify_and_add (x, &clist);
   11546              : 
   11547           64 :             x = build_outer_var_ref (var, ctx);
   11548           64 :             x = lang_hooks.decls.omp_clause_assign_op (c, unshare_expr (var4),
   11549              :                                                        x);
   11550           64 :             gimplify_and_add (x, &thr01_list);
   11551              : 
   11552           64 :             tree y = (DECL_HAS_VALUE_EXPR_P (new_vard)
   11553           64 :                       ? DECL_VALUE_EXPR (new_vard) : NULL_TREE);
   11554           64 :             if (var3)
   11555              :               {
   11556           32 :                 x = unshare_expr (var4);
   11557           32 :                 x = lang_hooks.decls.omp_clause_assign_op (c, x, var3);
   11558           32 :                 gimplify_and_add (x, &thrn1_list);
   11559           32 :                 x = unshare_expr (var4);
   11560           32 :                 x = lang_hooks.decls.omp_clause_assign_op (c, x, var3);
   11561           32 :                 gimplify_and_add (x, &thr02_list);
   11562              :               }
   11563           32 :             else if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
   11564              :               {
   11565              :                 /* Otherwise, assign to it the identity element.  */
   11566           32 :                 gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
   11567           32 :                 tseq = copy_gimple_seq_and_replace_locals (tseq);
   11568           32 :                 if (!is_for_simd)
   11569              :                   {
   11570           16 :                     if (new_vard != new_var)
   11571            4 :                       val = build_fold_addr_expr_loc (clause_loc, val);
   11572           16 :                     SET_DECL_VALUE_EXPR (new_vard, val);
   11573           16 :                     DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
   11574              :                   }
   11575           32 :                 SET_DECL_VALUE_EXPR (placeholder, error_mark_node);
   11576           32 :                 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
   11577           32 :                 lower_omp (&tseq, ctx);
   11578           32 :                 gimple_seq_add_seq (&thrn1_list, tseq);
   11579           32 :                 tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
   11580           32 :                 lower_omp (&tseq, ctx);
   11581           32 :                 gimple_seq_add_seq (&thr02_list, tseq);
   11582           32 :                 SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
   11583           32 :                 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
   11584           32 :                 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
   11585           32 :                 if (y)
   11586            0 :                   SET_DECL_VALUE_EXPR (new_vard, y);
   11587              :                 else
   11588              :                   {
   11589           32 :                     DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
   11590           32 :                     SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
   11591              :                   }
   11592              :               }
   11593              : 
   11594           64 :             x = unshare_expr (var4);
   11595           64 :             x = lang_hooks.decls.omp_clause_assign_op (c, x, rprivam1_ref);
   11596           64 :             gimplify_and_add (x, &thrn2_list);
   11597              : 
   11598           64 :             if (is_for_simd)
   11599              :               {
   11600           32 :                 x = unshare_expr (rprivb_ref);
   11601           32 :                 x = lang_hooks.decls.omp_clause_assign_op (c, x, var5);
   11602           32 :                 gimplify_and_add (x, &scan1_list);
   11603              :               }
   11604              :             else
   11605              :               {
   11606           32 :                 if (ctx->scan_exclusive)
   11607              :                   {
   11608           16 :                     x = unshare_expr (rprivb_ref);
   11609           16 :                     x = lang_hooks.decls.omp_clause_assign_op (c, x, var2);
   11610           16 :                     gimplify_and_add (x, &scan1_list);
   11611              :                   }
   11612              : 
   11613           32 :                 gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
   11614           32 :                 tseq = copy_gimple_seq_and_replace_locals (tseq);
   11615           32 :                 SET_DECL_VALUE_EXPR (placeholder, var2);
   11616           32 :                 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
   11617           32 :                 lower_omp (&tseq, ctx);
   11618           32 :                 gimple_seq_add_seq (&scan1_list, tseq);
   11619              : 
   11620           32 :                 if (ctx->scan_inclusive)
   11621              :                   {
   11622           16 :                     x = unshare_expr (rprivb_ref);
   11623           16 :                     x = lang_hooks.decls.omp_clause_assign_op (c, x, var2);
   11624           16 :                     gimplify_and_add (x, &scan1_list);
   11625              :                   }
   11626              :               }
   11627              : 
   11628           64 :             x = unshare_expr (rpriva_ref);
   11629           64 :             x = lang_hooks.decls.omp_clause_assign_op (c, x,
   11630              :                                                        unshare_expr (var4));
   11631           64 :             gimplify_and_add (x, &mdlist);
   11632              : 
   11633           96 :             x = unshare_expr (is_for_simd ? var6 : new_var);
   11634           64 :             x = lang_hooks.decls.omp_clause_assign_op (c, x, var4);
   11635           64 :             gimplify_and_add (x, &input2_list);
   11636              : 
   11637           64 :             val = rprivb_ref;
   11638           64 :             if (new_vard != new_var)
   11639           24 :               val = build_fold_addr_expr_loc (clause_loc, val);
   11640              : 
   11641           64 :             gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
   11642           64 :             tseq = copy_gimple_seq_and_replace_locals (tseq);
   11643           64 :             SET_DECL_VALUE_EXPR (new_vard, val);
   11644           64 :             DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
   11645           64 :             if (is_for_simd)
   11646              :               {
   11647           32 :                 SET_DECL_VALUE_EXPR (placeholder, var6);
   11648           32 :                 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
   11649              :               }
   11650              :             else
   11651           32 :               DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
   11652           64 :             lower_omp (&tseq, ctx);
   11653           64 :             if (y)
   11654            0 :               SET_DECL_VALUE_EXPR (new_vard, y);
   11655              :             else
   11656              :               {
   11657           64 :                 DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
   11658           64 :                 SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
   11659              :               }
   11660           64 :             if (!is_for_simd)
   11661              :               {
   11662           32 :                 SET_DECL_VALUE_EXPR (placeholder, new_var);
   11663           32 :                 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
   11664           32 :                 lower_omp (&tseq, ctx);
   11665              :               }
   11666           64 :             gimple_seq_add_seq (&input2_list, tseq);
   11667              : 
   11668           64 :             x = build_outer_var_ref (var, ctx);
   11669           64 :             x = lang_hooks.decls.omp_clause_assign_op (c, x, rpriva_ref);
   11670           64 :             gimplify_and_add (x, &last_list);
   11671              : 
   11672           64 :             x = lang_hooks.decls.omp_clause_assign_op (c, var2, rprivalmk_ref);
   11673           64 :             gimplify_and_add (x, &reduc_list);
   11674           64 :             tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
   11675           64 :             tseq = copy_gimple_seq_and_replace_locals (tseq);
   11676           64 :             val = rprival_ref;
   11677           64 :             if (new_vard != new_var)
   11678           24 :               val = build_fold_addr_expr_loc (clause_loc, val);
   11679           64 :             SET_DECL_VALUE_EXPR (new_vard, val);
   11680           64 :             DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
   11681           64 :             SET_DECL_VALUE_EXPR (placeholder, var2);
   11682           64 :             lower_omp (&tseq, ctx);
   11683           64 :             OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
   11684           64 :             SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
   11685           64 :             DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
   11686           64 :             if (y)
   11687            0 :               SET_DECL_VALUE_EXPR (new_vard, y);
   11688              :             else
   11689              :               {
   11690           64 :                 DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
   11691           64 :                 SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
   11692              :               }
   11693           64 :             gimple_seq_add_seq (&reduc_list, tseq);
   11694           64 :             x = lang_hooks.decls.omp_clause_assign_op (c, rprival_ref, var2);
   11695           64 :             gimplify_and_add (x, &reduc_list);
   11696              : 
   11697           64 :             x = lang_hooks.decls.omp_clause_dtor (c, var2);
   11698           64 :             if (x)
   11699           32 :               gimplify_and_add (x, dlist);
   11700              :           }
   11701              :         else
   11702              :           {
   11703          155 :             x = build_outer_var_ref (var, ctx);
   11704          155 :             gimplify_assign (unshare_expr (var4), x, &thr01_list);
   11705              : 
   11706          155 :             x = omp_reduction_init (c, TREE_TYPE (new_var));
   11707          155 :             gimplify_assign (unshare_expr (var4), unshare_expr (x),
   11708              :                              &thrn1_list);
   11709          155 :             gimplify_assign (unshare_expr (var4), x, &thr02_list);
   11710              : 
   11711          155 :             gimplify_assign (unshare_expr (var4), rprivam1_ref, &thrn2_list);
   11712              : 
   11713          155 :             enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
   11714          155 :             if (code == MINUS_EXPR)
   11715            0 :               code = PLUS_EXPR;
   11716              : 
   11717          155 :             if (is_for_simd)
   11718           77 :               gimplify_assign (unshare_expr (rprivb_ref), var5, &scan1_list);
   11719              :             else
   11720              :               {
   11721           78 :                 if (ctx->scan_exclusive)
   11722           28 :                   gimplify_assign (unshare_expr (rprivb_ref), var2,
   11723              :                                    &scan1_list);
   11724           78 :                 x = build2 (code, TREE_TYPE (new_var), var2, new_var);
   11725           78 :                 gimplify_assign (var2, x, &scan1_list);
   11726           78 :                 if (ctx->scan_inclusive)
   11727           50 :                   gimplify_assign (unshare_expr (rprivb_ref), var2,
   11728              :                                    &scan1_list);
   11729              :               }
   11730              : 
   11731          155 :             gimplify_assign (unshare_expr (rpriva_ref), unshare_expr (var4),
   11732              :                              &mdlist);
   11733              : 
   11734          155 :             x = build2 (code, TREE_TYPE (new_var), var4, rprivb_ref);
   11735          233 :             gimplify_assign (is_for_simd ? var6 : new_var, x, &input2_list);
   11736              : 
   11737          155 :             gimplify_assign (build_outer_var_ref (var, ctx), rpriva_ref,
   11738              :                              &last_list);
   11739              : 
   11740          155 :             x = build2 (code, TREE_TYPE (new_var), rprivalmk_ref,
   11741              :                         unshare_expr (rprival_ref));
   11742          155 :             gimplify_assign (rprival_ref, x, &reduc_list);
   11743              :           }
   11744              :       }
   11745              : 
   11746          187 :   g = gimple_build_assign (ivar, PLUS_EXPR, ivar, size_one_node);
   11747          187 :   gimple_seq_add_stmt (&scan1_list, g);
   11748          187 :   g = gimple_build_assign (ivar, PLUS_EXPR, ivar, size_one_node);
   11749          281 :   gimple_seq_add_stmt (gimple_omp_body_ptr (is_for_simd
   11750              :                                             ? scan_stmt4 : scan_stmt2), g);
   11751              : 
   11752          187 :   tree controlb = create_tmp_var (boolean_type_node);
   11753          187 :   tree controlp = create_tmp_var (ptr_type_node);
   11754          187 :   tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
   11755          187 :   OMP_CLAUSE_DECL (nc) = controlb;
   11756          187 :   OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
   11757          187 :   *cp1 = nc;
   11758          187 :   cp1 = &OMP_CLAUSE_CHAIN (nc);
   11759          187 :   nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
   11760          187 :   OMP_CLAUSE_DECL (nc) = controlp;
   11761          187 :   OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
   11762          187 :   *cp1 = nc;
   11763          187 :   cp1 = &OMP_CLAUSE_CHAIN (nc);
   11764          187 :   nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
   11765          187 :   OMP_CLAUSE_DECL (nc) = controlb;
   11766          187 :   OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
   11767          187 :   *cp2 = nc;
   11768          187 :   cp2 = &OMP_CLAUSE_CHAIN (nc);
   11769          187 :   nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
   11770          187 :   OMP_CLAUSE_DECL (nc) = controlp;
   11771          187 :   OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
   11772          187 :   *cp2 = nc;
   11773          187 :   cp2 = &OMP_CLAUSE_CHAIN (nc);
   11774              : 
   11775          187 :   *cp1 = gimple_omp_for_clauses (stmt);
   11776          187 :   gimple_omp_for_set_clauses (stmt, new_clauses1);
   11777          187 :   *cp2 = gimple_omp_for_clauses (new_stmt);
   11778          187 :   gimple_omp_for_set_clauses (new_stmt, new_clauses2);
   11779              : 
   11780          187 :   if (is_for_simd)
   11781              :     {
   11782           93 :       gimple_seq_add_seq (gimple_omp_body_ptr (scan_stmt3), scan1_list);
   11783           93 :       gimple_seq_add_seq (gimple_omp_body_ptr (input_stmt4), input2_list);
   11784              : 
   11785           93 :       gsi_insert_seq_after (&input3_gsi, gimple_omp_body (input_stmt3),
   11786              :                             GSI_SAME_STMT);
   11787           93 :       gsi_remove (&input3_gsi, true);
   11788           93 :       gsi_insert_seq_after (&scan3_gsi, gimple_omp_body (scan_stmt3),
   11789              :                             GSI_SAME_STMT);
   11790           93 :       gsi_remove (&scan3_gsi, true);
   11791           93 :       gsi_insert_seq_after (&input4_gsi, gimple_omp_body (input_stmt4),
   11792              :                             GSI_SAME_STMT);
   11793           93 :       gsi_remove (&input4_gsi, true);
   11794           93 :       gsi_insert_seq_after (&scan4_gsi, gimple_omp_body (scan_stmt4),
   11795              :                             GSI_SAME_STMT);
   11796           93 :       gsi_remove (&scan4_gsi, true);
   11797              :     }
   11798              :   else
   11799              :     {
   11800           94 :       gimple_omp_set_body (scan_stmt1, scan1_list);
   11801           94 :       gimple_omp_set_body (input_stmt2, input2_list);
   11802              :     }
   11803              : 
   11804          187 :   gsi_insert_seq_after (&input1_gsi, gimple_omp_body (input_stmt1),
   11805              :                         GSI_SAME_STMT);
   11806          187 :   gsi_remove (&input1_gsi, true);
   11807          187 :   gsi_insert_seq_after (&scan1_gsi, gimple_omp_body (scan_stmt1),
   11808              :                         GSI_SAME_STMT);
   11809          187 :   gsi_remove (&scan1_gsi, true);
   11810          187 :   gsi_insert_seq_after (&input2_gsi, gimple_omp_body (input_stmt2),
   11811              :                         GSI_SAME_STMT);
   11812          187 :   gsi_remove (&input2_gsi, true);
   11813          187 :   gsi_insert_seq_after (&scan2_gsi, gimple_omp_body (scan_stmt2),
   11814              :                         GSI_SAME_STMT);
   11815          187 :   gsi_remove (&scan2_gsi, true);
   11816              : 
   11817          187 :   gimple_seq_add_seq (body_p, clist);
   11818              : 
   11819          187 :   tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
   11820          187 :   tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
   11821          187 :   tree lab3 = create_artificial_label (UNKNOWN_LOCATION);
   11822          187 :   g = gimple_build_cond (EQ_EXPR, thread_num, integer_zero_node, lab1, lab2);
   11823          187 :   gimple_seq_add_stmt (body_p, g);
   11824          187 :   g = gimple_build_label (lab1);
   11825          187 :   gimple_seq_add_stmt (body_p, g);
   11826          187 :   gimple_seq_add_seq (body_p, thr01_list);
   11827          187 :   g = gimple_build_goto (lab3);
   11828          187 :   gimple_seq_add_stmt (body_p, g);
   11829          187 :   g = gimple_build_label (lab2);
   11830          187 :   gimple_seq_add_stmt (body_p, g);
   11831          187 :   gimple_seq_add_seq (body_p, thrn1_list);
   11832          187 :   g = gimple_build_label (lab3);
   11833          187 :   gimple_seq_add_stmt (body_p, g);
   11834              : 
   11835          187 :   g = gimple_build_assign (ivar, size_zero_node);
   11836          187 :   gimple_seq_add_stmt (body_p, g);
   11837              : 
   11838          187 :   gimple_seq_add_stmt (body_p, stmt);
   11839          187 :   gimple_seq_add_seq (body_p, body);
   11840          187 :   gimple_seq_add_stmt (body_p, gimple_build_omp_continue (fd->loop.v,
   11841              :                                                           fd->loop.v));
   11842              : 
   11843          187 :   g = gimple_build_omp_return (true);
   11844          187 :   gimple_seq_add_stmt (body_p, g);
   11845          187 :   gimple_seq_add_seq (body_p, mdlist);
   11846              : 
   11847          187 :   lab1 = create_artificial_label (UNKNOWN_LOCATION);
   11848          187 :   lab2 = create_artificial_label (UNKNOWN_LOCATION);
   11849          187 :   g = gimple_build_cond (GT_EXPR, num_threads, integer_one_node, lab1, lab2);
   11850          187 :   gimple_seq_add_stmt (body_p, g);
   11851          187 :   g = gimple_build_label (lab1);
   11852          187 :   gimple_seq_add_stmt (body_p, g);
   11853              : 
   11854          187 :   g = omp_build_barrier (NULL, GOMP_BARRIER_IMPLICIT_WORKSHARE);
   11855          187 :   gimple_seq_add_stmt (body_p, g);
   11856              : 
   11857          187 :   tree down = create_tmp_var (unsigned_type_node);
   11858          187 :   g = gimple_build_assign (down, build_zero_cst (unsigned_type_node));
   11859          187 :   gimple_seq_add_stmt (body_p, g);
   11860              : 
   11861          187 :   g = gimple_build_assign (k, build_one_cst (unsigned_type_node));
   11862          187 :   gimple_seq_add_stmt (body_p, g);
   11863              : 
   11864          187 :   tree num_threadsu = create_tmp_var (unsigned_type_node);
   11865          187 :   g = gimple_build_assign (num_threadsu, NOP_EXPR, num_threads);
   11866          187 :   gimple_seq_add_stmt (body_p, g);
   11867              : 
   11868          187 :   tree thread_numu = create_tmp_var (unsigned_type_node);
   11869          187 :   g = gimple_build_assign (thread_numu, NOP_EXPR, thread_num);
   11870          187 :   gimple_seq_add_stmt (body_p, g);
   11871              : 
   11872          187 :   tree thread_nump1 = create_tmp_var (unsigned_type_node);
   11873          187 :   g = gimple_build_assign (thread_nump1, PLUS_EXPR, thread_numu,
   11874              :                            build_int_cst (unsigned_type_node, 1));
   11875          187 :   gimple_seq_add_stmt (body_p, g);
   11876              : 
   11877          187 :   lab3 = create_artificial_label (UNKNOWN_LOCATION);
   11878          187 :   g = gimple_build_label (lab3);
   11879          187 :   gimple_seq_add_stmt (body_p, g);
   11880              : 
   11881          187 :   tree twok = create_tmp_var (unsigned_type_node);
   11882          187 :   g = gimple_build_assign (twok, LSHIFT_EXPR, k, integer_one_node);
   11883          187 :   gimple_seq_add_stmt (body_p, g);
   11884              : 
   11885          187 :   tree lab4 = create_artificial_label (UNKNOWN_LOCATION);
   11886          187 :   tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
   11887          187 :   tree lab6 = create_artificial_label (UNKNOWN_LOCATION);
   11888          187 :   g = gimple_build_cond (GT_EXPR, twok, num_threadsu, lab4, lab5);
   11889          187 :   gimple_seq_add_stmt (body_p, g);
   11890          187 :   g = gimple_build_label (lab4);
   11891          187 :   gimple_seq_add_stmt (body_p, g);
   11892          187 :   g = gimple_build_assign (down, build_all_ones_cst (unsigned_type_node));
   11893          187 :   gimple_seq_add_stmt (body_p, g);
   11894          187 :   g = gimple_build_assign (k, RSHIFT_EXPR, k, integer_one_node);
   11895          187 :   gimple_seq_add_stmt (body_p, g);
   11896              : 
   11897          187 :   g = gimple_build_cond (EQ_EXPR, k, num_threadsu, lab6, lab5);
   11898          187 :   gimple_seq_add_stmt (body_p, g);
   11899          187 :   g = gimple_build_label (lab6);
   11900          187 :   gimple_seq_add_stmt (body_p, g);
   11901              : 
   11902          187 :   g = gimple_build_assign (k, RSHIFT_EXPR, k, integer_one_node);
   11903          187 :   gimple_seq_add_stmt (body_p, g);
   11904              : 
   11905          187 :   g = gimple_build_label (lab5);
   11906          187 :   gimple_seq_add_stmt (body_p, g);
   11907              : 
   11908          187 :   g = gimple_build_assign (twok, LSHIFT_EXPR, k, integer_one_node);
   11909          187 :   gimple_seq_add_stmt (body_p, g);
   11910              : 
   11911          187 :   tree cplx = create_tmp_var (build_complex_type (unsigned_type_node, false));
   11912          187 :   g = gimple_build_call_internal (IFN_MUL_OVERFLOW, 2, thread_nump1, twok);
   11913          187 :   gimple_call_set_lhs (g, cplx);
   11914          187 :   gimple_seq_add_stmt (body_p, g);
   11915          187 :   tree mul = create_tmp_var (unsigned_type_node);
   11916          187 :   g = gimple_build_assign (mul, REALPART_EXPR,
   11917              :                            build1 (REALPART_EXPR, unsigned_type_node, cplx));
   11918          187 :   gimple_seq_add_stmt (body_p, g);
   11919          187 :   tree ovf = create_tmp_var (unsigned_type_node);
   11920          187 :   g = gimple_build_assign (ovf, IMAGPART_EXPR,
   11921              :                            build1 (IMAGPART_EXPR, unsigned_type_node, cplx));
   11922          187 :   gimple_seq_add_stmt (body_p, g);
   11923              : 
   11924          187 :   tree lab7 = create_artificial_label (UNKNOWN_LOCATION);
   11925          187 :   tree lab8 = create_artificial_label (UNKNOWN_LOCATION);
   11926          187 :   g = gimple_build_cond (EQ_EXPR, ovf, build_zero_cst (unsigned_type_node),
   11927              :                          lab7, lab8);
   11928          187 :   gimple_seq_add_stmt (body_p, g);
   11929          187 :   g = gimple_build_label (lab7);
   11930          187 :   gimple_seq_add_stmt (body_p, g);
   11931              : 
   11932          187 :   tree andv = create_tmp_var (unsigned_type_node);
   11933          187 :   g = gimple_build_assign (andv, BIT_AND_EXPR, k, down);
   11934          187 :   gimple_seq_add_stmt (body_p, g);
   11935          187 :   tree andvm1 = create_tmp_var (unsigned_type_node);
   11936          187 :   g = gimple_build_assign (andvm1, PLUS_EXPR, andv,
   11937              :                            build_minus_one_cst (unsigned_type_node));
   11938          187 :   gimple_seq_add_stmt (body_p, g);
   11939              : 
   11940          187 :   g = gimple_build_assign (l, PLUS_EXPR, mul, andvm1);
   11941          187 :   gimple_seq_add_stmt (body_p, g);
   11942              : 
   11943          187 :   tree lab9 = create_artificial_label (UNKNOWN_LOCATION);
   11944          187 :   g = gimple_build_cond (LT_EXPR, l, num_threadsu, lab9, lab8);
   11945          187 :   gimple_seq_add_stmt (body_p, g);
   11946          187 :   g = gimple_build_label (lab9);
   11947          187 :   gimple_seq_add_stmt (body_p, g);
   11948          187 :   gimple_seq_add_seq (body_p, reduc_list);
   11949          187 :   g = gimple_build_label (lab8);
   11950          187 :   gimple_seq_add_stmt (body_p, g);
   11951              : 
   11952          187 :   tree lab10 = create_artificial_label (UNKNOWN_LOCATION);
   11953          187 :   tree lab11 = create_artificial_label (UNKNOWN_LOCATION);
   11954          187 :   tree lab12 = create_artificial_label (UNKNOWN_LOCATION);
   11955          187 :   g = gimple_build_cond (EQ_EXPR, down, build_zero_cst (unsigned_type_node),
   11956              :                          lab10, lab11);
   11957          187 :   gimple_seq_add_stmt (body_p, g);
   11958          187 :   g = gimple_build_label (lab10);
   11959          187 :   gimple_seq_add_stmt (body_p, g);
   11960          187 :   g = gimple_build_assign (k, LSHIFT_EXPR, k, integer_one_node);
   11961          187 :   gimple_seq_add_stmt (body_p, g);
   11962          187 :   g = gimple_build_goto (lab12);
   11963          187 :   gimple_seq_add_stmt (body_p, g);
   11964          187 :   g = gimple_build_label (lab11);
   11965          187 :   gimple_seq_add_stmt (body_p, g);
   11966          187 :   g = gimple_build_assign (k, RSHIFT_EXPR, k, integer_one_node);
   11967          187 :   gimple_seq_add_stmt (body_p, g);
   11968          187 :   g = gimple_build_label (lab12);
   11969          187 :   gimple_seq_add_stmt (body_p, g);
   11970              : 
   11971          187 :   g = omp_build_barrier (NULL, GOMP_BARRIER_IMPLICIT_WORKSHARE);
   11972          187 :   gimple_seq_add_stmt (body_p, g);
   11973              : 
   11974          187 :   g = gimple_build_cond (NE_EXPR, k, build_zero_cst (unsigned_type_node),
   11975              :                          lab3, lab2);
   11976          187 :   gimple_seq_add_stmt (body_p, g);
   11977              : 
   11978          187 :   g = gimple_build_label (lab2);
   11979          187 :   gimple_seq_add_stmt (body_p, g);
   11980              : 
   11981          187 :   lab1 = create_artificial_label (UNKNOWN_LOCATION);
   11982          187 :   lab2 = create_artificial_label (UNKNOWN_LOCATION);
   11983          187 :   lab3 = create_artificial_label (UNKNOWN_LOCATION);
   11984          187 :   g = gimple_build_cond (EQ_EXPR, thread_num, integer_zero_node, lab1, lab2);
   11985          187 :   gimple_seq_add_stmt (body_p, g);
   11986          187 :   g = gimple_build_label (lab1);
   11987          187 :   gimple_seq_add_stmt (body_p, g);
   11988          187 :   gimple_seq_add_seq (body_p, thr02_list);
   11989          187 :   g = gimple_build_goto (lab3);
   11990          187 :   gimple_seq_add_stmt (body_p, g);
   11991          187 :   g = gimple_build_label (lab2);
   11992          187 :   gimple_seq_add_stmt (body_p, g);
   11993          187 :   gimple_seq_add_seq (body_p, thrn2_list);
   11994          187 :   g = gimple_build_label (lab3);
   11995          187 :   gimple_seq_add_stmt (body_p, g);
   11996              : 
   11997          187 :   g = gimple_build_assign (ivar, size_zero_node);
   11998          187 :   gimple_seq_add_stmt (body_p, g);
   11999          187 :   gimple_seq_add_stmt (body_p, new_stmt);
   12000          187 :   gimple_seq_add_seq (body_p, new_body);
   12001              : 
   12002          187 :   gimple_seq new_dlist = NULL;
   12003          187 :   lab1 = create_artificial_label (UNKNOWN_LOCATION);
   12004          187 :   lab2 = create_artificial_label (UNKNOWN_LOCATION);
   12005          187 :   tree num_threadsm1 = create_tmp_var (integer_type_node);
   12006          187 :   g = gimple_build_assign (num_threadsm1, PLUS_EXPR, num_threads,
   12007              :                            integer_minus_one_node);
   12008          187 :   gimple_seq_add_stmt (&new_dlist, g);
   12009          187 :   g = gimple_build_cond (EQ_EXPR, thread_num, num_threadsm1, lab1, lab2);
   12010          187 :   gimple_seq_add_stmt (&new_dlist, g);
   12011          187 :   g = gimple_build_label (lab1);
   12012          187 :   gimple_seq_add_stmt (&new_dlist, g);
   12013          187 :   gimple_seq_add_seq (&new_dlist, last_list);
   12014          187 :   g = gimple_build_label (lab2);
   12015          187 :   gimple_seq_add_stmt (&new_dlist, g);
   12016          187 :   gimple_seq_add_seq (&new_dlist, *dlist);
   12017          187 :   *dlist = new_dlist;
   12018          187 : }
   12019              : 
   12020              : /* Build an internal UNIQUE function with type IFN_UNIQUE_OACC_PRIVATE listing
   12021              :    the addresses of variables to be made private at the surrounding
   12022              :    parallelism level.  Such functions appear in the gimple code stream in two
   12023              :    forms, e.g. for a partitioned loop:
   12024              : 
   12025              :       .data_dep.6 = .UNIQUE (OACC_HEAD_MARK, .data_dep.6, 1, 68);
   12026              :       .data_dep.6 = .UNIQUE (OACC_PRIVATE, .data_dep.6, -1, &w);
   12027              :       .data_dep.6 = .UNIQUE (OACC_FORK, .data_dep.6, -1);
   12028              :       .data_dep.6 = .UNIQUE (OACC_HEAD_MARK, .data_dep.6);
   12029              : 
   12030              :    or alternatively, OACC_PRIVATE can appear at the top level of a parallel,
   12031              :    not as part of a HEAD_MARK sequence:
   12032              : 
   12033              :       .UNIQUE (OACC_PRIVATE, 0, 0, &w);
   12034              : 
   12035              :    For such stand-alone appearances, the 3rd argument is always 0, denoting
   12036              :    gang partitioning.  */
   12037              : 
   12038              : static gcall *
   12039        19728 : lower_oacc_private_marker (omp_context *ctx)
   12040              : {
   12041        19728 :   if (ctx->oacc_privatization_candidates.length () == 0)
   12042              :     return NULL;
   12043              : 
   12044          308 :   auto_vec<tree, 5> args;
   12045              : 
   12046          308 :   args.quick_push (build_int_cst (integer_type_node, IFN_UNIQUE_OACC_PRIVATE));
   12047          308 :   args.quick_push (integer_zero_node);
   12048          308 :   args.quick_push (integer_minus_one_node);
   12049              : 
   12050          308 :   int i;
   12051          308 :   tree decl;
   12052         1004 :   FOR_EACH_VEC_ELT (ctx->oacc_privatization_candidates, i, decl)
   12053              :     {
   12054          388 :       gcc_checking_assert (TREE_ADDRESSABLE (decl));
   12055          388 :       tree addr = build_fold_addr_expr (decl);
   12056          388 :       args.safe_push (addr);
   12057              :     }
   12058              : 
   12059          308 :   return gimple_build_call_internal_vec (IFN_UNIQUE, args);
   12060          308 : }
   12061              : 
   12062              : /* Lower code for an OMP loop directive.  */
   12063              : 
   12064              : static void
   12065        49458 : lower_omp_for (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   12066              : {
   12067        49458 :   tree *rhs_p, block;
   12068        49458 :   struct omp_for_data fd, *fdp = NULL;
   12069        49458 :   gomp_for *stmt = as_a <gomp_for *> (gsi_stmt (*gsi_p));
   12070        49458 :   gbind *new_stmt;
   12071        49458 :   gimple_seq omp_for_body, body, dlist, tred_ilist = NULL, tred_dlist = NULL;
   12072        49458 :   gimple_seq cnt_list = NULL, clist = NULL;
   12073        49458 :   gimple_seq oacc_head = NULL, oacc_tail = NULL;
   12074        49458 :   size_t i;
   12075              : 
   12076        49458 :   push_gimplify_context ();
   12077              : 
   12078        49458 :   if (is_gimple_omp_oacc (ctx->stmt))
   12079        11325 :     oacc_privatization_scan_clause_chain (ctx, gimple_omp_for_clauses (stmt));
   12080              : 
   12081        49458 :   lower_omp (gimple_omp_for_pre_body_ptr (stmt), ctx);
   12082              : 
   12083        49458 :   block = make_node (BLOCK);
   12084        49458 :   new_stmt = gimple_build_bind (NULL, NULL, block);
   12085              :   /* Replace at gsi right away, so that 'stmt' is no member
   12086              :      of a sequence anymore as we're going to add to a different
   12087              :      one below.  */
   12088        49458 :   gsi_replace (gsi_p, new_stmt, true);
   12089              : 
   12090              :   /* Move declaration of temporaries in the loop body before we make
   12091              :      it go away.  */
   12092        49458 :   omp_for_body = gimple_omp_body (stmt);
   12093        49458 :   if (!gimple_seq_empty_p (omp_for_body)
   12094        49458 :       && gimple_code (gimple_seq_first_stmt (omp_for_body)) == GIMPLE_BIND)
   12095              :     {
   12096        20874 :       gbind *inner_bind
   12097        20874 :         = as_a <gbind *> (gimple_seq_first_stmt (omp_for_body));
   12098        20874 :       tree vars = gimple_bind_vars (inner_bind);
   12099        20874 :       if (is_gimple_omp_oacc (ctx->stmt))
   12100         5305 :         oacc_privatization_scan_decl_chain (ctx, vars);
   12101        20874 :       gimple_bind_append_vars (new_stmt, vars);
   12102              :       /* bind_vars/BLOCK_VARS are being moved to new_stmt/block, don't
   12103              :          keep them on the inner_bind and it's block.  */
   12104        20874 :       gimple_bind_set_vars (inner_bind, NULL_TREE);
   12105        20874 :       if (gimple_bind_block (inner_bind))
   12106        18283 :         BLOCK_VARS (gimple_bind_block (inner_bind)) = NULL_TREE;
   12107              :     }
   12108              : 
   12109        49458 :   if (gimple_omp_for_combined_into_p (stmt))
   12110              :     {
   12111        15977 :       omp_extract_for_data (stmt, &fd, NULL);
   12112        15977 :       fdp = &fd;
   12113              : 
   12114              :       /* We need two temporaries with fd.loop.v type (istart/iend)
   12115              :          and then (fd.collapse - 1) temporaries with the same
   12116              :          type for count2 ... countN-1 vars if not constant.  */
   12117        15977 :       size_t count = 2;
   12118        15977 :       tree type = fd.iter_type;
   12119        15977 :       if (fd.collapse > 1
   12120         6165 :           && TREE_CODE (fd.loop.n2) != INTEGER_CST)
   12121         3082 :         count += fd.collapse - 1;
   12122        15977 :       size_t count2 = 0;
   12123        15977 :       tree type2 = NULL_TREE;
   12124        15977 :       bool taskreg_for
   12125        15977 :         = (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR
   12126        15977 :            || gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_TASKLOOP);
   12127        15977 :       tree outerc = NULL, *pc = gimple_omp_for_clauses_ptr (stmt);
   12128        15977 :       tree simtc = NULL;
   12129        15977 :       tree clauses = *pc;
   12130        15977 :       if (fd.collapse > 1
   12131         6165 :           && fd.non_rect
   12132          137 :           && fd.last_nonrect == fd.first_nonrect + 1
   12133           89 :           && TREE_CODE (fd.loop.n2) != INTEGER_CST)
   12134           64 :         if (tree v = gimple_omp_for_index (stmt, fd.last_nonrect))
   12135           64 :           if (!TYPE_UNSIGNED (TREE_TYPE (v)))
   12136              :             {
   12137           60 :               v = gimple_omp_for_index (stmt, fd.first_nonrect);
   12138           60 :               type2 = TREE_TYPE (v);
   12139           60 :               count++;
   12140           60 :               count2 = 3;
   12141              :             }
   12142        15977 :       if (taskreg_for)
   12143         8255 :         outerc
   12144         8255 :           = omp_find_clause (gimple_omp_taskreg_clauses (ctx->outer->stmt),
   12145              :                              OMP_CLAUSE__LOOPTEMP_);
   12146        15977 :       if (ctx->simt_stmt)
   12147            0 :         simtc = omp_find_clause (gimple_omp_for_clauses (ctx->simt_stmt),
   12148              :                                  OMP_CLAUSE__LOOPTEMP_);
   12149        53774 :       for (i = 0; i < count + count2; i++)
   12150              :         {
   12151        37797 :           tree temp;
   12152        37797 :           if (taskreg_for)
   12153              :             {
   12154        19575 :               gcc_assert (outerc);
   12155        19575 :               temp = lookup_decl (OMP_CLAUSE_DECL (outerc), ctx->outer);
   12156        19575 :               outerc = omp_find_clause (OMP_CLAUSE_CHAIN (outerc),
   12157              :                                         OMP_CLAUSE__LOOPTEMP_);
   12158              :             }
   12159              :           else
   12160              :             {
   12161              :               /* If there are 2 adjacent SIMD stmts, one with _simt_
   12162              :                  clause, another without, make sure they have the same
   12163              :                  decls in _looptemp_ clauses, because the outer stmt
   12164              :                  they are combined into will look up just one inner_stmt.  */
   12165        18222 :               if (ctx->simt_stmt)
   12166            0 :                 temp = OMP_CLAUSE_DECL (simtc);
   12167              :               else
   12168        36300 :                 temp = create_tmp_var (i >= count ? type2 : type);
   12169        18222 :               insert_decl_map (&ctx->outer->cb, temp, temp);
   12170              :             }
   12171        37797 :           *pc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__LOOPTEMP_);
   12172        37797 :           OMP_CLAUSE_DECL (*pc) = temp;
   12173        37797 :           pc = &OMP_CLAUSE_CHAIN (*pc);
   12174        37797 :           if (ctx->simt_stmt)
   12175            0 :             simtc = omp_find_clause (OMP_CLAUSE_CHAIN (simtc),
   12176              :                                      OMP_CLAUSE__LOOPTEMP_);
   12177              :         }
   12178        15977 :       *pc = clauses;
   12179              :     }
   12180              : 
   12181              :   /* The pre-body and input clauses go before the lowered GIMPLE_OMP_FOR.  */
   12182        49458 :   dlist = NULL;
   12183        49458 :   body = NULL;
   12184        49458 :   tree rclauses
   12185        49458 :     = omp_task_reductions_find_first (gimple_omp_for_clauses (stmt), OMP_FOR,
   12186              :                                       OMP_CLAUSE_REDUCTION);
   12187        49458 :   tree rtmp = NULL_TREE;
   12188        49458 :   if (rclauses)
   12189              :     {
   12190          228 :       tree type = build_pointer_type (pointer_sized_int_node);
   12191          228 :       tree temp = create_tmp_var (type);
   12192          228 :       tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
   12193          228 :       OMP_CLAUSE_DECL (c) = temp;
   12194          228 :       OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (stmt);
   12195          228 :       gimple_omp_for_set_clauses (stmt, c);
   12196          228 :       lower_omp_task_reductions (ctx, OMP_FOR,
   12197              :                                  gimple_omp_for_clauses (stmt),
   12198              :                                  &tred_ilist, &tred_dlist);
   12199          228 :       rclauses = c;
   12200          228 :       rtmp = make_ssa_name (type);
   12201          228 :       gimple_seq_add_stmt (&body, gimple_build_assign (rtmp, temp));
   12202              :     }
   12203              : 
   12204        49458 :   lower_lastprivate_conditional_clauses (gimple_omp_for_clauses_ptr (stmt),
   12205              :                                          ctx);
   12206              : 
   12207        49458 :   lower_rec_input_clauses (gimple_omp_for_clauses (stmt), &body, &dlist, ctx,
   12208              :                            fdp);
   12209        98688 :   gimple_seq_add_seq (rclauses ? &tred_ilist : &body,
   12210              :                       gimple_omp_for_pre_body (stmt));
   12211              : 
   12212        49458 :   lower_omp (gimple_omp_body_ptr (stmt), ctx);
   12213              : 
   12214        49458 :   gcall *private_marker = NULL;
   12215        49458 :   if (is_gimple_omp_oacc (ctx->stmt)
   12216        49458 :       && !gimple_seq_empty_p (omp_for_body))
   12217        10333 :     private_marker = lower_oacc_private_marker (ctx);
   12218              : 
   12219              :   /* Lower the header expressions.  At this point, we can assume that
   12220              :      the header is of the form:
   12221              : 
   12222              :         #pragma omp for (V = VAL1; V {<|>|<=|>=} VAL2; V = V [+-] VAL3)
   12223              : 
   12224              :      We just need to make sure that VAL1, VAL2 and VAL3 are lowered
   12225              :      using the .omp_data_s mapping, if needed.  */
   12226       122671 :   for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
   12227              :     {
   12228        73213 :       rhs_p = gimple_omp_for_initial_ptr (stmt, i);
   12229        73213 :       if (TREE_CODE (*rhs_p) == TREE_VEC)
   12230              :         {
   12231          645 :           if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 1)))
   12232          149 :             TREE_VEC_ELT (*rhs_p, 1)
   12233          298 :               = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 1), &cnt_list);
   12234          645 :           if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 2)))
   12235          161 :             TREE_VEC_ELT (*rhs_p, 2)
   12236          322 :               = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 2), &cnt_list);
   12237              :         }
   12238        72568 :       else if (!is_gimple_min_invariant (*rhs_p))
   12239        11533 :         *rhs_p = get_formal_tmp_var (*rhs_p, &cnt_list);
   12240        61035 :       else if (TREE_CODE (*rhs_p) == ADDR_EXPR)
   12241         4713 :         recompute_tree_invariant_for_addr_expr (*rhs_p);
   12242              : 
   12243        73213 :       rhs_p = gimple_omp_for_final_ptr (stmt, i);
   12244        73213 :       if (TREE_CODE (*rhs_p) == TREE_VEC)
   12245              :         {
   12246          519 :           if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 1)))
   12247          142 :             TREE_VEC_ELT (*rhs_p, 1)
   12248          284 :               = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 1), &cnt_list);
   12249          519 :           if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 2)))
   12250          157 :             TREE_VEC_ELT (*rhs_p, 2)
   12251          314 :               = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 2), &cnt_list);
   12252              :         }
   12253        72694 :       else if (!is_gimple_min_invariant (*rhs_p))
   12254        18327 :         *rhs_p = get_formal_tmp_var (*rhs_p, &cnt_list);
   12255        54367 :       else if (TREE_CODE (*rhs_p) == ADDR_EXPR)
   12256         4630 :         recompute_tree_invariant_for_addr_expr (*rhs_p);
   12257              : 
   12258        73213 :       rhs_p = &TREE_OPERAND (gimple_omp_for_incr (stmt, i), 1);
   12259        73213 :       if (!is_gimple_min_invariant (*rhs_p))
   12260         6400 :         *rhs_p = get_formal_tmp_var (*rhs_p, &cnt_list);
   12261              :     }
   12262        49458 :   if (rclauses)
   12263          228 :     gimple_seq_add_seq (&tred_ilist, cnt_list);
   12264              :   else
   12265        49230 :     gimple_seq_add_seq (&body, cnt_list);
   12266              : 
   12267              :   /* Once lowered, extract the bounds and clauses.  */
   12268        49458 :   omp_extract_for_data (stmt, &fd, NULL);
   12269              : 
   12270        49458 :   if (is_gimple_omp_oacc (ctx->stmt)
   12271        49458 :       && !ctx_in_oacc_kernels_region (ctx))
   12272         9665 :     lower_oacc_head_tail (gimple_location (stmt),
   12273              :                           gimple_omp_for_clauses (stmt), private_marker,
   12274              :                           &oacc_head, &oacc_tail, ctx);
   12275              : 
   12276              :   /* Add OpenACC partitioning and reduction markers just before the loop.  */
   12277        49458 :   if (oacc_head)
   12278         9665 :     gimple_seq_add_seq (&body, oacc_head);
   12279              : 
   12280        49458 :   lower_omp_for_lastprivate (&fd, &body, &dlist, &clist, ctx);
   12281              : 
   12282        49458 :   if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR)
   12283        91242 :     for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
   12284        74876 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
   12285        74876 :           && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
   12286              :         {
   12287          227 :           OMP_CLAUSE_DECL (c) = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
   12288          227 :           if (DECL_P (OMP_CLAUSE_LINEAR_STEP (c)))
   12289           42 :             OMP_CLAUSE_LINEAR_STEP (c)
   12290           84 :               = maybe_lookup_decl_in_outer_ctx (OMP_CLAUSE_LINEAR_STEP (c),
   12291              :                                                 ctx);
   12292              :         }
   12293              : 
   12294        49058 :   if ((ctx->scan_inclusive || ctx->scan_exclusive)
   12295        49726 :       && gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR)
   12296          187 :     lower_omp_for_scan (&body, &dlist, stmt, &fd, ctx);
   12297              :   else
   12298              :     {
   12299        49271 :       gimple_seq_add_stmt (&body, stmt);
   12300        49271 :       gimple_seq_add_seq (&body, gimple_omp_body (stmt));
   12301              :     }
   12302              : 
   12303        49458 :   gimple_seq_add_stmt (&body, gimple_build_omp_continue (fd.loop.v,
   12304              :                                                          fd.loop.v));
   12305              : 
   12306              :   /* After the loop, add exit clauses.  */
   12307        49458 :   lower_reduction_clauses (gimple_omp_for_clauses (stmt), &body, &clist, ctx);
   12308              : 
   12309        49458 :   if (clist)
   12310              :     {
   12311          183 :       tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_REDUCTION_START);
   12312          183 :       gcall *g = gimple_build_call (fndecl, 0);
   12313          183 :       gimple_seq_add_stmt (&body, g);
   12314          183 :       gimple_seq_add_seq (&body, clist);
   12315          183 :       fndecl = builtin_decl_explicit (BUILT_IN_GOMP_REDUCTION_END);
   12316          183 :       g = gimple_build_call (fndecl, 0);
   12317          183 :       gimple_seq_add_stmt (&body, g);
   12318              :     }
   12319              : 
   12320        49458 :   if (ctx->cancellable)
   12321           84 :     gimple_seq_add_stmt (&body, gimple_build_label (ctx->cancel_label));
   12322              : 
   12323        49458 :   gimple_seq_add_seq (&body, dlist);
   12324              : 
   12325        49458 :   if (rclauses)
   12326              :     {
   12327          228 :       gimple_seq_add_seq (&tred_ilist, body);
   12328          228 :       body = tred_ilist;
   12329              :     }
   12330              : 
   12331        49458 :   body = maybe_catch_exception (body);
   12332              : 
   12333              :   /* Region exit marker goes at the end of the loop body.  */
   12334        49458 :   gimple *g = gimple_build_omp_return (fd.have_nowait);
   12335        49458 :   gimple_seq_add_stmt (&body, g);
   12336              : 
   12337        49458 :   gimple_seq_add_seq (&body, tred_dlist);
   12338              : 
   12339        49458 :   maybe_add_implicit_barrier_cancel (ctx, g, &body);
   12340              : 
   12341        49458 :   if (rclauses)
   12342          228 :     OMP_CLAUSE_DECL (rclauses) = rtmp;
   12343              : 
   12344              :   /* Add OpenACC joining and reduction markers just after the loop.  */
   12345        49458 :   if (oacc_tail)
   12346         9665 :     gimple_seq_add_seq (&body, oacc_tail);
   12347              : 
   12348        49458 :   pop_gimplify_context (new_stmt);
   12349              : 
   12350        49458 :   gimple_bind_append_vars (new_stmt, ctx->block_vars);
   12351        49458 :   maybe_remove_omp_member_access_dummy_vars (new_stmt);
   12352        49458 :   BLOCK_VARS (block) = gimple_bind_vars (new_stmt);
   12353        49458 :   if (BLOCK_VARS (block))
   12354        48808 :     TREE_USED (block) = 1;
   12355              : 
   12356        49458 :   gimple_bind_set_body (new_stmt, body);
   12357        49458 :   gimple_omp_set_body (stmt, NULL);
   12358        49458 :   gimple_omp_for_set_pre_body (stmt, NULL);
   12359        49458 : }
   12360              : 
   12361              : /* Callback for walk_stmts.  Check if the current statement only contains
   12362              :    GIMPLE_OMP_FOR or GIMPLE_OMP_SECTIONS.  */
   12363              : 
   12364              : static tree
   12365       217746 : check_combined_parallel (gimple_stmt_iterator *gsi_p,
   12366              :                          bool *handled_ops_p,
   12367              :                          struct walk_stmt_info *wi)
   12368              : {
   12369       217746 :   int *info = (int *) wi->info;
   12370       217746 :   gimple *stmt = gsi_stmt (*gsi_p);
   12371              : 
   12372       217746 :   *handled_ops_p = true;
   12373       217746 :   switch (gimple_code (stmt))
   12374              :     {
   12375        12041 :     WALK_SUBSTMTS;
   12376              : 
   12377              :     case GIMPLE_DEBUG:
   12378              :       break;
   12379         1918 :     case GIMPLE_OMP_FOR:
   12380         1918 :     case GIMPLE_OMP_SECTIONS:
   12381         1918 :       *info = *info == 0 ? 1 : -1;
   12382         1918 :       break;
   12383       203748 :     default:
   12384       203748 :       *info = -1;
   12385       203748 :       break;
   12386              :     }
   12387       217746 :   return NULL;
   12388              : }
   12389              : 
   12390              : struct omp_taskcopy_context
   12391              : {
   12392              :   /* This field must be at the beginning, as we do "inheritance": Some
   12393              :      callback functions for tree-inline.cc (e.g., omp_copy_decl)
   12394              :      receive a copy_body_data pointer that is up-casted to an
   12395              :      omp_context pointer.  */
   12396              :   copy_body_data cb;
   12397              :   omp_context *ctx;
   12398              : };
   12399              : 
   12400              : static tree
   12401          366 : task_copyfn_copy_decl (tree var, copy_body_data *cb)
   12402              : {
   12403          366 :   struct omp_taskcopy_context *tcctx = (struct omp_taskcopy_context *) cb;
   12404              : 
   12405          366 :   if (splay_tree_lookup (tcctx->ctx->sfield_map, (splay_tree_key) var))
   12406          158 :     return create_tmp_var (TREE_TYPE (var));
   12407              : 
   12408              :   return var;
   12409              : }
   12410              : 
   12411              : static tree
   12412           50 : task_copyfn_remap_type (struct omp_taskcopy_context *tcctx, tree orig_type)
   12413              : {
   12414           50 :   tree name, new_fields = NULL, type, f;
   12415              : 
   12416           50 :   type = lang_hooks.types.make_type (RECORD_TYPE);
   12417           50 :   name = DECL_NAME (TYPE_NAME (orig_type));
   12418           50 :   name = build_decl (gimple_location (tcctx->ctx->stmt),
   12419              :                      TYPE_DECL, name, type);
   12420           50 :   TYPE_NAME (type) = name;
   12421              : 
   12422         1140 :   for (f = TYPE_FIELDS (orig_type); f ; f = TREE_CHAIN (f))
   12423              :     {
   12424         1090 :       tree new_f = copy_node (f);
   12425         1090 :       DECL_CONTEXT (new_f) = type;
   12426         1090 :       TREE_TYPE (new_f) = remap_type (TREE_TYPE (f), &tcctx->cb);
   12427         1090 :       TREE_CHAIN (new_f) = new_fields;
   12428         1090 :       walk_tree (&DECL_SIZE (new_f), copy_tree_body_r, &tcctx->cb, NULL);
   12429         1090 :       walk_tree (&DECL_SIZE_UNIT (new_f), copy_tree_body_r, &tcctx->cb, NULL);
   12430         1090 :       walk_tree (&DECL_FIELD_OFFSET (new_f), copy_tree_body_r,
   12431              :                  &tcctx->cb, NULL);
   12432         1090 :       new_fields = new_f;
   12433         1090 :       tcctx->cb.decl_map->put (f, new_f);
   12434              :     }
   12435           50 :   TYPE_FIELDS (type) = nreverse (new_fields);
   12436           50 :   layout_type (type);
   12437           50 :   return type;
   12438              : }
   12439              : 
   12440              : /* Create task copyfn.  */
   12441              : 
   12442              : static void
   12443          514 : create_task_copyfn (gomp_task *task_stmt, omp_context *ctx)
   12444              : {
   12445          514 :   struct function *child_cfun;
   12446          514 :   tree child_fn, t, c, src, dst, f, sf, arg, sarg, decl;
   12447          514 :   tree record_type, srecord_type, bind, list;
   12448          514 :   bool record_needs_remap = false, srecord_needs_remap = false;
   12449          514 :   splay_tree_node n;
   12450          514 :   struct omp_taskcopy_context tcctx;
   12451          514 :   location_t loc = gimple_location (task_stmt);
   12452          514 :   size_t looptempno = 0;
   12453              : 
   12454          514 :   child_fn = gimple_omp_task_copy_fn (task_stmt);
   12455          514 :   task_cpyfns.safe_push (task_stmt);
   12456          514 :   child_cfun = DECL_STRUCT_FUNCTION (child_fn);
   12457          514 :   gcc_assert (child_cfun->cfg == NULL);
   12458          514 :   DECL_SAVED_TREE (child_fn) = alloc_stmt_list ();
   12459              : 
   12460              :   /* Reset DECL_CONTEXT on function arguments.  */
   12461         1542 :   for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
   12462         1028 :     DECL_CONTEXT (t) = child_fn;
   12463              : 
   12464              :   /* Populate the function.  */
   12465          514 :   push_gimplify_context ();
   12466          514 :   push_cfun (child_cfun);
   12467              : 
   12468          514 :   bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
   12469          514 :   TREE_SIDE_EFFECTS (bind) = 1;
   12470          514 :   list = NULL;
   12471          514 :   DECL_SAVED_TREE (child_fn) = bind;
   12472          514 :   DECL_SOURCE_LOCATION (child_fn) = gimple_location (task_stmt);
   12473              : 
   12474              :   /* Remap src and dst argument types if needed.  */
   12475          514 :   record_type = ctx->record_type;
   12476          514 :   srecord_type = ctx->srecord_type;
   12477         3137 :   for (f = TYPE_FIELDS (record_type); f ; f = DECL_CHAIN (f))
   12478         2648 :     if (variably_modified_type_p (TREE_TYPE (f), ctx->cb.src_fn))
   12479              :       {
   12480              :         record_needs_remap = true;
   12481              :         break;
   12482              :       }
   12483         2876 :   for (f = TYPE_FIELDS (srecord_type); f ; f = DECL_CHAIN (f))
   12484         2387 :     if (variably_modified_type_p (TREE_TYPE (f), ctx->cb.src_fn))
   12485              :       {
   12486              :         srecord_needs_remap = true;
   12487              :         break;
   12488              :       }
   12489              : 
   12490          514 :   if (record_needs_remap || srecord_needs_remap)
   12491              :     {
   12492           25 :       memset (&tcctx, '\0', sizeof (tcctx));
   12493           25 :       tcctx.cb.src_fn = ctx->cb.src_fn;
   12494           25 :       tcctx.cb.dst_fn = child_fn;
   12495           25 :       tcctx.cb.src_node = cgraph_node::get (tcctx.cb.src_fn);
   12496           25 :       gcc_checking_assert (tcctx.cb.src_node);
   12497           25 :       tcctx.cb.dst_node = tcctx.cb.src_node;
   12498           25 :       tcctx.cb.src_cfun = ctx->cb.src_cfun;
   12499           25 :       tcctx.cb.copy_decl = task_copyfn_copy_decl;
   12500           25 :       tcctx.cb.eh_lp_nr = 0;
   12501           25 :       tcctx.cb.transform_call_graph_edges = CB_CGE_MOVE;
   12502           25 :       tcctx.cb.decl_map = new hash_map<tree, tree>;
   12503           25 :       tcctx.ctx = ctx;
   12504              : 
   12505           25 :       if (record_needs_remap)
   12506           25 :         record_type = task_copyfn_remap_type (&tcctx, record_type);
   12507           25 :       if (srecord_needs_remap)
   12508           25 :         srecord_type = task_copyfn_remap_type (&tcctx, srecord_type);
   12509              :     }
   12510              :   else
   12511          489 :     tcctx.cb.decl_map = NULL;
   12512              : 
   12513          514 :   arg = DECL_ARGUMENTS (child_fn);
   12514          514 :   TREE_TYPE (arg) = build_pointer_type (record_type);
   12515          514 :   sarg = DECL_CHAIN (arg);
   12516          514 :   TREE_TYPE (sarg) = build_pointer_type (srecord_type);
   12517              : 
   12518              :   /* First pass: initialize temporaries used in record_type and srecord_type
   12519              :      sizes and field offsets.  */
   12520          514 :   if (tcctx.cb.decl_map)
   12521          631 :     for (c = gimple_omp_task_clauses (task_stmt); c; c = OMP_CLAUSE_CHAIN (c))
   12522          606 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
   12523              :         {
   12524          497 :           tree *p;
   12525              : 
   12526          497 :           decl = OMP_CLAUSE_DECL (c);
   12527          497 :           p = tcctx.cb.decl_map->get (decl);
   12528          497 :           if (p == NULL)
   12529          339 :             continue;
   12530          158 :           n = splay_tree_lookup (ctx->sfield_map, (splay_tree_key) decl);
   12531          158 :           sf = (tree) n->value;
   12532          158 :           sf = *tcctx.cb.decl_map->get (sf);
   12533          158 :           src = build_simple_mem_ref_loc (loc, sarg);
   12534          158 :           src = omp_build_component_ref (src, sf);
   12535          158 :           t = build2 (MODIFY_EXPR, TREE_TYPE (*p), *p, src);
   12536          158 :           append_to_statement_list (t, &list);
   12537              :         }
   12538              : 
   12539              :   /* Second pass: copy shared var pointers and copy construct non-VLA
   12540              :      firstprivate vars.  */
   12541         5902 :   for (c = gimple_omp_task_clauses (task_stmt); c; c = OMP_CLAUSE_CHAIN (c))
   12542         5388 :     switch (OMP_CLAUSE_CODE (c))
   12543              :       {
   12544          583 :         splay_tree_key key;
   12545          583 :       case OMP_CLAUSE_SHARED:
   12546          583 :         decl = OMP_CLAUSE_DECL (c);
   12547          583 :         key = (splay_tree_key) decl;
   12548          583 :         if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
   12549          273 :           key = (splay_tree_key) &DECL_UID (decl);
   12550          583 :         n = splay_tree_lookup (ctx->field_map, key);
   12551          583 :         if (n == NULL)
   12552              :           break;
   12553          149 :         f = (tree) n->value;
   12554          149 :         if (tcctx.cb.decl_map)
   12555           12 :           f = *tcctx.cb.decl_map->get (f);
   12556          149 :         n = splay_tree_lookup (ctx->sfield_map, key);
   12557          149 :         sf = (tree) n->value;
   12558          149 :         if (tcctx.cb.decl_map)
   12559           12 :           sf = *tcctx.cb.decl_map->get (sf);
   12560          149 :         src = build_simple_mem_ref_loc (loc, sarg);
   12561          149 :         src = omp_build_component_ref (src, sf);
   12562          149 :         dst = build_simple_mem_ref_loc (loc, arg);
   12563          149 :         dst = omp_build_component_ref (dst, f);
   12564          149 :         t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
   12565          149 :         append_to_statement_list (t, &list);
   12566          149 :         break;
   12567          460 :       case OMP_CLAUSE_REDUCTION:
   12568          460 :       case OMP_CLAUSE_IN_REDUCTION:
   12569          460 :         decl = OMP_CLAUSE_DECL (c);
   12570          460 :         if (TREE_CODE (decl) == MEM_REF)
   12571              :           {
   12572          160 :             decl = TREE_OPERAND (decl, 0);
   12573          160 :             if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
   12574           38 :               decl = TREE_OPERAND (decl, 0);
   12575          160 :             if (TREE_CODE (decl) == INDIRECT_REF
   12576          128 :                 || TREE_CODE (decl) == ADDR_EXPR)
   12577           95 :               decl = TREE_OPERAND (decl, 0);
   12578              :           }
   12579          460 :         key = (splay_tree_key) decl;
   12580          460 :         n = splay_tree_lookup (ctx->field_map, key);
   12581          460 :         if (n == NULL)
   12582              :           break;
   12583          101 :         f = (tree) n->value;
   12584          101 :         if (tcctx.cb.decl_map)
   12585           20 :           f = *tcctx.cb.decl_map->get (f);
   12586          101 :         n = splay_tree_lookup (ctx->sfield_map, key);
   12587          101 :         sf = (tree) n->value;
   12588          101 :         if (tcctx.cb.decl_map)
   12589           20 :           sf = *tcctx.cb.decl_map->get (sf);
   12590          101 :         src = build_simple_mem_ref_loc (loc, sarg);
   12591          101 :         src = omp_build_component_ref (src, sf);
   12592          101 :         if (decl != OMP_CLAUSE_DECL (c)
   12593           95 :             && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
   12594          173 :             && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
   12595           32 :           src = build_simple_mem_ref_loc (loc, src);
   12596          101 :         dst = build_simple_mem_ref_loc (loc, arg);
   12597          101 :         dst = omp_build_component_ref (dst, f);
   12598          101 :         t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
   12599          101 :         append_to_statement_list (t, &list);
   12600          101 :         break;
   12601          768 :       case OMP_CLAUSE__LOOPTEMP_:
   12602              :         /* Fields for first two _looptemp_ clauses are initialized by
   12603              :            GOMP_taskloop*, the rest are handled like firstprivate.  */
   12604          768 :         if (looptempno < 2)
   12605              :           {
   12606          762 :             looptempno++;
   12607          762 :             break;
   12608              :           }
   12609              :         /* FALLTHRU */
   12610         1742 :       case OMP_CLAUSE__REDUCTEMP_:
   12611         1742 :       case OMP_CLAUSE_FIRSTPRIVATE:
   12612         1742 :         decl = OMP_CLAUSE_DECL (c);
   12613         1742 :         if (is_variable_sized (decl))
   12614              :           break;
   12615         1728 :         n = splay_tree_lookup (ctx->field_map, (splay_tree_key) decl);
   12616         1728 :         if (n == NULL)
   12617              :           break;
   12618         1728 :         f = (tree) n->value;
   12619         1728 :         if (tcctx.cb.decl_map)
   12620          490 :           f = *tcctx.cb.decl_map->get (f);
   12621         1728 :         n = splay_tree_lookup (ctx->sfield_map, (splay_tree_key) decl);
   12622         1728 :         if (n != NULL)
   12623              :           {
   12624         1478 :             sf = (tree) n->value;
   12625         1478 :             if (tcctx.cb.decl_map)
   12626          490 :               sf = *tcctx.cb.decl_map->get (sf);
   12627         1478 :             src = build_simple_mem_ref_loc (loc, sarg);
   12628         1478 :             src = omp_build_component_ref (src, sf);
   12629         1478 :             if (use_pointer_for_field (decl, NULL)
   12630         1478 :                 || omp_privatize_by_reference (decl))
   12631          327 :               src = build_simple_mem_ref_loc (loc, src);
   12632              :           }
   12633              :         else
   12634              :           src = decl;
   12635         1728 :         dst = build_simple_mem_ref_loc (loc, arg);
   12636         1728 :         dst = omp_build_component_ref (dst, f);
   12637         1728 :         if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
   12638          192 :           t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
   12639              :         else
   12640              :           {
   12641         1536 :             if (ctx->allocate_map)
   12642           93 :               if (tree *allocatorp = ctx->allocate_map->get (decl))
   12643              :                 {
   12644           59 :                   tree allocator = *allocatorp;
   12645           59 :                   HOST_WIDE_INT ialign = 0;
   12646           59 :                   if (TREE_CODE (allocator) == TREE_LIST)
   12647              :                     {
   12648            4 :                       ialign = tree_to_uhwi (TREE_VALUE (allocator));
   12649            4 :                       allocator = TREE_PURPOSE (allocator);
   12650              :                     }
   12651           59 :                   if (TREE_CODE (allocator) != INTEGER_CST)
   12652              :                     {
   12653           22 :                       n = splay_tree_lookup (ctx->sfield_map,
   12654              :                                              (splay_tree_key) allocator);
   12655           22 :                       allocator = (tree) n->value;
   12656           22 :                       if (tcctx.cb.decl_map)
   12657            0 :                         allocator = *tcctx.cb.decl_map->get (allocator);
   12658           22 :                       tree a = build_simple_mem_ref_loc (loc, sarg);
   12659           22 :                       allocator = omp_build_component_ref (a, allocator);
   12660              :                     }
   12661           59 :                   allocator = fold_convert (pointer_sized_int_node, allocator);
   12662           59 :                   tree a = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
   12663           59 :                   tree align = build_int_cst (size_type_node,
   12664           59 :                                               MAX (ialign,
   12665              :                                                    DECL_ALIGN_UNIT (decl)));
   12666           59 :                   tree sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (dst)));
   12667           59 :                   tree ptr = build_call_expr_loc (loc, a, 3, align, sz,
   12668              :                                                   allocator);
   12669           59 :                   ptr = fold_convert (TREE_TYPE (dst), ptr);
   12670           59 :                   t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, ptr);
   12671           59 :                   append_to_statement_list (t, &list);
   12672           59 :                   dst = build_simple_mem_ref_loc (loc, dst);
   12673              :                 }
   12674         1536 :             t = lang_hooks.decls.omp_clause_copy_ctor (c, dst, src);
   12675              :           }
   12676         1728 :         append_to_statement_list (t, &list);
   12677         1728 :         break;
   12678          497 :       case OMP_CLAUSE_PRIVATE:
   12679          497 :         if (! OMP_CLAUSE_PRIVATE_OUTER_REF (c))
   12680              :           break;
   12681           12 :         decl = OMP_CLAUSE_DECL (c);
   12682           12 :         n = splay_tree_lookup (ctx->field_map, (splay_tree_key) decl);
   12683           12 :         f = (tree) n->value;
   12684           12 :         if (tcctx.cb.decl_map)
   12685            0 :           f = *tcctx.cb.decl_map->get (f);
   12686           12 :         n = splay_tree_lookup (ctx->sfield_map, (splay_tree_key) decl);
   12687           12 :         if (n != NULL)
   12688              :           {
   12689           12 :             sf = (tree) n->value;
   12690           12 :             if (tcctx.cb.decl_map)
   12691            0 :               sf = *tcctx.cb.decl_map->get (sf);
   12692           12 :             src = build_simple_mem_ref_loc (loc, sarg);
   12693           12 :             src = omp_build_component_ref (src, sf);
   12694           12 :             if (use_pointer_for_field (decl, NULL))
   12695           12 :               src = build_simple_mem_ref_loc (loc, src);
   12696              :           }
   12697              :         else
   12698              :           src = decl;
   12699           12 :         dst = build_simple_mem_ref_loc (loc, arg);
   12700           12 :         dst = omp_build_component_ref (dst, f);
   12701           12 :         t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
   12702           12 :         append_to_statement_list (t, &list);
   12703           12 :         break;
   12704              :       default:
   12705              :         break;
   12706              :       }
   12707              : 
   12708              :   /* Last pass: handle VLA firstprivates.  */
   12709          514 :   if (tcctx.cb.decl_map)
   12710          631 :     for (c = gimple_omp_task_clauses (task_stmt); c; c = OMP_CLAUSE_CHAIN (c))
   12711          606 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
   12712              :         {
   12713          497 :           tree ind, ptr, df;
   12714              : 
   12715          497 :           decl = OMP_CLAUSE_DECL (c);
   12716          497 :           if (!is_variable_sized (decl))
   12717          483 :             continue;
   12718           14 :           n = splay_tree_lookup (ctx->field_map, (splay_tree_key) decl);
   12719           14 :           if (n == NULL)
   12720            0 :             continue;
   12721           14 :           f = (tree) n->value;
   12722           14 :           f = *tcctx.cb.decl_map->get (f);
   12723           14 :           gcc_assert (DECL_HAS_VALUE_EXPR_P (decl));
   12724           14 :           ind = DECL_VALUE_EXPR (decl);
   12725           14 :           gcc_assert (TREE_CODE (ind) == INDIRECT_REF);
   12726           14 :           gcc_assert (DECL_P (TREE_OPERAND (ind, 0)));
   12727           42 :           n = splay_tree_lookup (ctx->sfield_map,
   12728           14 :                                  (splay_tree_key) TREE_OPERAND (ind, 0));
   12729           14 :           sf = (tree) n->value;
   12730           14 :           sf = *tcctx.cb.decl_map->get (sf);
   12731           14 :           src = build_simple_mem_ref_loc (loc, sarg);
   12732           14 :           src = omp_build_component_ref (src, sf);
   12733           14 :           src = build_simple_mem_ref_loc (loc, src);
   12734           14 :           dst = build_simple_mem_ref_loc (loc, arg);
   12735           14 :           dst = omp_build_component_ref (dst, f);
   12736           14 :           t = lang_hooks.decls.omp_clause_copy_ctor (c, dst, src);
   12737           14 :           append_to_statement_list (t, &list);
   12738           42 :           n = splay_tree_lookup (ctx->field_map,
   12739           14 :                                  (splay_tree_key) TREE_OPERAND (ind, 0));
   12740           14 :           df = (tree) n->value;
   12741           14 :           df = *tcctx.cb.decl_map->get (df);
   12742           14 :           ptr = build_simple_mem_ref_loc (loc, arg);
   12743           14 :           ptr = omp_build_component_ref (ptr, df);
   12744           14 :           t = build2 (MODIFY_EXPR, TREE_TYPE (ptr), ptr,
   12745              :                       build_fold_addr_expr_loc (loc, dst));
   12746           14 :           append_to_statement_list (t, &list);
   12747              :         }
   12748              : 
   12749          514 :   t = build1 (RETURN_EXPR, void_type_node, NULL);
   12750          514 :   append_to_statement_list (t, &list);
   12751              : 
   12752          514 :   if (tcctx.cb.decl_map)
   12753           25 :     delete tcctx.cb.decl_map;
   12754          514 :   pop_gimplify_context (NULL);
   12755          514 :   BIND_EXPR_BODY (bind) = list;
   12756          514 :   pop_cfun ();
   12757          514 : }
   12758              : 
   12759              : static void
   12760         1553 : lower_depend_clauses (tree *pclauses, gimple_seq *iseq, gimple_seq *oseq)
   12761              : {
   12762         1553 :   tree c, clauses;
   12763         1553 :   gimple *g;
   12764         1553 :   size_t cnt[5] = { 0, 0, 0, 0, 0 }, idx = 2, i;
   12765              : 
   12766         1553 :   clauses = omp_find_clause (*pclauses, OMP_CLAUSE_DEPEND);
   12767         1553 :   gcc_assert (clauses);
   12768         6316 :   for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
   12769         4888 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
   12770         1713 :       switch (OMP_CLAUSE_DEPEND_KIND (c))
   12771              :         {
   12772          125 :         case OMP_CLAUSE_DEPEND_LAST:
   12773              :           /* Lowering already done at gimplification.  */
   12774          125 :           return;
   12775          481 :         case OMP_CLAUSE_DEPEND_IN:
   12776          481 :           cnt[2]++;
   12777          481 :           break;
   12778          969 :         case OMP_CLAUSE_DEPEND_OUT:
   12779          969 :         case OMP_CLAUSE_DEPEND_INOUT:
   12780          969 :           cnt[0]++;
   12781          969 :           break;
   12782           30 :         case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
   12783           30 :           cnt[1]++;
   12784           30 :           break;
   12785           76 :         case OMP_CLAUSE_DEPEND_DEPOBJ:
   12786           76 :           cnt[3]++;
   12787           76 :           break;
   12788           32 :         case OMP_CLAUSE_DEPEND_INOUTSET:
   12789           32 :           cnt[4]++;
   12790           32 :           break;
   12791            0 :         default:
   12792            0 :           gcc_unreachable ();
   12793              :         }
   12794         1428 :   if (cnt[1] || cnt[3] || cnt[4])
   12795          135 :     idx = 5;
   12796         1428 :   size_t total = cnt[0] + cnt[1] + cnt[2] + cnt[3] + cnt[4];
   12797         1428 :   size_t inoutidx = total + idx;
   12798         1428 :   tree type = build_array_type_nelts (ptr_type_node, total + idx + 2 * cnt[4]);
   12799         1428 :   tree array = create_tmp_var (type);
   12800         1428 :   TREE_ADDRESSABLE (array) = 1;
   12801         1428 :   tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
   12802              :                    NULL_TREE);
   12803         1428 :   if (idx == 5)
   12804              :     {
   12805          135 :       g = gimple_build_assign (r, build_int_cst (ptr_type_node, 0));
   12806          135 :       gimple_seq_add_stmt (iseq, g);
   12807          135 :       r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
   12808              :                   NULL_TREE);
   12809              :     }
   12810         1428 :   g = gimple_build_assign (r, build_int_cst (ptr_type_node, total));
   12811         1428 :   gimple_seq_add_stmt (iseq, g);
   12812         7140 :   for (i = 0; i < (idx == 5 ? 3 : 1); i++)
   12813              :     {
   12814         1698 :       r = build4 (ARRAY_REF, ptr_type_node, array,
   12815         1698 :                   size_int (i + 1 + (idx == 5)), NULL_TREE, NULL_TREE);
   12816         1698 :       g = gimple_build_assign (r, build_int_cst (ptr_type_node, cnt[i]));
   12817         1698 :       gimple_seq_add_stmt (iseq, g);
   12818              :     }
   12819         8568 :   for (i = 0; i < 5; i++)
   12820              :     {
   12821         7140 :       if (cnt[i] == 0)
   12822         5656 :         continue;
   12823         6565 :       for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
   12824         5081 :         if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
   12825         3354 :           continue;
   12826              :         else
   12827              :           {
   12828         1727 :             switch (OMP_CLAUSE_DEPEND_KIND (c))
   12829              :               {
   12830          547 :               case OMP_CLAUSE_DEPEND_IN:
   12831          547 :                 if (i != 2)
   12832          139 :                   continue;
   12833              :                 break;
   12834         1025 :               case OMP_CLAUSE_DEPEND_OUT:
   12835         1025 :               case OMP_CLAUSE_DEPEND_INOUT:
   12836         1025 :                 if (i != 0)
   12837           56 :                   continue;
   12838              :                 break;
   12839           44 :               case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
   12840           44 :                 if (i != 1)
   12841           14 :                   continue;
   12842              :                 break;
   12843           79 :               case OMP_CLAUSE_DEPEND_DEPOBJ:
   12844           79 :                 if (i != 3)
   12845            3 :                   continue;
   12846              :                 break;
   12847           32 :               case OMP_CLAUSE_DEPEND_INOUTSET:
   12848           32 :                 if (i != 4)
   12849            0 :                    continue;
   12850              :                 break;
   12851            0 :               default:
   12852            0 :                 gcc_unreachable ();
   12853              :               }
   12854         1588 :             tree t = OMP_CLAUSE_DECL (c);
   12855         1588 :             if (i == 4)
   12856              :               {
   12857           32 :                 t = build4 (ARRAY_REF, ptr_type_node, array,
   12858           32 :                             size_int (inoutidx), NULL_TREE, NULL_TREE);
   12859           32 :                 t = build_fold_addr_expr (t);
   12860           32 :                 inoutidx += 2;
   12861              :               }
   12862         1588 :             t = fold_convert (ptr_type_node, t);
   12863         1588 :             gimplify_expr (&t, iseq, NULL, is_gimple_val, fb_rvalue);
   12864         1588 :             r = build4 (ARRAY_REF, ptr_type_node, array, size_int (idx++),
   12865              :                         NULL_TREE, NULL_TREE);
   12866         1588 :             g = gimple_build_assign (r, t);
   12867         1588 :             gimple_seq_add_stmt (iseq, g);
   12868              :           }
   12869              :     }
   12870         1428 :   if (cnt[4])
   12871           81 :     for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
   12872           49 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
   12873           49 :           && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_INOUTSET)
   12874              :         {
   12875           32 :           tree t = OMP_CLAUSE_DECL (c);
   12876           32 :           t = fold_convert (ptr_type_node, t);
   12877           32 :           gimplify_expr (&t, iseq, NULL, is_gimple_val, fb_rvalue);
   12878           32 :           r = build4 (ARRAY_REF, ptr_type_node, array, size_int (idx++),
   12879              :                       NULL_TREE, NULL_TREE);
   12880           32 :           g = gimple_build_assign (r, t);
   12881           32 :           gimple_seq_add_stmt (iseq, g);
   12882           32 :           t = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
   12883           32 :           r = build4 (ARRAY_REF, ptr_type_node, array, size_int (idx++),
   12884              :                       NULL_TREE, NULL_TREE);
   12885           32 :           g = gimple_build_assign (r, t);
   12886           32 :           gimple_seq_add_stmt (iseq, g);
   12887              :         }
   12888              : 
   12889         1428 :   c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
   12890         1428 :   OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
   12891         1428 :   OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
   12892         1428 :   OMP_CLAUSE_CHAIN (c) = *pclauses;
   12893         1428 :   *pclauses = c;
   12894         1428 :   tree clobber = build_clobber (type);
   12895         1428 :   g = gimple_build_assign (array, clobber);
   12896         1428 :   gimple_seq_add_stmt (oseq, g);
   12897              : }
   12898              : 
   12899              : /* Lower the OpenMP parallel or task directive in the current statement
   12900              :    in GSI_P.  CTX holds context information for the directive.  */
   12901              : 
   12902              : static void
   12903        23524 : lower_omp_taskreg (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   12904              : {
   12905        23524 :   tree clauses;
   12906        23524 :   tree child_fn, t;
   12907        23524 :   gimple *stmt = gsi_stmt (*gsi_p);
   12908        23524 :   gbind *par_bind, *bind, *dep_bind = NULL;
   12909        23524 :   gimple_seq par_body;
   12910        23524 :   location_t loc = gimple_location (stmt);
   12911              : 
   12912        23524 :   clauses = gimple_omp_taskreg_clauses (stmt);
   12913        23524 :   if (gimple_code (stmt) == GIMPLE_OMP_TASK
   12914        23524 :       && gimple_omp_task_taskwait_p (stmt))
   12915              :     {
   12916           84 :       par_bind = NULL;
   12917           84 :       par_body = NULL;
   12918              :     }
   12919              :   else
   12920              :     {
   12921        23440 :       par_bind
   12922        23440 :         = as_a <gbind *> (gimple_seq_first_stmt (gimple_omp_body (stmt)));
   12923        23440 :       par_body = gimple_bind_body (par_bind);
   12924              :     }
   12925        23524 :   child_fn = ctx->cb.dst_fn;
   12926        23524 :   if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
   12927        23524 :       && !gimple_omp_parallel_combined_p (stmt))
   12928              :     {
   12929         4370 :       struct walk_stmt_info wi;
   12930         4370 :       int ws_num = 0;
   12931              : 
   12932         4370 :       memset (&wi, 0, sizeof (wi));
   12933         4370 :       wi.info = &ws_num;
   12934         4370 :       wi.val_only = true;
   12935         4370 :       walk_gimple_seq (par_body, check_combined_parallel, NULL, &wi);
   12936         4370 :       if (ws_num == 1)
   12937          385 :         gimple_omp_parallel_set_combined_p (stmt, true);
   12938              :     }
   12939        23524 :   gimple_seq dep_ilist = NULL;
   12940        23524 :   gimple_seq dep_olist = NULL;
   12941        23524 :   if (gimple_code (stmt) == GIMPLE_OMP_TASK
   12942        23524 :       && omp_find_clause (clauses, OMP_CLAUSE_DEPEND))
   12943              :     {
   12944         1189 :       push_gimplify_context ();
   12945         1189 :       dep_bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
   12946         1189 :       lower_depend_clauses (gimple_omp_task_clauses_ptr (stmt),
   12947              :                             &dep_ilist, &dep_olist);
   12948              :     }
   12949              : 
   12950        23524 :   if (gimple_code (stmt) == GIMPLE_OMP_TASK
   12951        23524 :       && gimple_omp_task_taskwait_p (stmt))
   12952              :     {
   12953           84 :       if (dep_bind)
   12954              :         {
   12955           84 :           gsi_replace (gsi_p, dep_bind, true);
   12956           84 :           gimple_bind_add_seq (dep_bind, dep_ilist);
   12957           84 :           gimple_bind_add_stmt (dep_bind, stmt);
   12958           84 :           gimple_bind_add_seq (dep_bind, dep_olist);
   12959           84 :           pop_gimplify_context (dep_bind);
   12960              :         }
   12961           84 :       return;
   12962              :     }
   12963              : 
   12964        23440 :   if (ctx->srecord_type)
   12965          514 :     create_task_copyfn (as_a <gomp_task *> (stmt), ctx);
   12966              : 
   12967        23440 :   gimple_seq tskred_ilist = NULL;
   12968        23440 :   gimple_seq tskred_olist = NULL;
   12969        23440 :   if ((is_task_ctx (ctx)
   12970         3844 :        && gimple_omp_task_taskloop_p (ctx->stmt)
   12971         1337 :        && omp_find_clause (gimple_omp_task_clauses (ctx->stmt),
   12972              :                            OMP_CLAUSE_REDUCTION))
   12973        26787 :       || (is_parallel_ctx (ctx)
   12974        17018 :           && omp_find_clause (gimple_omp_parallel_clauses (stmt),
   12975              :                               OMP_CLAUSE__REDUCTEMP_)))
   12976              :     {
   12977          559 :       if (dep_bind == NULL)
   12978              :         {
   12979          559 :           push_gimplify_context ();
   12980          559 :           dep_bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
   12981              :         }
   12982          621 :       lower_omp_task_reductions (ctx, is_task_ctx (ctx) ? OMP_TASKLOOP
   12983              :                                                         : OMP_PARALLEL,
   12984          559 :                                  gimple_omp_taskreg_clauses (ctx->stmt),
   12985              :                                  &tskred_ilist, &tskred_olist);
   12986              :     }
   12987              : 
   12988        23440 :   push_gimplify_context ();
   12989              : 
   12990        23440 :   gimple_seq par_olist = NULL;
   12991        23440 :   gimple_seq par_ilist = NULL;
   12992        23440 :   gimple_seq par_rlist = NULL;
   12993        23440 :   lower_rec_input_clauses (clauses, &par_ilist, &par_olist, ctx, NULL);
   12994        23436 :   lower_omp (&par_body, ctx);
   12995        23436 :   if (gimple_code (stmt) != GIMPLE_OMP_TASK)
   12996        19592 :     lower_reduction_clauses (clauses, &par_rlist, NULL, ctx);
   12997              : 
   12998              :   /* Declare all the variables created by mapping and the variables
   12999              :      declared in the scope of the parallel body.  */
   13000        23436 :   record_vars_into (ctx->block_vars, child_fn);
   13001        23436 :   maybe_remove_omp_member_access_dummy_vars (par_bind);
   13002        23436 :   record_vars_into (gimple_bind_vars (par_bind), child_fn);
   13003              : 
   13004        23436 :   if (ctx->record_type)
   13005              :     {
   13006        18896 :       ctx->sender_decl
   13007        37278 :         = create_tmp_var (ctx->srecord_type ? ctx->srecord_type
   13008              :                           : ctx->record_type, ".omp_data_o");
   13009        18896 :       DECL_NAMELESS (ctx->sender_decl) = 1;
   13010        18896 :       TREE_ADDRESSABLE (ctx->sender_decl) = 1;
   13011        18896 :       gimple_omp_taskreg_set_data_arg (stmt, ctx->sender_decl);
   13012              :     }
   13013              : 
   13014        23436 :   gimple_seq olist = NULL;
   13015        23436 :   gimple_seq ilist = NULL;
   13016        23436 :   lower_send_clauses (clauses, &ilist, &olist, ctx);
   13017        23436 :   lower_send_shared_vars (&ilist, &olist, ctx);
   13018              : 
   13019        23436 :   if (ctx->record_type)
   13020              :     {
   13021        18896 :       tree clobber = build_clobber (TREE_TYPE (ctx->sender_decl));
   13022        18896 :       gimple_seq_add_stmt (&olist, gimple_build_assign (ctx->sender_decl,
   13023              :                                                         clobber));
   13024              :     }
   13025              : 
   13026              :   /* Once all the expansions are done, sequence all the different
   13027              :      fragments inside gimple_omp_body.  */
   13028              : 
   13029        23436 :   gimple_seq new_body = NULL;
   13030              : 
   13031        23436 :   if (ctx->record_type)
   13032              :     {
   13033        18896 :       t = build_fold_addr_expr_loc (loc, ctx->sender_decl);
   13034              :       /* fixup_child_record_type might have changed receiver_decl's type.  */
   13035        18896 :       t = fold_convert_loc (loc, TREE_TYPE (ctx->receiver_decl), t);
   13036        18896 :       gimple_seq_add_stmt (&new_body,
   13037        18896 :                            gimple_build_assign (ctx->receiver_decl, t));
   13038              :     }
   13039              : 
   13040        23436 :   gimple_seq_add_seq (&new_body, par_ilist);
   13041        23436 :   gimple_seq_add_seq (&new_body, par_body);
   13042        23436 :   gimple_seq_add_seq (&new_body, par_rlist);
   13043        23436 :   if (ctx->cancellable)
   13044          146 :     gimple_seq_add_stmt (&new_body, gimple_build_label (ctx->cancel_label));
   13045        23436 :   gimple_seq_add_seq (&new_body, par_olist);
   13046        23436 :   new_body = maybe_catch_exception (new_body);
   13047        23436 :   if (gimple_code (stmt) == GIMPLE_OMP_TASK)
   13048         3844 :     gimple_seq_add_stmt (&new_body,
   13049         3844 :                          gimple_build_omp_continue (integer_zero_node,
   13050              :                                                     integer_zero_node));
   13051        23436 :   gimple_seq_add_stmt (&new_body, gimple_build_omp_return (false));
   13052        23436 :   gimple_omp_set_body (stmt, new_body);
   13053              : 
   13054        23436 :   if (dep_bind && gimple_bind_block (par_bind) == NULL_TREE)
   13055          497 :     bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
   13056              :   else
   13057        22939 :     bind = gimple_build_bind (NULL, NULL, gimple_bind_block (par_bind));
   13058        45208 :   gsi_replace (gsi_p, dep_bind ? dep_bind : bind, true);
   13059        23436 :   gimple_bind_add_seq (bind, ilist);
   13060        23436 :   gimple_bind_add_stmt (bind, stmt);
   13061        23436 :   gimple_bind_add_seq (bind, olist);
   13062              : 
   13063        23436 :   pop_gimplify_context (NULL);
   13064              : 
   13065        23436 :   if (dep_bind)
   13066              :     {
   13067         1664 :       gimple_bind_add_seq (dep_bind, dep_ilist);
   13068         1664 :       gimple_bind_add_seq (dep_bind, tskred_ilist);
   13069         1664 :       gimple_bind_add_stmt (dep_bind, bind);
   13070         1664 :       gimple_bind_add_seq (dep_bind, tskred_olist);
   13071         1664 :       gimple_bind_add_seq (dep_bind, dep_olist);
   13072         1664 :       pop_gimplify_context (dep_bind);
   13073              :     }
   13074              : }
   13075              : 
   13076              :  /* Set EXPR as the hostaddr expression that should result from the clause C
   13077              :     in the target statement STMT.  Returns the tree that should be
   13078              :     passed as the hostaddr (a pointer to the array containing the expanded
   13079              :     hostaddrs and sizes of the clause).  */
   13080              : 
   13081              : static tree
   13082        37477 : lower_omp_map_iterator_expr (tree expr, tree c, gomp_target *stmt)
   13083              : {
   13084        37477 :   if (!OMP_CLAUSE_HAS_ITERATORS (c))
   13085              :     return expr;
   13086              : 
   13087          215 :   tree iterator = OMP_CLAUSE_ITERATORS (c);
   13088          215 :   tree index = OMP_ITERATOR_INDEX (iterator);
   13089          215 :   tree elems = OMP_ITERATOR_ELEMS (iterator);
   13090          215 :   gimple_seq *loop_body_p = enter_omp_iterator_loop_context (c, stmt);
   13091              : 
   13092              :    /* IN LOOP BODY:  */
   13093              :    /* elems[idx] = <expr>;  */
   13094          215 :   tree lhs = build4 (ARRAY_REF, ptr_type_node, elems, index,
   13095              :                      NULL_TREE, NULL_TREE);
   13096          215 :   tree mod_expr = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
   13097              :                               void_type_node, lhs, expr);
   13098          215 :   gimplify_and_add (mod_expr, loop_body_p);
   13099          215 :   exit_omp_iterator_loop_context (c);
   13100              : 
   13101          215 :   return build_fold_addr_expr_with_type (elems, ptr_type_node);
   13102              : }
   13103              : 
   13104              : /* Set SIZE as the size expression that should result from the clause C
   13105              :    in the target statement STMT.  Returns the tree that should be
   13106              :    passed as the clause size (a size_int with the value SIZE_MAX, indicating
   13107              :    that the clause uses an iterator).  */
   13108              : 
   13109              : static tree
   13110        82870 : lower_omp_map_iterator_size (tree size, tree c, gomp_target *stmt)
   13111              : {
   13112        82870 :   if (!OMP_CLAUSE_HAS_ITERATORS (c))
   13113              :     return size;
   13114              : 
   13115          219 :   tree iterator = OMP_CLAUSE_ITERATORS (c);
   13116          219 :   tree index = OMP_ITERATOR_INDEX (iterator);
   13117          219 :   tree elems = OMP_ITERATOR_ELEMS (iterator);
   13118          219 :   gimple_seq *loop_body_p = enter_omp_iterator_loop_context (c, stmt);
   13119              : 
   13120              :   /* IN LOOP BODY:  */
   13121              :   /* elems[idx+1] = <size>;  */
   13122          219 :   tree lhs = build4 (ARRAY_REF, ptr_type_node, elems,
   13123              :                      size_binop (PLUS_EXPR, index, size_int (1)),
   13124              :                      NULL_TREE, NULL_TREE);
   13125          219 :   tree mod_expr = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
   13126              :                               void_type_node, lhs, size);
   13127          219 :   gimplify_and_add (mod_expr, loop_body_p);
   13128          219 :   exit_omp_iterator_loop_context (c);
   13129              : 
   13130          219 :   return size_int (SIZE_MAX);
   13131              : }
   13132              : 
   13133              : /* Lower the GIMPLE_OMP_TARGET in the current statement
   13134              :    in GSI_P.  CTX holds context information for the directive.  */
   13135              : 
   13136              : static void
   13137        38655 : lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   13138              : {
   13139        38655 :   tree clauses;
   13140        38655 :   tree child_fn, t, c;
   13141        38655 :   gomp_target *stmt = as_a <gomp_target *> (gsi_stmt (*gsi_p));
   13142        38655 :   gbind *tgt_bind, *bind, *dep_bind = NULL;
   13143        38655 :   gimple_seq tgt_body, olist, ilist, fplist, new_body;
   13144        38655 :   location_t loc = gimple_location (stmt);
   13145        38655 :   bool offloaded, data_region;
   13146        38655 :   unsigned int map_cnt = 0;
   13147        38655 :   tree in_reduction_clauses = NULL_TREE;
   13148              : 
   13149        38655 :   tree deep_map_cnt = NULL_TREE;
   13150        38655 :   tree deep_map_data = NULL_TREE;
   13151        38655 :   tree deep_map_offset_data = NULL_TREE;
   13152        38655 :   tree deep_map_offset = NULL_TREE;
   13153              : 
   13154        38655 :   offloaded = is_gimple_omp_offloaded (stmt);
   13155        38655 :   switch (gimple_omp_target_kind (stmt))
   13156              :     {
   13157        12881 :     case GF_OMP_TARGET_KIND_REGION:
   13158        12881 :       tree *p, *q;
   13159        12881 :       q = &in_reduction_clauses;
   13160        85351 :       for (p = gimple_omp_target_clauses_ptr (stmt); *p; )
   13161        72470 :         if (OMP_CLAUSE_CODE (*p) == OMP_CLAUSE_IN_REDUCTION)
   13162              :           {
   13163          422 :             *q = *p;
   13164          422 :             q = &OMP_CLAUSE_CHAIN (*q);
   13165          422 :             *p = OMP_CLAUSE_CHAIN (*p);
   13166              :           }
   13167              :         else
   13168        72048 :           p = &OMP_CLAUSE_CHAIN (*p);
   13169        12881 :       *q = NULL_TREE;
   13170        12881 :       *p = in_reduction_clauses;
   13171              :       /* FALLTHRU */
   13172              :     case GF_OMP_TARGET_KIND_UPDATE:
   13173              :     case GF_OMP_TARGET_KIND_ENTER_DATA:
   13174              :     case GF_OMP_TARGET_KIND_EXIT_DATA:
   13175              :     case GF_OMP_TARGET_KIND_OACC_PARALLEL:
   13176              :     case GF_OMP_TARGET_KIND_OACC_KERNELS:
   13177              :     case GF_OMP_TARGET_KIND_OACC_SERIAL:
   13178              :     case GF_OMP_TARGET_KIND_OACC_UPDATE:
   13179              :     case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
   13180              :     case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
   13181              :     case GF_OMP_TARGET_KIND_OACC_DECLARE:
   13182              :     case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
   13183              :     case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
   13184              :       data_region = false;
   13185              :       break;
   13186              :     case GF_OMP_TARGET_KIND_DATA:
   13187              :     case GF_OMP_TARGET_KIND_OACC_DATA:
   13188              :     case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
   13189              :     case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
   13190        38655 :       data_region = true;
   13191              :       break;
   13192            0 :     default:
   13193            0 :       gcc_unreachable ();
   13194              :     }
   13195              : 
   13196              :   /* Ensure that requires map is written via output_offload_tables, even if only
   13197              :      'target (enter/exit) data' is used in the translation unit.  */
   13198        38655 :   if (ENABLE_OFFLOADING && (omp_requires_mask & OMP_REQUIRES_TARGET_USED))
   13199              :     g->have_offload = true;
   13200              : 
   13201        38655 :   clauses = gimple_omp_target_clauses (stmt);
   13202              : 
   13203        38655 :   gimple_seq dep_ilist = NULL;
   13204        38655 :   gimple_seq dep_olist = NULL;
   13205        38655 :   bool has_depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND) != NULL_TREE;
   13206        38655 :   if (has_depend || in_reduction_clauses)
   13207              :     {
   13208          392 :       push_gimplify_context ();
   13209          392 :       dep_bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
   13210          392 :       if (has_depend)
   13211          329 :         lower_depend_clauses (gimple_omp_target_clauses_ptr (stmt),
   13212              :                               &dep_ilist, &dep_olist);
   13213          392 :       if (in_reduction_clauses)
   13214          314 :         lower_rec_input_clauses (in_reduction_clauses, &dep_ilist, &dep_olist,
   13215              :                                  ctx, NULL);
   13216              :     }
   13217              : 
   13218        38655 :   tgt_bind = NULL;
   13219        38655 :   tgt_body = NULL;
   13220        38655 :   if (offloaded)
   13221              :     {
   13222        22276 :       tgt_bind = gimple_seq_first_stmt_as_a_bind (gimple_omp_body (stmt));
   13223        22276 :       tgt_body = gimple_bind_body (tgt_bind);
   13224              :     }
   13225        16379 :   else if (data_region)
   13226         4024 :     tgt_body = gimple_omp_body (stmt);
   13227        38655 :   child_fn = ctx->cb.dst_fn;
   13228              : 
   13229        38655 :   push_gimplify_context ();
   13230        38655 :   fplist = NULL;
   13231              : 
   13232        38655 :   ilist = NULL;
   13233        38655 :   olist = NULL;
   13234              : 
   13235        38655 :   gimple_seq alloc_dlist = NULL;
   13236        38655 :   hash_map<tree, tree> alloc_map;
   13237        77310 :   hash_map<tree, gimple_seq> alloc_seq_map;
   13238              : 
   13239              :   /* The value expression for USES_ALLOCATORS needs to be setup before the
   13240              :      allocator is used via lower_private_allocate.  */
   13241       223049 :   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
   13242       145739 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USES_ALLOCATORS)
   13243              :       {
   13244           86 :         tree allocator = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
   13245           86 :         tree new_allocator = lookup_decl (allocator, ctx);
   13246           86 :         tree x = build_receiver_ref (allocator, false, ctx);
   13247           86 :         SET_DECL_VALUE_EXPR (new_allocator, x);
   13248           86 :         DECL_HAS_VALUE_EXPR_P (new_allocator) = 1;
   13249           86 :         map_cnt++;
   13250              :       }
   13251              : 
   13252       184394 :   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
   13253       145739 :     switch (OMP_CLAUSE_CODE (c))
   13254              :       {
   13255              :         tree var, x, new_var, allocator, allocate_ptr, size;
   13256              :         gimple_seq alloc_seq;
   13257              :         bool by_ref;
   13258              : 
   13259              :       default:
   13260        98152 :         break;
   13261        80049 :       case OMP_CLAUSE_MAP:
   13262              : #if CHECKING_P
   13263              :         /* First check what we're prepared to handle in the following.  */
   13264        80049 :         switch (OMP_CLAUSE_MAP_KIND (c))
   13265              :           {
   13266              :           case GOMP_MAP_ALLOC:
   13267              :           case GOMP_MAP_TO:
   13268              :           case GOMP_MAP_FROM:
   13269              :           case GOMP_MAP_TOFROM:
   13270              :           case GOMP_MAP_POINTER:
   13271              :           case GOMP_MAP_TO_PSET:
   13272              :           case GOMP_MAP_DELETE:
   13273              :           case GOMP_MAP_RELEASE:
   13274              :           case GOMP_MAP_ALWAYS_TO:
   13275              :           case GOMP_MAP_ALWAYS_FROM:
   13276              :           case GOMP_MAP_ALWAYS_TOFROM:
   13277              :           case GOMP_MAP_FORCE_PRESENT:
   13278              :           case GOMP_MAP_ALWAYS_PRESENT_FROM:
   13279              :           case GOMP_MAP_ALWAYS_PRESENT_TO:
   13280              :           case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
   13281              : 
   13282              :           case GOMP_MAP_FIRSTPRIVATE_POINTER:
   13283              :           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
   13284              :           case GOMP_MAP_STRUCT:
   13285              :           case GOMP_MAP_STRUCT_UNORD:
   13286              :           case GOMP_MAP_ALWAYS_POINTER:
   13287              :           case GOMP_MAP_ATTACH:
   13288              :           case GOMP_MAP_DETACH:
   13289              :           case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
   13290              :           case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
   13291              :             break;
   13292         2587 :           case GOMP_MAP_IF_PRESENT:
   13293         2587 :           case GOMP_MAP_FORCE_ALLOC:
   13294         2587 :           case GOMP_MAP_FORCE_TO:
   13295         2587 :           case GOMP_MAP_FORCE_FROM:
   13296         2587 :           case GOMP_MAP_FORCE_TOFROM:
   13297         2587 :           case GOMP_MAP_FORCE_DEVICEPTR:
   13298         2587 :           case GOMP_MAP_DEVICE_RESIDENT:
   13299         2587 :           case GOMP_MAP_LINK:
   13300         2587 :           case GOMP_MAP_FORCE_DETACH:
   13301         2587 :             gcc_assert (is_gimple_omp_oacc (stmt));
   13302              :             break;
   13303            0 :           default:
   13304            0 :             gcc_unreachable ();
   13305              :           }
   13306              : #endif
   13307              :           /* FALLTHRU */
   13308        93164 :       case OMP_CLAUSE_TO:
   13309        93164 :       case OMP_CLAUSE_FROM:
   13310        93164 :       oacc_firstprivate:
   13311        93164 :         var = OMP_CLAUSE_DECL (c);
   13312        93164 :         {
   13313        93164 :           tree extra = lang_hooks.decls.omp_deep_mapping_cnt (stmt, c, &ilist);
   13314        93164 :           if (extra != NULL_TREE && deep_map_cnt != NULL_TREE)
   13315           59 :             deep_map_cnt = fold_build2_loc (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
   13316              :                                             size_type_node, deep_map_cnt,
   13317              :                                             extra);
   13318        93105 :           else if (extra != NULL_TREE)
   13319              :             deep_map_cnt = extra;
   13320              :         }
   13321              : 
   13322        93019 :         if (deep_map_cnt
   13323        93386 :             && OMP_CLAUSE_HAS_ITERATORS (c))
   13324            0 :           sorry ("iterators used together with deep mapping are not "
   13325              :                  "supported yet");
   13326              : 
   13327        93164 :         if (!DECL_P (var))
   13328              :           {
   13329        37052 :             if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
   13330        37052 :                 || (!OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
   13331        35440 :                     && (OMP_CLAUSE_MAP_KIND (c)
   13332              :                         != GOMP_MAP_FIRSTPRIVATE_POINTER)))
   13333        37052 :               map_cnt++;
   13334        47587 :             continue;
   13335              :           }
   13336              : 
   13337        56112 :         if (DECL_SIZE (var)
   13338        56112 :             && !poly_int_tree_p (DECL_SIZE (var)))
   13339              :           {
   13340          719 :             tree var2 = DECL_VALUE_EXPR (var);
   13341          719 :             gcc_assert (TREE_CODE (var2) == INDIRECT_REF);
   13342          719 :             var2 = TREE_OPERAND (var2, 0);
   13343          719 :             gcc_assert (DECL_P (var2));
   13344              :             var = var2;
   13345              :           }
   13346              : 
   13347        56112 :         if (offloaded
   13348        38687 :             && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13349        90903 :             && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
   13350        31496 :                 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
   13351              :           {
   13352         3724 :             if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
   13353              :               {
   13354          419 :                 if (is_global_var (maybe_lookup_decl_in_outer_ctx (var, ctx))
   13355          419 :                     && varpool_node::get_create (var)->offloadable)
   13356            2 :                   continue;
   13357              : 
   13358          417 :                 tree type = build_pointer_type (TREE_TYPE (var));
   13359          417 :                 tree new_var = lookup_decl (var, ctx);
   13360          417 :                 x = create_tmp_var_raw (type, get_name (new_var));
   13361          417 :                 gimple_add_tmp_var (x);
   13362          417 :                 x = build_simple_mem_ref (x);
   13363          417 :                 SET_DECL_VALUE_EXPR (new_var, x);
   13364          417 :                 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
   13365              :               }
   13366         3722 :             continue;
   13367         3722 :           }
   13368              : 
   13369        52388 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13370        40885 :             && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
   13371        40674 :                 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
   13372        52653 :             && is_omp_target (stmt))
   13373              :           {
   13374          241 :             gcc_assert (maybe_lookup_field (c, ctx));
   13375          241 :             map_cnt++;
   13376          241 :             continue;
   13377              :           }
   13378              : 
   13379        52147 :         if (!maybe_lookup_field (c, ctx))
   13380         6570 :           continue;
   13381              : 
   13382              :         /* Don't remap compute constructs' reduction variables, because the
   13383              :            intermediate result must be local to each gang.  */
   13384        74424 :         if (offloaded && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13385        24951 :                            && is_gimple_omp_oacc (ctx->stmt)
   13386        13572 :                            && OMP_CLAUSE_MAP_IN_REDUCTION (c)))
   13387              :           {
   13388        28067 :             x = build_receiver_ref (c, true, ctx);
   13389        28067 :             tree new_var = lookup_decl (var, ctx);
   13390              : 
   13391        28067 :             if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13392        24171 :                 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
   13393         2992 :                 && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
   13394        31059 :                 && TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
   13395          425 :               x = build_simple_mem_ref (x);
   13396        28067 :             if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
   13397              :               {
   13398         3896 :                 gcc_assert (is_gimple_omp_oacc (ctx->stmt));
   13399         3896 :                 if (omp_privatize_by_reference (new_var)
   13400         3896 :                     && (TREE_CODE (TREE_TYPE (new_var)) != POINTER_TYPE
   13401           44 :                         || DECL_BY_REFERENCE (var)))
   13402              :                   {
   13403              :                     /* Create a local object to hold the instance
   13404              :                        value.  */
   13405          455 :                     tree type = TREE_TYPE (TREE_TYPE (new_var));
   13406          455 :                     const char *id = IDENTIFIER_POINTER (DECL_NAME (new_var));
   13407          455 :                     tree inst = create_tmp_var (type, id);
   13408          455 :                     gimplify_assign (inst, fold_indirect_ref (x), &fplist);
   13409          455 :                     x = build_fold_addr_expr (inst);
   13410              :                   }
   13411         3896 :                 gimplify_assign (new_var, x, &fplist);
   13412              :               }
   13413        24171 :             else if (DECL_P (new_var))
   13414              :               {
   13415        24171 :                 SET_DECL_VALUE_EXPR (new_var, x);
   13416        24171 :                 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
   13417              :               }
   13418              :             else
   13419            0 :               gcc_unreachable ();
   13420              :           }
   13421        45577 :         map_cnt++;
   13422        45577 :         break;
   13423              : 
   13424        15873 :       case OMP_CLAUSE_FIRSTPRIVATE:
   13425        15873 :       omp_firstprivate_recv:
   13426        15873 :         gcc_checking_assert (offloaded);
   13427        15873 :         if (is_gimple_omp_oacc (ctx->stmt))
   13428              :           {
   13429              :             /* No 'firstprivate' clauses on OpenACC 'kernels'.  */
   13430         3896 :             gcc_checking_assert (!is_oacc_kernels (ctx));
   13431              :             /* Likewise, on OpenACC 'kernels' decomposed parts.  */
   13432         3896 :             gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
   13433              : 
   13434         3896 :             goto oacc_firstprivate;
   13435              :           }
   13436        11977 :         map_cnt++;
   13437        11977 :         var = OMP_CLAUSE_DECL (c);
   13438        11977 :         new_var = lookup_decl (var, ctx);
   13439        11977 :         allocator = NULL_TREE;
   13440        11977 :         allocate_ptr = NULL_TREE;
   13441        11977 :         size = TREE_TYPE (var);
   13442        11977 :         by_ref = omp_privatize_by_reference (var);
   13443        11977 :         if (by_ref)
   13444          849 :           size = TREE_TYPE (size);
   13445        11977 :         size = TYPE_SIZE_UNIT (size);
   13446        11977 :         if (is_variable_sized (var, by_ref))
   13447           42 :           size = lookup_decl (size, ctx);
   13448        11977 :         alloc_seq = NULL;
   13449        11977 :         if (lower_private_allocate (var, new_var, allocator, allocate_ptr,
   13450              :                                     &alloc_seq, ctx, by_ref, size))
   13451              :           {
   13452           68 :             alloc_map.put (new_var, allocate_ptr);
   13453           68 :             alloc_seq_map.put (new_var, alloc_seq);
   13454              :           }
   13455        11977 :         if (!by_ref && !is_gimple_reg_type (TREE_TYPE (var)))
   13456              :           {
   13457          163 :             if (is_variable_sized (var))
   13458              :               {
   13459           10 :                 tree pvar = DECL_VALUE_EXPR (var);
   13460           10 :                 gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
   13461           10 :                 pvar = TREE_OPERAND (pvar, 0);
   13462           10 :                 gcc_assert (DECL_P (pvar));
   13463           10 :                 tree new_pvar = lookup_decl (pvar, ctx);
   13464           10 :                 x = build_fold_indirect_ref (new_pvar);
   13465           10 :                 TREE_THIS_NOTRAP (x) = 1;
   13466              :               }
   13467          153 :             else if (allocate_ptr)
   13468           22 :               x = build_fold_indirect_ref (allocate_ptr);
   13469              :             else
   13470          131 :               x = build_receiver_ref (var, true, ctx);
   13471          163 :             SET_DECL_VALUE_EXPR (new_var, x);
   13472          163 :             DECL_HAS_VALUE_EXPR_P (new_var) = 1;
   13473              :           }
   13474              :           /* Fortran array descriptors: firstprivate of data + attach.  */
   13475        11977 :           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
   13476        11958 :               && lang_hooks.decls.omp_array_data (var, true)
   13477        12019 :               && lang_hooks.decls.omp_array_data_privatize (var))
   13478           26 :             map_cnt += 2;
   13479              : 
   13480        12125 :       do_dtor:
   13481        12125 :         if (allocator)
   13482              :           {
   13483          125 :             if (!is_gimple_val (allocator))
   13484              :               {
   13485            0 :                 tree avar = create_tmp_var (TREE_TYPE (allocator));
   13486            0 :                 gimplify_assign (avar, allocator, &alloc_dlist);
   13487            0 :                 allocator = avar;
   13488              :               }
   13489          125 :             if (!is_gimple_val (allocate_ptr))
   13490              :               {
   13491            0 :                 tree apvar = create_tmp_var (TREE_TYPE (allocate_ptr));
   13492            0 :                 gimplify_assign (apvar, allocate_ptr, &alloc_dlist);
   13493            0 :                 allocate_ptr = apvar;
   13494              :               }
   13495          125 :             tree f = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
   13496          125 :             gimple *g = gimple_build_call (f, 2, allocate_ptr, allocator);
   13497          125 :             gimple_seq_add_stmt (&alloc_dlist, g);
   13498              :           }
   13499              :         break;
   13500              : 
   13501          228 :       case OMP_CLAUSE_PRIVATE:
   13502          228 :         gcc_checking_assert (offloaded);
   13503          228 :         if (is_gimple_omp_oacc (ctx->stmt))
   13504              :           {
   13505              :             /* No 'private' clauses on OpenACC 'kernels'.  */
   13506           80 :             gcc_checking_assert (!is_oacc_kernels (ctx));
   13507              :             /* Likewise, on OpenACC 'kernels' decomposed parts.  */
   13508           80 :             gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
   13509              : 
   13510              :             break;
   13511              :           }
   13512          148 :         var = OMP_CLAUSE_DECL (c);
   13513          148 :         new_var = lookup_decl (var, ctx);
   13514          148 :         allocator = NULL_TREE;
   13515          148 :         allocate_ptr = NULL_TREE;
   13516          148 :         alloc_seq = NULL;
   13517          148 :         size = TREE_TYPE (var);
   13518          148 :         by_ref = omp_privatize_by_reference (var);
   13519          148 :         if (by_ref)
   13520           22 :           size = TREE_TYPE (size);
   13521          148 :         size = TYPE_SIZE_UNIT (size);
   13522          148 :         if (is_variable_sized (var, by_ref))
   13523           21 :           size = lookup_decl (size, ctx);
   13524          148 :         lower_private_allocate (var, new_var, allocator, allocate_ptr,
   13525              :                                 &alloc_seq, ctx, by_ref, size);
   13526          148 :         if (allocate_ptr)
   13527              :           {
   13528           57 :             alloc_map.put (new_var, allocate_ptr);
   13529           57 :             alloc_seq_map.put (new_var, alloc_seq);
   13530              :           }
   13531          148 :         if (!allocate_ptr && is_variable_sized (var))
   13532              :           {
   13533            1 :             tree new_var = lookup_decl (var, ctx);
   13534            1 :             tree pvar = DECL_VALUE_EXPR (var);
   13535            1 :             gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
   13536            1 :             pvar = TREE_OPERAND (pvar, 0);
   13537            1 :             gcc_assert (DECL_P (pvar));
   13538            1 :             tree new_pvar = lookup_decl (pvar, ctx);
   13539            1 :             x = build_fold_indirect_ref (new_pvar);
   13540            1 :             TREE_THIS_NOTRAP (x) = 1;
   13541            1 :             SET_DECL_VALUE_EXPR (new_var, x);
   13542            1 :             DECL_HAS_VALUE_EXPR_P (new_var) = 1;
   13543              :           }
   13544          148 :         goto do_dtor;
   13545              : 
   13546         2431 :       case OMP_CLAUSE_USE_DEVICE_PTR:
   13547         2431 :       case OMP_CLAUSE_USE_DEVICE_ADDR:
   13548         2431 :       case OMP_CLAUSE_HAS_DEVICE_ADDR:
   13549         2431 :       case OMP_CLAUSE_IS_DEVICE_PTR:
   13550         2431 :         var = OMP_CLAUSE_DECL (c);
   13551         2431 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
   13552              :           {
   13553          253 :             while (TREE_CODE (var) == INDIRECT_REF
   13554          253 :                    || TREE_CODE (var) == ARRAY_REF)
   13555           17 :               var = TREE_OPERAND (var, 0);
   13556          236 :             if (lang_hooks.decls.omp_array_data (var, true))
   13557           19 :               goto omp_firstprivate_recv;
   13558              :           }
   13559         2412 :         map_cnt++;
   13560         2412 :         if (is_variable_sized (var))
   13561              :           {
   13562           12 :             tree new_var = lookup_decl (var, ctx);
   13563           12 :             tree pvar = DECL_VALUE_EXPR (var);
   13564           12 :             gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
   13565           12 :             pvar = TREE_OPERAND (pvar, 0);
   13566           12 :             gcc_assert (DECL_P (pvar));
   13567           12 :             tree new_pvar = lookup_decl (pvar, ctx);
   13568           12 :             x = build_fold_indirect_ref (new_pvar);
   13569           12 :             TREE_THIS_NOTRAP (x) = 1;
   13570           12 :             SET_DECL_VALUE_EXPR (new_var, x);
   13571           12 :             DECL_HAS_VALUE_EXPR_P (new_var) = 1;
   13572              :           }
   13573         2400 :         else if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
   13574          543 :                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
   13575         2072 :                   && !omp_privatize_by_reference (var)
   13576          703 :                   && !omp_is_allocatable_or_ptr (var)
   13577          564 :                   && !lang_hooks.decls.omp_array_data (var, true))
   13578         4435 :                  || TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
   13579              :           {
   13580          389 :             tree new_var = lookup_decl (var, ctx);
   13581          389 :             tree type = build_pointer_type (TREE_TYPE (var));
   13582          389 :             x = create_tmp_var_raw (type, get_name (new_var));
   13583          389 :             gimple_add_tmp_var (x);
   13584          389 :             x = build_simple_mem_ref (x);
   13585          389 :             SET_DECL_VALUE_EXPR (new_var, x);
   13586          389 :             DECL_HAS_VALUE_EXPR_P (new_var) = 1;
   13587              :           }
   13588              :         else
   13589              :           {
   13590         2011 :             tree new_var = lookup_decl (var, ctx);
   13591         2011 :             x = create_tmp_var_raw (TREE_TYPE (new_var), get_name (new_var));
   13592         2011 :             gimple_add_tmp_var (x);
   13593         2011 :             SET_DECL_VALUE_EXPR (new_var, x);
   13594         2011 :             DECL_HAS_VALUE_EXPR_P (new_var) = 1;
   13595              :           }
   13596              :         break;
   13597              :       }
   13598              : 
   13599        38655 :   if (offloaded)
   13600              :     {
   13601        22276 :       target_nesting_level++;
   13602        22276 :       lower_omp (&tgt_body, ctx);
   13603        22276 :       target_nesting_level--;
   13604              :     }
   13605        16379 :   else if (data_region)
   13606         4024 :     lower_omp (&tgt_body, ctx);
   13607              : 
   13608        38655 :   if (offloaded)
   13609              :     {
   13610              :       /* Declare all the variables created by mapping and the variables
   13611              :          declared in the scope of the target body.  */
   13612        22276 :       record_vars_into (ctx->block_vars, child_fn);
   13613        22276 :       maybe_remove_omp_member_access_dummy_vars (tgt_bind);
   13614        22276 :       record_vars_into (gimple_bind_vars (tgt_bind), child_fn);
   13615              :     }
   13616              : 
   13617        38655 :   if (ctx->record_type)
   13618              :     {
   13619        33566 :       if (deep_map_cnt && TREE_CODE (deep_map_cnt) == INTEGER_CST)
   13620              :         /* map_cnt = map_cnt + tree_to_hwi (deep_map_cnt); */
   13621              :         /* deep_map_cnt = NULL_TREE; */
   13622            0 :         gcc_unreachable ();
   13623          145 :       else if (deep_map_cnt)
   13624              :         {
   13625          145 :           gcc_assert (flexible_array_type_p (ctx->record_type));
   13626          145 :           tree n = create_tmp_var_raw (size_type_node, "nn_map");
   13627          145 :           gimple_add_tmp_var (n);
   13628          145 :           gimplify_assign (n, deep_map_cnt, &ilist);
   13629          145 :           deep_map_cnt = n;
   13630              :         }
   13631          145 :       ctx->sender_decl
   13632        33566 :         = create_tmp_var (deep_map_cnt ? build_pointer_type (ctx->record_type)
   13633              :                                        : ctx->record_type, ".omp_data_arr");
   13634        33566 :       DECL_NAMELESS (ctx->sender_decl) = 1;
   13635        33566 :       TREE_ADDRESSABLE (ctx->sender_decl) = 1;
   13636        66987 :       t = make_tree_vec (deep_map_cnt ? 4 : 3);
   13637        33566 :       TREE_VEC_ELT (t, 0) = ctx->sender_decl;
   13638        33566 :       TREE_VEC_ELT (t, 1)
   13639        67132 :         = create_tmp_var (deep_map_cnt
   13640          145 :                           ? build_pointer_type (size_type_node)
   13641        33421 :                           : build_array_type_nelts (size_type_node, map_cnt),
   13642              :                           ".omp_data_sizes");
   13643        33566 :       DECL_NAMELESS (TREE_VEC_ELT (t, 1)) = 1;
   13644        33566 :       TREE_ADDRESSABLE (TREE_VEC_ELT (t, 1)) = 1;
   13645        33566 :       TREE_STATIC (TREE_VEC_ELT (t, 1)) = 1;
   13646        33566 :       tree tkind_type = short_unsigned_type_node;
   13647        33566 :       int talign_shift = 8;
   13648        33566 :       TREE_VEC_ELT (t, 2)
   13649        67132 :         = create_tmp_var (deep_map_cnt
   13650          145 :                           ? build_pointer_type (tkind_type)
   13651        33421 :                           : build_array_type_nelts (tkind_type, map_cnt),
   13652              :                           ".omp_data_kinds");
   13653        33566 :       DECL_NAMELESS (TREE_VEC_ELT (t, 2)) = 1;
   13654        33566 :       TREE_ADDRESSABLE (TREE_VEC_ELT (t, 2)) = 1;
   13655        33566 :       TREE_STATIC (TREE_VEC_ELT (t, 2)) = 1;
   13656        33566 :       gimple_omp_target_set_data_arg (stmt, t);
   13657              : 
   13658        33566 :       if (deep_map_cnt)
   13659              :         {
   13660          145 :           tree tmp, size;
   13661          145 :           size = create_tmp_var (size_type_node, NULL);
   13662          145 :           DECL_NAMELESS (size) = 1;
   13663          145 :           gimplify_assign (size,
   13664              :                            fold_build2_loc (UNKNOWN_LOCATION, PLUS_EXPR,
   13665              :                                             size_type_node, deep_map_cnt,
   13666              :                                             build_int_cst (size_type_node,
   13667          145 :                                                            map_cnt)), &ilist);
   13668          145 :           TREE_VEC_ELT (t, 3) = size;
   13669              : 
   13670          145 :           tree call = builtin_decl_explicit (BUILT_IN_MALLOC);
   13671          145 :           size = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR,
   13672              :                                   size_type_node, deep_map_cnt,
   13673          145 :                                   TYPE_SIZE_UNIT (ptr_type_node));
   13674          145 :           size = fold_build2_loc (UNKNOWN_LOCATION, PLUS_EXPR,
   13675              :                                   size_type_node, size,
   13676          145 :                                   TYPE_SIZE_UNIT (ctx->record_type));
   13677          145 :           tmp = build_call_expr_loc (input_location, call, 1, size);
   13678          145 :           gimplify_assign (ctx->sender_decl, tmp, &ilist);
   13679              : 
   13680          145 :           size = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR,
   13681          145 :                                   size_type_node, TREE_VEC_ELT (t, 3),
   13682          145 :                                   TYPE_SIZE_UNIT (size_type_node));
   13683          145 :           tmp = build_call_expr_loc (input_location, call, 1, size);
   13684          145 :           gimplify_assign (TREE_VEC_ELT (t, 1), tmp, &ilist);
   13685              : 
   13686          145 :           size = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR,
   13687          145 :                                   size_type_node, TREE_VEC_ELT (t, 3),
   13688          145 :                                   TYPE_SIZE_UNIT (tkind_type));
   13689          145 :           tmp = build_call_expr_loc (input_location, call, 1, size);
   13690          145 :           gimplify_assign (TREE_VEC_ELT (t, 2), tmp, &ilist);
   13691          145 :           tree field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (ctx->sender_decl)));
   13692          599 :           for ( ; DECL_CHAIN (field) != NULL_TREE; field = DECL_CHAIN (field))
   13693              :             ;
   13694          145 :           gcc_assert (TREE_CODE (TREE_TYPE (field)));
   13695          145 :           tmp = build_fold_indirect_ref (ctx->sender_decl);
   13696          145 :           deep_map_data = omp_build_component_ref (tmp, field);
   13697          145 :           deep_map_offset_data = create_tmp_var_raw (size_type_node,
   13698              :                                                      "map_offset_data");
   13699          145 :           deep_map_offset = create_tmp_var_raw (size_type_node, "map_offset");
   13700          145 :           gimple_add_tmp_var (deep_map_offset_data);
   13701          145 :           gimple_add_tmp_var (deep_map_offset);
   13702          145 :           gimplify_assign (deep_map_offset_data, build_int_cst (size_type_node,
   13703              :                                                                 0), &ilist);
   13704          145 :           gimplify_assign (deep_map_offset, build_int_cst (size_type_node,
   13705          145 :                                                            map_cnt), &ilist);
   13706              :         }
   13707              : 
   13708        33566 :       vec<constructor_elt, va_gc> *vsize;
   13709        33566 :       vec<constructor_elt, va_gc> *vkind;
   13710        33566 :       vec_alloc (vsize, map_cnt);
   13711        33566 :       vec_alloc (vkind, map_cnt);
   13712        33566 :       unsigned int map_idx = 0;
   13713              : 
   13714       202163 :       for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
   13715       135031 :         switch (OMP_CLAUSE_CODE (c))
   13716              :           {
   13717              :             tree ovar, nc, s, purpose, var, x, type;
   13718              :             unsigned int talign;
   13719              : 
   13720              :           default:
   13721       131048 :             break;
   13722              : 
   13723        90745 :           case OMP_CLAUSE_MAP:
   13724        90745 :           case OMP_CLAUSE_TO:
   13725        90745 :           case OMP_CLAUSE_FROM:
   13726        90745 :           oacc_firstprivate_map:
   13727        90745 :             nc = c;
   13728        90745 :             ovar = OMP_CLAUSE_DECL (c);
   13729        90745 :             if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13730        90745 :                 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
   13731        74167 :                     || (OMP_CLAUSE_MAP_KIND (c)
   13732              :                         == GOMP_MAP_FIRSTPRIVATE_REFERENCE)))
   13733              :               break;
   13734        86853 :             if (deep_map_cnt)
   13735              :               {
   13736          431 :                 unsigned HOST_WIDE_INT tkind2;
   13737          431 :                 switch (OMP_CLAUSE_CODE (c))
   13738              :                   {
   13739          427 :                   case OMP_CLAUSE_MAP:
   13740          427 :                     tkind2 = OMP_CLAUSE_MAP_KIND (c);
   13741          427 :                     if (OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (c)
   13742          427 :                         && (((tkind2 & GOMP_MAP_FLAG_SPECIAL_BITS)
   13743              :                              & ~GOMP_MAP_IMPLICIT)
   13744              :                             == 0))
   13745              :                       {
   13746              :                         /* If this is an implicit map, and the GOMP_MAP_IMPLICIT
   13747              :                            bits are not interfered by other special bit
   13748              :                            encodings, then turn the GOMP_IMPLICIT_BIT flag on
   13749              :                            for the runtime to see.  */
   13750          120 :                         tkind2 |= GOMP_MAP_IMPLICIT;
   13751              :                       }
   13752              :                     break;
   13753              :                   case OMP_CLAUSE_FIRSTPRIVATE: tkind2 = GOMP_MAP_TO; break;
   13754              :                   case OMP_CLAUSE_TO: tkind2 = GOMP_MAP_TO; break;
   13755            2 :                   case OMP_CLAUSE_FROM: tkind2 = GOMP_MAP_FROM; break;
   13756            0 :                   default: gcc_unreachable ();
   13757              :                   }
   13758          431 :                 lang_hooks.decls.omp_deep_mapping (stmt, c, tkind2,
   13759              :                                                    deep_map_data,
   13760          431 :                                                    TREE_VEC_ELT (t, 1),
   13761          431 :                                                    TREE_VEC_ELT (t, 2),
   13762              :                                                    deep_map_offset_data,
   13763              :                                                    deep_map_offset, &ilist);
   13764              :               }
   13765        86853 :             if (!DECL_P (ovar))
   13766              :               {
   13767        37052 :                 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13768        37052 :                     && OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c))
   13769              :                   {
   13770            0 :                     nc = OMP_CLAUSE_CHAIN (c);
   13771            0 :                     gcc_checking_assert (OMP_CLAUSE_DECL (nc)
   13772              :                                          == get_base_address (ovar));
   13773            0 :                     ovar = OMP_CLAUSE_DECL (nc);
   13774              :                   }
   13775              :                 else
   13776              :                   {
   13777        37052 :                     tree x = build_sender_ref (c, ctx);
   13778        37052 :                     tree v = ovar;
   13779        37052 :                     if (in_reduction_clauses
   13780          163 :                         && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13781        37215 :                         && OMP_CLAUSE_MAP_IN_REDUCTION (c))
   13782              :                       {
   13783          104 :                         v = unshare_expr (v);
   13784          104 :                         tree *p = &v;
   13785          104 :                         while (handled_component_p (*p)
   13786              :                                || TREE_CODE (*p) == INDIRECT_REF
   13787              :                                || TREE_CODE (*p) == ADDR_EXPR
   13788              :                                || TREE_CODE (*p) == MEM_REF
   13789          256 :                                || TREE_CODE (*p) == NON_LVALUE_EXPR)
   13790          152 :                           p = &TREE_OPERAND (*p, 0);
   13791          104 :                         tree d = *p;
   13792          104 :                         if (is_variable_sized (d))
   13793              :                           {
   13794           16 :                             gcc_assert (DECL_HAS_VALUE_EXPR_P (d));
   13795           16 :                             d = DECL_VALUE_EXPR (d);
   13796           16 :                             gcc_assert (TREE_CODE (d) == INDIRECT_REF);
   13797           16 :                             d = TREE_OPERAND (d, 0);
   13798           16 :                             gcc_assert (DECL_P (d));
   13799              :                           }
   13800          104 :                         splay_tree_key key
   13801          104 :                           = (splay_tree_key) &DECL_CONTEXT (d);
   13802          104 :                         tree nd = (tree) splay_tree_lookup (ctx->field_map,
   13803          104 :                                                             key)->value;
   13804          104 :                         if (d == *p)
   13805           88 :                           *p = nd;
   13806              :                         else
   13807           16 :                           *p = build_fold_indirect_ref (nd);
   13808              :                       }
   13809        37052 :                     v = build_fold_addr_expr_with_type (v, ptr_type_node);
   13810        37052 :                     v = lower_omp_map_iterator_expr (v, c, stmt);
   13811        37052 :                     gimplify_assign (x, v, &ilist);
   13812        37052 :                     nc = NULL_TREE;
   13813              :                   }
   13814              :               }
   13815              :             else
   13816              :               {
   13817        49801 :                 if (DECL_SIZE (ovar)
   13818        49801 :                     && !poly_int_tree_p (DECL_SIZE (ovar)))
   13819              :                   {
   13820            7 :                     tree ovar2 = DECL_VALUE_EXPR (ovar);
   13821            7 :                     gcc_assert (TREE_CODE (ovar2) == INDIRECT_REF);
   13822            7 :                     ovar2 = TREE_OPERAND (ovar2, 0);
   13823            7 :                     gcc_assert (DECL_P (ovar2));
   13824              :                     ovar = ovar2;
   13825              :                   }
   13826        49801 :                 if (!maybe_lookup_field (c, ctx)
   13827        53784 :                     && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13828         3983 :                          && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
   13829         3983 :                              || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)))
   13830         3983 :                   continue;
   13831              :               }
   13832              : 
   13833        82870 :             talign = TYPE_ALIGN_UNIT (TREE_TYPE (ovar));
   13834        82870 :             if (DECL_P (ovar) && DECL_ALIGN_UNIT (ovar) > talign)
   13835         7301 :               talign = DECL_ALIGN_UNIT (ovar);
   13836              : 
   13837        82870 :             var = NULL_TREE;
   13838        82870 :             if (nc)
   13839              :               {
   13840        45818 :                 if (in_reduction_clauses
   13841          879 :                     && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13842        46697 :                     && OMP_CLAUSE_MAP_IN_REDUCTION (c))
   13843              :                   {
   13844          268 :                     tree d = ovar;
   13845          268 :                     if (is_variable_sized (d))
   13846              :                       {
   13847            0 :                         gcc_assert (DECL_HAS_VALUE_EXPR_P (d));
   13848            0 :                         d = DECL_VALUE_EXPR (d);
   13849            0 :                         gcc_assert (TREE_CODE (d) == INDIRECT_REF);
   13850            0 :                         d = TREE_OPERAND (d, 0);
   13851            0 :                         gcc_assert (DECL_P (d));
   13852              :                       }
   13853          268 :                     splay_tree_key key
   13854          268 :                       = (splay_tree_key) &DECL_CONTEXT (d);
   13855          268 :                     tree nd = (tree) splay_tree_lookup (ctx->field_map,
   13856          268 :                                                         key)->value;
   13857          268 :                     if (d == ovar)
   13858              :                       var = nd;
   13859              :                     else
   13860            0 :                       var = build_fold_indirect_ref (nd);
   13861              :                   }
   13862              :                 else
   13863        45550 :                   var = lookup_decl_in_outer_ctx (ovar, ctx);
   13864              :               }
   13865        45818 :             if (nc
   13866        45818 :                 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13867        34315 :                 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
   13868        34104 :                     || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
   13869          265 :                 && is_omp_target (stmt))
   13870              :               {
   13871          241 :                 x = build_sender_ref (c, ctx);
   13872          241 :                 gimplify_assign (x, build_fold_addr_expr (var), &ilist);
   13873              :               }
   13874        82629 :             else if (nc)
   13875              :               {
   13876        45577 :                 x = build_sender_ref (nc, ctx);
   13877              : 
   13878        45577 :                 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13879        34074 :                     && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
   13880         5162 :                     && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
   13881        50739 :                     && TREE_CODE (TREE_TYPE (ovar)) == ARRAY_TYPE)
   13882              :                   {
   13883          425 :                     gcc_assert (offloaded);
   13884          425 :                     tree avar = build_fold_addr_expr (var);
   13885          425 :                     if (!OMP_CLAUSE_ITERATORS (c))
   13886              :                       {
   13887          425 :                         tree tmp = create_tmp_var (TREE_TYPE (TREE_TYPE (x)));
   13888          425 :                         mark_addressable (tmp);
   13889          425 :                         gimplify_assign (tmp, avar, &ilist);
   13890          425 :                         avar = tmp;
   13891              :                       }
   13892          425 :                     talign = TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (x)));
   13893          425 :                     avar = build_fold_addr_expr (avar);
   13894          425 :                     avar = lower_omp_map_iterator_expr (avar, c, stmt);
   13895          425 :                     gimplify_assign (x, avar, &ilist);
   13896              :                   }
   13897        45152 :                 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
   13898              :                   {
   13899         3896 :                     gcc_assert (is_gimple_omp_oacc (ctx->stmt));
   13900         3896 :                     if (!omp_privatize_by_reference (var))
   13901              :                       {
   13902         3441 :                         if (is_gimple_reg (var)
   13903         3441 :                             && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
   13904         2578 :                           suppress_warning (var);
   13905         3441 :                         var = build_fold_addr_expr (var);
   13906              :                       }
   13907              :                     else
   13908          455 :                       talign = TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (ovar)));
   13909         3896 :                     gimplify_assign (x, var, &ilist);
   13910              :                   }
   13911        41256 :                 else if (is_gimple_reg (var))
   13912              :                   {
   13913         5212 :                     gcc_assert (offloaded);
   13914         5212 :                     tree avar = create_tmp_var (TREE_TYPE (var));
   13915         5212 :                     mark_addressable (avar);
   13916         5212 :                     enum gomp_map_kind map_kind = OMP_CLAUSE_MAP_KIND (c);
   13917         5212 :                     if (GOMP_MAP_COPY_TO_P (map_kind)
   13918              :                         || map_kind == GOMP_MAP_POINTER
   13919          463 :                         || map_kind == GOMP_MAP_TO_PSET
   13920            2 :                         || map_kind == GOMP_MAP_FORCE_DEVICEPTR)
   13921              :                       {
   13922              :                         /* If we need to initialize a temporary
   13923              :                            with VAR because it is not addressable, and
   13924              :                            the variable hasn't been initialized yet, then
   13925              :                            we'll get a warning for the store to avar.
   13926              :                            Don't warn in that case, the mapping might
   13927              :                            be implicit.  */
   13928         5210 :                         suppress_warning (var, OPT_Wuninitialized);
   13929         5210 :                         gimplify_assign (avar, var, &ilist);
   13930              :                       }
   13931         5212 :                     avar = build_fold_addr_expr (avar);
   13932         5212 :                     gimplify_assign (x, avar, &ilist);
   13933         5212 :                     if ((GOMP_MAP_COPY_FROM_P (map_kind)
   13934         1395 :                          || map_kind == GOMP_MAP_FORCE_DEVICEPTR)
   13935         9029 :                         && !TYPE_READONLY (TREE_TYPE (var)))
   13936              :                       {
   13937         3817 :                         x = unshare_expr (x);
   13938         3817 :                         x = build_simple_mem_ref (x);
   13939         3817 :                         gimplify_assign (var, x, &olist);
   13940              :                       }
   13941              :                   }
   13942              :                 else
   13943              :                   {
   13944              :                     /* While MAP is handled explicitly by the FE,
   13945              :                        for 'target update', only the identified is passed.  */
   13946        36044 :                     if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM
   13947        30291 :                          || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO)
   13948        37898 :                         && (omp_is_allocatable_or_ptr (var)
   13949          120 :                             && omp_check_optional_argument (var, false)))
   13950            0 :                       var = build_fold_indirect_ref (var);
   13951        36044 :                     else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FROM
   13952        30291 :                               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TO)
   13953        37898 :                              || (!omp_is_allocatable_or_ptr (var)
   13954         7487 :                                  && !omp_check_optional_argument (var, false)))
   13955        35924 :                       var = build_fold_addr_expr (var);
   13956        36044 :                     gimplify_assign (x, var, &ilist);
   13957              :                   }
   13958              :               }
   13959        82870 :             s = NULL_TREE;
   13960        82870 :             if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
   13961              :               {
   13962         3896 :                 gcc_checking_assert (is_gimple_omp_oacc (ctx->stmt));
   13963         3896 :                 s = TREE_TYPE (ovar);
   13964         3896 :                 if (TREE_CODE (s) == REFERENCE_TYPE
   13965         3896 :                     || omp_check_optional_argument (ovar, false))
   13966          449 :                   s = TREE_TYPE (s);
   13967         3896 :                 s = TYPE_SIZE_UNIT (s);
   13968              :               }
   13969              :             else
   13970        78974 :               s = OMP_CLAUSE_SIZE (c);
   13971        82870 :             if (s == NULL_TREE)
   13972          161 :               s = TYPE_SIZE_UNIT (TREE_TYPE (ovar));
   13973        82870 :             s = fold_convert (size_type_node, s);
   13974        82870 :             s = lower_omp_map_iterator_size (s, c, stmt);
   13975        82870 :             purpose = size_int (map_idx++);
   13976        82870 :             CONSTRUCTOR_APPEND_ELT (vsize, purpose, s);
   13977        82870 :             if (TREE_CODE (s) != INTEGER_CST)
   13978        16170 :               TREE_STATIC (TREE_VEC_ELT (t, 1)) = 0;
   13979              : 
   13980        82870 :             unsigned HOST_WIDE_INT tkind, tkind_zero;
   13981        82870 :             switch (OMP_CLAUSE_CODE (c))
   13982              :               {
   13983        69755 :               case OMP_CLAUSE_MAP:
   13984        69755 :                 tkind = OMP_CLAUSE_MAP_KIND (c);
   13985        69755 :                 tkind_zero = tkind;
   13986        69755 :                 if (OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c))
   13987         5378 :                   switch (tkind)
   13988              :                     {
   13989              :                     case GOMP_MAP_ALLOC:
   13990              :                     case GOMP_MAP_IF_PRESENT:
   13991              :                     case GOMP_MAP_TO:
   13992              :                     case GOMP_MAP_FROM:
   13993              :                     case GOMP_MAP_TOFROM:
   13994              :                     case GOMP_MAP_ALWAYS_TO:
   13995              :                     case GOMP_MAP_ALWAYS_FROM:
   13996              :                     case GOMP_MAP_ALWAYS_TOFROM:
   13997              :                     case GOMP_MAP_ALWAYS_PRESENT_TO:
   13998              :                     case GOMP_MAP_ALWAYS_PRESENT_FROM:
   13999              :                     case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
   14000              :                     case GOMP_MAP_RELEASE:
   14001              :                     case GOMP_MAP_FORCE_TO:
   14002              :                     case GOMP_MAP_FORCE_FROM:
   14003              :                     case GOMP_MAP_FORCE_TOFROM:
   14004              :                     case GOMP_MAP_FORCE_PRESENT:
   14005        69755 :                       tkind_zero = GOMP_MAP_ZERO_LEN_ARRAY_SECTION;
   14006              :                       break;
   14007           21 :                     case GOMP_MAP_DELETE:
   14008           21 :                       tkind_zero = GOMP_MAP_DELETE_ZERO_LEN_ARRAY_SECTION;
   14009              :                     default:
   14010              :                       break;
   14011              :                     }
   14012        69755 :                 if (tkind_zero != tkind)
   14013              :                   {
   14014         5378 :                     if (integer_zerop (s))
   14015              :                       tkind = tkind_zero;
   14016         4646 :                     else if (integer_nonzerop (s))
   14017        69755 :                       tkind_zero = tkind;
   14018              :                   }
   14019        69755 :                 if (tkind_zero == tkind
   14020        68292 :                     && OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (c)
   14021        69755 :                     && (((tkind & GOMP_MAP_FLAG_SPECIAL_BITS)
   14022         5186 :                          & ~GOMP_MAP_IMPLICIT)
   14023              :                         == 0))
   14024              :                   {
   14025              :                     /* If this is an implicit map, and the GOMP_MAP_IMPLICIT
   14026              :                        bits are not interfered by other special bit encodings,
   14027              :                        then turn the GOMP_IMPLICIT_BIT flag on for the runtime
   14028              :                        to see.  */
   14029         5143 :                     tkind |= GOMP_MAP_IMPLICIT;
   14030         5143 :                     tkind_zero = tkind;
   14031              :                   }
   14032              :                 break;
   14033         3896 :               case OMP_CLAUSE_FIRSTPRIVATE:
   14034         3896 :                 gcc_checking_assert (is_gimple_omp_oacc (ctx->stmt));
   14035              :                 tkind = GOMP_MAP_TO;
   14036              :                 tkind_zero = tkind;
   14037              :                 break;
   14038         2563 :               case OMP_CLAUSE_TO:
   14039         2563 :                 tkind
   14040         2563 :                   = (OMP_CLAUSE_MOTION_PRESENT (c)
   14041         2563 :                      ? GOMP_MAP_ALWAYS_PRESENT_TO : GOMP_MAP_TO);
   14042              :                 tkind_zero = tkind;
   14043              :                 break;
   14044         6656 :               case OMP_CLAUSE_FROM:
   14045         6656 :                 tkind
   14046         6656 :                   = (OMP_CLAUSE_MOTION_PRESENT (c)
   14047         6656 :                      ? GOMP_MAP_ALWAYS_PRESENT_FROM : GOMP_MAP_FROM);
   14048              :                 tkind_zero = tkind;
   14049              :                 break;
   14050            0 :               default:
   14051            0 :                 gcc_unreachable ();
   14052              :               }
   14053        69755 :             gcc_checking_assert (tkind
   14054              :                                  < (HOST_WIDE_INT_C (1U) << talign_shift));
   14055        82870 :             gcc_checking_assert (tkind_zero
   14056              :                                  < (HOST_WIDE_INT_C (1U) << talign_shift));
   14057        82870 :             talign = ceil_log2 (talign);
   14058        82870 :             tkind |= talign << talign_shift;
   14059        82870 :             tkind_zero |= talign << talign_shift;
   14060        82870 :             gcc_checking_assert (tkind
   14061              :                                  <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
   14062        82870 :             gcc_checking_assert (tkind_zero
   14063              :                                  <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
   14064        82870 :             if (tkind == tkind_zero)
   14065        81407 :               x = build_int_cstu (tkind_type, tkind);
   14066              :             else
   14067              :               {
   14068         1463 :                 TREE_STATIC (TREE_VEC_ELT (t, 2)) = 0;
   14069         1463 :                 x = build3 (COND_EXPR, tkind_type,
   14070              :                             fold_build2 (EQ_EXPR, boolean_type_node,
   14071              :                                          unshare_expr (s), size_zero_node),
   14072         1463 :                             build_int_cstu (tkind_type, tkind_zero),
   14073         1463 :                             build_int_cstu (tkind_type, tkind));
   14074              :               }
   14075        82870 :             CONSTRUCTOR_APPEND_ELT (vkind, purpose, x);
   14076        82870 :             if (nc && nc != c)
   14077            0 :               c = nc;
   14078              :             break;
   14079              : 
   14080        15873 :           case OMP_CLAUSE_FIRSTPRIVATE:
   14081        15873 :           omp_has_device_addr_descr:
   14082        15873 :             if (is_gimple_omp_oacc (ctx->stmt))
   14083         3896 :               goto oacc_firstprivate_map;
   14084        11977 :             ovar = OMP_CLAUSE_DECL (c);
   14085        11977 :             if (omp_privatize_by_reference (ovar))
   14086          849 :               talign = TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (ovar)));
   14087              :             else
   14088        11128 :               talign = DECL_ALIGN_UNIT (ovar);
   14089        11977 :             var = lookup_decl_in_outer_ctx (ovar, ctx);
   14090        11977 :             x = build_sender_ref (ovar, ctx);
   14091        11977 :             tkind = GOMP_MAP_FIRSTPRIVATE;
   14092        11977 :             type = TREE_TYPE (ovar);
   14093        11977 :             if (omp_privatize_by_reference (ovar))
   14094          849 :               type = TREE_TYPE (type);
   14095        11977 :             if ((INTEGRAL_TYPE_P (type)
   14096        11530 :                  && TYPE_PRECISION (type) <= POINTER_SIZE)
   14097        11987 :                 || TREE_CODE (type) == POINTER_TYPE)
   14098              :               {
   14099        11689 :                 tkind = GOMP_MAP_FIRSTPRIVATE_INT;
   14100        11689 :                 tree t = var;
   14101        11689 :                 if (omp_privatize_by_reference (var))
   14102          787 :                   t = build_simple_mem_ref (var);
   14103        10902 :                 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
   14104         9381 :                   suppress_warning (var);
   14105        11689 :                 if (TREE_CODE (type) != POINTER_TYPE)
   14106        11520 :                   t = fold_convert (pointer_sized_int_node, t);
   14107        11689 :                 t = fold_convert (TREE_TYPE (x), t);
   14108        11689 :                 gimplify_assign (x, t, &ilist);
   14109              :               }
   14110          288 :             else if (omp_privatize_by_reference (var))
   14111           62 :               gimplify_assign (x, var, &ilist);
   14112          226 :             else if (is_gimple_reg (var))
   14113              :               {
   14114           51 :                 tree avar = create_tmp_var (TREE_TYPE (var));
   14115           51 :                 mark_addressable (avar);
   14116           51 :                 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
   14117           44 :                   suppress_warning (var);
   14118           51 :                 gimplify_assign (avar, var, &ilist);
   14119           51 :                 avar = build_fold_addr_expr (avar);
   14120           51 :                 gimplify_assign (x, avar, &ilist);
   14121              :               }
   14122              :             else
   14123              :               {
   14124          175 :                 var = build_fold_addr_expr (var);
   14125          175 :                 gimplify_assign (x, var, &ilist);
   14126              :               }
   14127          288 :             if (tkind == GOMP_MAP_FIRSTPRIVATE_INT)
   14128        11689 :               s = size_int (0);
   14129          288 :             else if (omp_privatize_by_reference (ovar))
   14130           62 :               s = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ovar)));
   14131              :             else
   14132          226 :               s = TYPE_SIZE_UNIT (TREE_TYPE (ovar));
   14133        11977 :             s = fold_convert (size_type_node, s);
   14134        11977 :             purpose = size_int (map_idx++);
   14135        11977 :             CONSTRUCTOR_APPEND_ELT (vsize, purpose, s);
   14136        11977 :             if (TREE_CODE (s) != INTEGER_CST)
   14137           42 :               TREE_STATIC (TREE_VEC_ELT (t, 1)) = 0;
   14138              : 
   14139        11977 :             gcc_checking_assert (tkind
   14140              :                                  < (HOST_WIDE_INT_C (1U) << talign_shift));
   14141        11977 :             talign = ceil_log2 (talign);
   14142        11977 :             tkind |= talign << talign_shift;
   14143        11977 :             gcc_checking_assert (tkind
   14144              :                                  <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
   14145        11977 :             CONSTRUCTOR_APPEND_ELT (vkind, purpose,
   14146              :                                     build_int_cstu (tkind_type, tkind));
   14147              :             /* Fortran array descriptors: firstprivate of data + attach.  */
   14148        11977 :             if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
   14149        11958 :                 && lang_hooks.decls.omp_array_data (ovar, true)
   14150        12019 :                 && lang_hooks.decls.omp_array_data_privatize (ovar))
   14151              :               {
   14152           26 :                 tree not_null_lb, null_lb, after_lb;
   14153           26 :                 tree var1, var2, size1, size2;
   14154           26 :                 tree present = omp_check_optional_argument (ovar, true);
   14155           26 :                 if (present)
   14156              :                   {
   14157            2 :                     location_t clause_loc = OMP_CLAUSE_LOCATION (c);
   14158            2 :                     not_null_lb = create_artificial_label (clause_loc);
   14159            2 :                     null_lb = create_artificial_label (clause_loc);
   14160            2 :                     after_lb = create_artificial_label (clause_loc);
   14161            2 :                     gimple_seq seq = NULL;
   14162            2 :                     present = force_gimple_operand (present, &seq, true,
   14163              :                                                     NULL_TREE);
   14164            2 :                     gimple_seq_add_seq (&ilist, seq);
   14165            2 :                     gimple_seq_add_stmt (&ilist,
   14166            2 :                       gimple_build_cond_from_tree (present,
   14167              :                                                    not_null_lb, null_lb));
   14168            2 :                     gimple_seq_add_stmt (&ilist,
   14169            2 :                                          gimple_build_label (not_null_lb));
   14170              :                   }
   14171           26 :                 var1 = lang_hooks.decls.omp_array_data (var, false);
   14172           26 :                 size1 = lang_hooks.decls.omp_array_size (var, &ilist);
   14173           26 :                 var2 = build_fold_addr_expr (x);
   14174           26 :                 if (!POINTER_TYPE_P (TREE_TYPE (var)))
   14175            0 :                   var = build_fold_addr_expr (var);
   14176           26 :                 size2 = fold_build2 (POINTER_DIFF_EXPR, ssizetype,
   14177              :                                    build_fold_addr_expr (var1), var);
   14178           26 :                 size2 = fold_convert (sizetype, size2);
   14179           26 :                 if (present)
   14180              :                   {
   14181            2 :                     tree tmp = create_tmp_var (TREE_TYPE (var1));
   14182            2 :                     gimplify_assign (tmp, var1, &ilist);
   14183            2 :                     var1 = tmp;
   14184            2 :                     tmp = create_tmp_var (TREE_TYPE (var2));
   14185            2 :                     gimplify_assign (tmp, var2, &ilist);
   14186            2 :                     var2 = tmp;
   14187            2 :                     tmp = create_tmp_var (TREE_TYPE (size1));
   14188            2 :                     gimplify_assign (tmp, size1, &ilist);
   14189            2 :                     size1 = tmp;
   14190            2 :                     tmp = create_tmp_var (TREE_TYPE (size2));
   14191            2 :                     gimplify_assign (tmp, size2, &ilist);
   14192            2 :                     size2 = tmp;
   14193            2 :                     gimple_seq_add_stmt (&ilist, gimple_build_goto (after_lb));
   14194            2 :                     gimple_seq_add_stmt (&ilist, gimple_build_label (null_lb));
   14195            2 :                     gimplify_assign (var1, null_pointer_node, &ilist);
   14196            2 :                     gimplify_assign (var2, null_pointer_node, &ilist);
   14197            2 :                     gimplify_assign (size1, size_zero_node, &ilist);
   14198            2 :                     gimplify_assign (size2, size_zero_node, &ilist);
   14199            2 :                     gimple_seq_add_stmt (&ilist, gimple_build_label (after_lb));
   14200              :                   }
   14201           26 :                 x = build_sender_ref ((splay_tree_key) &DECL_NAME (ovar), ctx);
   14202           26 :                 gimplify_assign (x, var1, &ilist);
   14203           26 :                 tkind = GOMP_MAP_FIRSTPRIVATE;
   14204           26 :                 talign = DECL_ALIGN_UNIT (ovar);
   14205           26 :                 talign = ceil_log2 (talign);
   14206           26 :                 tkind |= talign << talign_shift;
   14207           26 :                 gcc_checking_assert (tkind
   14208              :                                      <= tree_to_uhwi (
   14209              :                                           TYPE_MAX_VALUE (tkind_type)));
   14210           26 :                 purpose = size_int (map_idx++);
   14211           26 :                 CONSTRUCTOR_APPEND_ELT (vsize, purpose, size1);
   14212           26 :                 if (TREE_CODE (size1) != INTEGER_CST)
   14213           26 :                   TREE_STATIC (TREE_VEC_ELT (t, 1)) = 0;
   14214           26 :                 CONSTRUCTOR_APPEND_ELT (vkind, purpose,
   14215              :                                         build_int_cstu (tkind_type, tkind));
   14216           26 :                 x = build_sender_ref ((splay_tree_key) &DECL_UID (ovar), ctx);
   14217           26 :                 gimplify_assign (x, var2, &ilist);
   14218           26 :                 tkind = GOMP_MAP_ATTACH;
   14219           26 :                 purpose = size_int (map_idx++);
   14220           26 :                 CONSTRUCTOR_APPEND_ELT (vsize, purpose, size2);
   14221           26 :                 CONSTRUCTOR_APPEND_ELT (vkind, purpose,
   14222              :                                         build_int_cstu (tkind_type, tkind));
   14223              :               }
   14224              :             break;
   14225              : 
   14226         2431 :           case OMP_CLAUSE_USE_DEVICE_PTR:
   14227         2431 :           case OMP_CLAUSE_USE_DEVICE_ADDR:
   14228         2431 :           case OMP_CLAUSE_HAS_DEVICE_ADDR:
   14229         2431 :           case OMP_CLAUSE_IS_DEVICE_PTR:
   14230         2431 :             ovar = OMP_CLAUSE_DECL (c);
   14231         2431 :             if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
   14232              :               {
   14233          236 :                 if (lang_hooks.decls.omp_array_data (ovar, true))
   14234           19 :                   goto omp_has_device_addr_descr;
   14235          234 :                 while (TREE_CODE (ovar) == INDIRECT_REF
   14236          234 :                        || TREE_CODE (ovar) == ARRAY_REF)
   14237           17 :                   ovar = TREE_OPERAND (ovar, 0);
   14238              :               }
   14239         2412 :             var = lookup_decl_in_outer_ctx (ovar, ctx);
   14240              : 
   14241         2412 :             if (lang_hooks.decls.omp_array_data (ovar, true))
   14242              :               {
   14243          601 :                 tkind = ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR
   14244          601 :                           && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
   14245          601 :                          ? GOMP_MAP_USE_DEVICE_PTR : GOMP_MAP_FIRSTPRIVATE_INT);
   14246          601 :                 x = build_sender_ref ((splay_tree_key) &DECL_NAME (ovar), ctx);
   14247              :               }
   14248         1811 :             else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR
   14249         1811 :                      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
   14250              :               {
   14251         1453 :                 tkind = GOMP_MAP_USE_DEVICE_PTR;
   14252         1453 :                 x = build_sender_ref ((splay_tree_key) &DECL_UID (ovar), ctx);
   14253              :               }
   14254              :             else
   14255              :               {
   14256          358 :                 tkind = GOMP_MAP_FIRSTPRIVATE_INT;
   14257          358 :                 x = build_sender_ref (ovar, ctx);
   14258              :               }
   14259              : 
   14260         2412 :             if (is_gimple_omp_oacc (ctx->stmt))
   14261              :               {
   14262          147 :                 gcc_assert (tkind == GOMP_MAP_USE_DEVICE_PTR);
   14263              : 
   14264          147 :                 if (OMP_CLAUSE_USE_DEVICE_PTR_IF_PRESENT (c))
   14265           57 :                   tkind = GOMP_MAP_USE_DEVICE_PTR_IF_PRESENT;
   14266              :               }
   14267              : 
   14268         2412 :             type = TREE_TYPE (ovar);
   14269         2412 :             if (lang_hooks.decls.omp_array_data (ovar, true))
   14270          601 :               var = lang_hooks.decls.omp_array_data (var, false);
   14271         1811 :             else if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
   14272          543 :                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
   14273         1485 :                       && !omp_privatize_by_reference (ovar)
   14274          512 :                       && !omp_is_allocatable_or_ptr (ovar))
   14275         3249 :                      || TREE_CODE (type) == ARRAY_TYPE)
   14276          401 :               var = build_fold_addr_expr (var);
   14277              :             else
   14278              :               {
   14279         1410 :                 if (omp_privatize_by_reference (ovar)
   14280          370 :                     || omp_check_optional_argument (ovar, false)
   14281         1780 :                     || omp_is_allocatable_or_ptr (ovar))
   14282              :                   {
   14283         1213 :                     type = TREE_TYPE (type);
   14284         1213 :                     if (POINTER_TYPE_P (type)
   14285         1213 :                         && TREE_CODE (type) != ARRAY_TYPE
   14286         1572 :                         && ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_USE_DEVICE_ADDR
   14287           36 :                             && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
   14288           28 :                             && !omp_is_allocatable_or_ptr (ovar))
   14289          337 :                            || (omp_privatize_by_reference (ovar)
   14290          337 :                                && omp_is_allocatable_or_ptr (ovar))))
   14291          357 :                       var = build_simple_mem_ref (var);
   14292         1213 :                     var = fold_convert (TREE_TYPE (x), var);
   14293              :                   }
   14294              :               }
   14295         2412 :             tree present;
   14296         2412 :             present = omp_check_optional_argument (ovar, true);
   14297         2412 :             if (present)
   14298              :               {
   14299          878 :                 tree null_label = create_artificial_label (UNKNOWN_LOCATION);
   14300          878 :                 tree notnull_label = create_artificial_label (UNKNOWN_LOCATION);
   14301          878 :                 tree opt_arg_label = create_artificial_label (UNKNOWN_LOCATION);
   14302          878 :                 tree new_x = unshare_expr (x);
   14303          878 :                 gimplify_expr (&present, &ilist, NULL, is_gimple_val,
   14304              :                                fb_rvalue);
   14305          878 :                 gcond *cond = gimple_build_cond_from_tree (present,
   14306              :                                                            notnull_label,
   14307              :                                                            null_label);
   14308          878 :                 gimple_seq_add_stmt (&ilist, cond);
   14309          878 :                 gimple_seq_add_stmt (&ilist, gimple_build_label (null_label));
   14310          878 :                 gimplify_assign (new_x, null_pointer_node, &ilist);
   14311          878 :                 gimple_seq_add_stmt (&ilist, gimple_build_goto (opt_arg_label));
   14312          878 :                 gimple_seq_add_stmt (&ilist,
   14313          878 :                                      gimple_build_label (notnull_label));
   14314          878 :                 gimplify_assign (x, var, &ilist);
   14315          878 :                 gimple_seq_add_stmt (&ilist,
   14316          878 :                                      gimple_build_label (opt_arg_label));
   14317              :               }
   14318              :             else
   14319         1534 :               gimplify_assign (x, var, &ilist);
   14320         2412 :             s = size_int (0);
   14321         2412 :             purpose = size_int (map_idx++);
   14322         2412 :             CONSTRUCTOR_APPEND_ELT (vsize, purpose, s);
   14323         2412 :             gcc_checking_assert (tkind
   14324              :                                  < (HOST_WIDE_INT_C (1U) << talign_shift));
   14325         2412 :             gcc_checking_assert (tkind
   14326              :                                  <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
   14327         2412 :             CONSTRUCTOR_APPEND_ELT (vkind, purpose,
   14328              :                                     build_int_cstu (tkind_type, tkind));
   14329         2412 :             break;
   14330              : 
   14331           86 :           case OMP_CLAUSE_USES_ALLOCATORS:
   14332           86 :             tree allocator = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
   14333           86 :             tree memspace = OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c);
   14334           86 :             tree traits = OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c);
   14335              : 
   14336           86 :             tree ntraits, traits_var;
   14337           86 :             if (traits == NULL_TREE)
   14338              :               {
   14339           29 :                 ntraits = integer_zero_node;
   14340           29 :                 traits_var = null_pointer_node;
   14341              :               }
   14342              :             else
   14343              :               {
   14344           57 :                 location_t loc = OMP_CLAUSE_LOCATION (c);
   14345           57 :                 ntraits = array_type_nelts_top (TREE_TYPE (traits));
   14346           57 :                 traits_var = build_fold_addr_expr_loc (loc, traits);
   14347              :               }
   14348              : 
   14349           86 :             if (memspace == NULL_TREE)
   14350           61 :               memspace = build_int_cst (pointer_sized_int_node, 0);
   14351              :             else
   14352           25 :               memspace = fold_convert (pointer_sized_int_node, memspace);
   14353              : 
   14354           86 :             tree arr_type
   14355           86 :               = build_array_type_nelts (pointer_sized_int_node, 3);
   14356           86 :             tree uses_allocators_descr
   14357           86 :               = create_tmp_var (arr_type, "uses_allocator_descr");
   14358           86 :             tree ua_descr[3];
   14359          430 :             for (int i = 0; i < 3; i++)
   14360          258 :               ua_descr[i] = build4 (ARRAY_REF, pointer_sized_int_node,
   14361              :                                     uses_allocators_descr,
   14362          258 :                                     build_int_cst (size_type_node, i),
   14363              :                                     NULL_TREE, NULL_TREE);
   14364           86 :             gimplify_assign (ua_descr[0],
   14365              :                              fold_convert (pointer_sized_int_node, memspace),
   14366              :                              &ilist);
   14367           86 :             gimplify_assign (ua_descr[1],
   14368              :                              fold_convert (pointer_sized_int_node, ntraits),
   14369              :                              &ilist);
   14370           86 :             gimplify_assign (ua_descr[2],
   14371              :                              fold_convert (pointer_sized_int_node, traits_var),
   14372              :                              &ilist);
   14373              : 
   14374           86 :             x = build_sender_ref (allocator, ctx);
   14375           86 :             tree ptr = build_fold_addr_expr (uses_allocators_descr);
   14376           86 :             gimplify_assign (x, fold_convert (TREE_TYPE (x), ptr), &ilist);
   14377              : 
   14378           86 :             s = size_int (0);
   14379           86 :             purpose = size_int (map_idx++);
   14380           86 :             CONSTRUCTOR_APPEND_ELT (vsize, purpose, s);
   14381           86 :             tkind = GOMP_MAP_USES_ALLOCATORS;
   14382           86 :             gcc_checking_assert (tkind
   14383              :                                  < (HOST_WIDE_INT_C (1U) << talign_shift));
   14384           86 :             talign = TYPE_ALIGN_UNIT (pointer_sized_int_node);
   14385           86 :             talign = ceil_log2 (talign);
   14386           86 :             tkind |= talign << talign_shift;
   14387           86 :             gcc_checking_assert (tkind
   14388              :                                  <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
   14389           86 :             CONSTRUCTOR_APPEND_ELT (vkind, purpose,
   14390              :                                     build_int_cstu (tkind_type, tkind));
   14391           86 :             break;
   14392              :           }
   14393              : 
   14394        33566 :       gcc_assert (map_idx == map_cnt);
   14395              : 
   14396        33566 :       if (!deep_map_cnt)
   14397              :         {
   14398        33421 :           DECL_INITIAL (TREE_VEC_ELT (t, 1))
   14399        33421 :             = build_constructor (TREE_TYPE (TREE_VEC_ELT (t, 1)), vsize);
   14400        33421 :           DECL_INITIAL (TREE_VEC_ELT (t, 2))
   14401        66842 :             = build_constructor (TREE_TYPE (TREE_VEC_ELT (t, 2)), vkind);
   14402              :         }
   14403       100698 :       for (int i = 1; i <= 2; i++)
   14404        67132 :         if (deep_map_cnt || !TREE_STATIC (TREE_VEC_ELT (t, i)))
   14405              :           {
   14406         9675 :             tree tmp = TREE_VEC_ELT (t, i);
   14407         9675 :             if (deep_map_cnt)
   14408              :               {
   14409          290 :                 const char *prefix = (i == 1 ? ".omp_data_sizes0"
   14410              :                                              : ".omp_data_kinds0");
   14411          145 :                 tree type = (i == 1) ? size_type_node : tkind_type;
   14412          290 :                 type = build_array_type_nelts (type, map_cnt);
   14413          290 :                 tree var = create_tmp_var (type, prefix);
   14414          290 :                 DECL_NAMELESS (var) = 1;
   14415          290 :                 TREE_ADDRESSABLE (var) = 1;
   14416          290 :                 TREE_STATIC (var) = TREE_STATIC (tmp);
   14417          290 :                 DECL_INITIAL (var) = build_constructor (type, i == 1
   14418              :                                                               ? vsize : vkind);
   14419          290 :                 tmp = var;
   14420          290 :                 TREE_STATIC (TREE_VEC_ELT (t, i)) = 0;
   14421              :               }
   14422              : 
   14423         9675 :             gimple_seq initlist = NULL;
   14424         9675 :             force_gimple_operand (build1 (DECL_EXPR, void_type_node, tmp),
   14425              :                                   &initlist, true, NULL_TREE);
   14426         9675 :             gimple_seq_add_seq (&ilist, initlist);
   14427              : 
   14428         9675 :             if (deep_map_cnt)
   14429              :               {
   14430          290 :                 tree tmp2;
   14431          290 :                 tree call = builtin_decl_explicit (BUILT_IN_MEMCPY);
   14432          290 :                 tmp2 = TYPE_SIZE_UNIT (TREE_TYPE (tmp));
   14433          580 :                 call = build_call_expr_loc (input_location, call, 3,
   14434          290 :                                             TREE_VEC_ELT (t, i),
   14435              :                                             build_fold_addr_expr (tmp), tmp2);
   14436          290 :                 gimplify_and_add (call, &ilist);
   14437              :               }
   14438              : 
   14439         9675 :             if (!TREE_STATIC (tmp))
   14440              :               {
   14441         9456 :                 tree clobber = build_clobber (TREE_TYPE (tmp));
   14442         9456 :                 gimple_seq_add_stmt (&olist,
   14443         9456 :                                      gimple_build_assign (tmp, clobber));
   14444              :               }
   14445         9675 :             if (deep_map_cnt)
   14446              :               {
   14447          290 :                 tmp = TREE_VEC_ELT (t, i);
   14448          290 :                 tree call = builtin_decl_explicit (BUILT_IN_FREE);
   14449          290 :                 call = build_call_expr_loc (input_location, call, 1, tmp);
   14450          290 :                 gimplify_and_add (call, &olist);
   14451          290 :                 tree clobber = build_clobber (TREE_TYPE (tmp));
   14452          290 :                 gimple_seq_add_stmt (&olist,
   14453          290 :                                      gimple_build_assign (tmp, clobber));
   14454              :               }
   14455              :           }
   14456        57457 :         else if (omp_maybe_offloaded_ctx (ctx->outer))
   14457              :           {
   14458         3296 :             tree id = get_identifier ("omp declare target");
   14459         3296 :             tree decl = TREE_VEC_ELT (t, i);
   14460         3296 :             DECL_ATTRIBUTES (decl)
   14461         3296 :               = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
   14462         3296 :             varpool_node *node = varpool_node::get (decl);
   14463         3296 :             if (node)
   14464              :               {
   14465            0 :                 node->offloadable = 1;
   14466            0 :                 if (ENABLE_OFFLOADING)
   14467              :                   {
   14468              :                     g->have_offload = true;
   14469              :                     vec_safe_push (offload_vars, t);
   14470              :                   }
   14471              :               }
   14472              :           }
   14473              : 
   14474        33566 :       if (deep_map_cnt)
   14475              :         {
   14476          145 :           tree call = builtin_decl_explicit (BUILT_IN_FREE);
   14477          145 :           call = build_call_expr_loc (input_location, call, 1,
   14478          145 :                                       TREE_VEC_ELT (t, 0));
   14479          145 :           gimplify_and_add (call, &olist);
   14480              : 
   14481          145 :           gimplify_expr (&TREE_VEC_ELT (t, 1), &ilist, NULL, is_gimple_val,
   14482              :                          fb_rvalue);
   14483              :         }
   14484              : 
   14485        33566 :       tree clobber = build_clobber (TREE_TYPE (ctx->sender_decl));
   14486        33566 :       gimple_seq_add_stmt (&olist, gimple_build_assign (ctx->sender_decl,
   14487              :                                                         clobber));
   14488              :     }
   14489              : 
   14490              :   /* Once all the expansions are done, sequence all the different
   14491              :      fragments inside gimple_omp_body.  */
   14492              : 
   14493        38655 :   new_body = NULL;
   14494              : 
   14495        38655 :   if (offloaded
   14496        22276 :       && ctx->record_type)
   14497              :     {
   14498        17420 :       t = ctx->sender_decl;
   14499        17420 :       if (!deep_map_cnt)
   14500        17312 :         t = build_fold_addr_expr_loc (loc, t);
   14501              :       /* fixup_child_record_type might have changed receiver_decl's type.  */
   14502        17420 :       t = fold_convert_loc (loc, TREE_TYPE (ctx->receiver_decl), t);
   14503        17420 :       if (!AGGREGATE_TYPE_P (TREE_TYPE (ctx->sender_decl)))
   14504          108 :         gimplify_assign (ctx->receiver_decl, t, &new_body);
   14505              :       else
   14506        17312 :         gimple_seq_add_stmt (&new_body,
   14507        17312 :                              gimple_build_assign (ctx->receiver_decl, t));
   14508              :     }
   14509        38655 :   gimple_seq_add_seq (&new_body, fplist);
   14510              : 
   14511        38655 :   if (offloaded || data_region)
   14512              :     {
   14513       147079 :       tree prev = NULL_TREE;
   14514              :       bool by_ref;
   14515       147079 :       for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
   14516       120779 :         switch (OMP_CLAUSE_CODE (c))
   14517              :           {
   14518              :             tree var, x, new_var, *allocate_ptr;
   14519              :           default:
   14520              :             break;
   14521        15873 :           case OMP_CLAUSE_FIRSTPRIVATE:
   14522        15873 :           omp_firstprivatize_data_region:
   14523        15873 :             if (is_gimple_omp_oacc (ctx->stmt))
   14524              :               break;
   14525        11977 :             var = OMP_CLAUSE_DECL (c);
   14526        11977 :             new_var = lookup_decl (var, ctx);
   14527        11977 :             allocate_ptr = alloc_map.get (new_var);
   14528        11977 :             by_ref = omp_privatize_by_reference (var);
   14529        11977 :             if (allocate_ptr)
   14530              :               {
   14531           68 :                 if (is_variable_sized (var, by_ref))
   14532              :                   /* Handle this in the next pass when the size is
   14533              :                      available.  */
   14534              :                   break;
   14535              : 
   14536           53 :                 gimple_seq *allocate_seq = alloc_seq_map.get (new_var);
   14537           53 :                 gcc_assert (allocate_seq);
   14538           53 :                 gimple_seq_add_seq (&new_body, *allocate_seq);
   14539              : 
   14540           53 :                 if (by_ref)
   14541              :                   {
   14542            4 :                     x = fold_convert (TREE_TYPE (new_var), *allocate_ptr);
   14543            4 :                     gimplify_assign (new_var, x, &new_body);
   14544            4 :                     new_var = build_fold_indirect_ref (new_var);
   14545              :                   }
   14546              :                 else
   14547           49 :                   new_var = build_simple_mem_ref (*allocate_ptr);
   14548              :               }
   14549        11962 :             if (by_ref || is_gimple_reg_type (TREE_TYPE (var)))
   14550              :               {
   14551        11801 :                 tree type;
   14552        11801 :                 type = TREE_TYPE (var);
   14553        11801 :                 if (by_ref)
   14554          836 :                   type = TREE_TYPE (type);
   14555        11801 :                 if ((INTEGRAL_TYPE_P (type)
   14556        11530 :                      && TYPE_PRECISION (type) <= POINTER_SIZE)
   14557        11811 :                     || TREE_CODE (type) == POINTER_TYPE)
   14558              :                   {
   14559        11689 :                     x = build_receiver_ref (var, false, ctx);
   14560        11689 :                     if (TREE_CODE (type) != POINTER_TYPE)
   14561        11520 :                       x = fold_convert (pointer_sized_int_node, x);
   14562        11689 :                     x = fold_convert (type, x);
   14563        11689 :                     gimplify_expr (&x, &new_body, NULL, is_gimple_val,
   14564              :                                    fb_rvalue);
   14565        11689 :                     if (by_ref && !allocate_ptr)
   14566              :                       {
   14567          786 :                         tree v = create_tmp_var_raw (type, get_name (var));
   14568          786 :                         gimple_add_tmp_var (v);
   14569          786 :                         TREE_ADDRESSABLE (v) = 1;
   14570          786 :                         gimple_seq_add_stmt (&new_body,
   14571          786 :                                              gimple_build_assign (v, x));
   14572          786 :                         x = build_fold_addr_expr (v);
   14573              :                       }
   14574        11689 :                     gimplify_assign (new_var, x, &new_body);
   14575              :                   }
   14576              :                 else
   14577              :                   {
   14578          112 :                     x = build_receiver_ref (var, allocate_ptr || !by_ref, ctx);
   14579          112 :                     gimplify_expr (&x, &new_body, NULL, is_gimple_val,
   14580              :                                    fb_rvalue);
   14581          112 :                     gimplify_assign (new_var, x, &new_body);
   14582              :                   }
   14583              :               }
   14584          161 :             else if (is_variable_sized (var))
   14585              :               {
   14586            8 :                 tree pvar = DECL_VALUE_EXPR (var);
   14587            8 :                 gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
   14588            8 :                 pvar = TREE_OPERAND (pvar, 0);
   14589            8 :                 gcc_assert (DECL_P (pvar));
   14590            8 :                 tree new_var = lookup_decl (pvar, ctx);
   14591            8 :                 x = build_receiver_ref (var, false, ctx);
   14592            8 :                 gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
   14593            8 :                 gimple_seq_add_stmt (&new_body,
   14594            8 :                                      gimple_build_assign (new_var, x));
   14595              :               }
   14596          153 :             else if (allocate_ptr)
   14597              :               {
   14598           22 :                 x = build_receiver_ref (var, true, ctx);
   14599           22 :                 new_var = unshare_expr (new_var);
   14600           22 :                 x = lang_hooks.decls.omp_clause_copy_ctor (c, new_var, x);
   14601           22 :                 gimplify_and_add (x, &new_body);
   14602              :               }
   14603              :             break;
   14604          228 :           case OMP_CLAUSE_PRIVATE:
   14605          228 :             if (is_gimple_omp_oacc (ctx->stmt))
   14606              :               break;
   14607          148 :             var = OMP_CLAUSE_DECL (c);
   14608          148 :             new_var = lookup_decl (var, ctx);
   14609          148 :             allocate_ptr = alloc_map.get (new_var);
   14610          148 :             by_ref = omp_privatize_by_reference (var);
   14611          148 :             if (allocate_ptr)
   14612              :               {
   14613           57 :                 if (is_variable_sized (var, by_ref))
   14614              :                   /* Handle this in the next pass when the size is
   14615              :                      available.  */
   14616              :                   break;
   14617              : 
   14618           45 :                 gimple_seq *allocate_seq = alloc_seq_map.get (new_var);
   14619           45 :                 gcc_assert (allocate_seq);
   14620           45 :                 gimple_seq_add_seq (&new_body, *allocate_seq);
   14621              : 
   14622           45 :                 if (!by_ref)
   14623           44 :                   new_var = build_simple_mem_ref (*allocate_ptr);
   14624              :               }
   14625          135 :             if (by_ref)
   14626              :               {
   14627           12 :                 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
   14628           12 :                 if (!allocate_ptr)
   14629              :                   {
   14630           11 :                     x = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var)));
   14631           11 :                     if (TREE_CONSTANT (x))
   14632              :                       {
   14633            3 :                         x = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_var)),
   14634              :                                                 get_name (var));
   14635            3 :                         gimple_add_tmp_var (x);
   14636            3 :                         TREE_ADDRESSABLE (x) = 1;
   14637            3 :                         x = build_fold_addr_expr_loc (clause_loc, x);
   14638              :                       }
   14639              :                     else
   14640              :                       break;
   14641              : 
   14642            3 :                     x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
   14643            3 :                     gimplify_expr (&x, &new_body, NULL, is_gimple_val,
   14644              :                                    fb_rvalue);
   14645              :                   }
   14646              :                 else
   14647            1 :                   x = *allocate_ptr;
   14648              : 
   14649            4 :                 gimple_seq_add_stmt (&new_body,
   14650            4 :                                      gimple_build_assign (new_var, x));
   14651              :               }
   14652              :             break;
   14653         2431 :           case OMP_CLAUSE_USE_DEVICE_PTR:
   14654         2431 :           case OMP_CLAUSE_USE_DEVICE_ADDR:
   14655         2431 :           case OMP_CLAUSE_HAS_DEVICE_ADDR:
   14656         2431 :           case OMP_CLAUSE_IS_DEVICE_PTR:
   14657         2431 :             gimple_seq assign_body;
   14658         2431 :             bool is_array_data;
   14659         2431 :             bool do_optional_check;
   14660         2431 :             assign_body = NULL;
   14661         2431 :             do_optional_check = false;
   14662         2431 :             var = OMP_CLAUSE_DECL (c);
   14663         2431 :             is_array_data = lang_hooks.decls.omp_array_data (var, true) != NULL;
   14664         2431 :             if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR && is_array_data)
   14665           19 :               goto omp_firstprivatize_data_region;
   14666              : 
   14667         2412 :             if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR
   14668         2412 :                 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
   14669         4108 :               x = build_sender_ref (is_array_data
   14670          601 :                                     ? (splay_tree_key) &DECL_NAME (var)
   14671         1453 :                                     : (splay_tree_key) &DECL_UID (var), ctx);
   14672              :             else
   14673              :               {
   14674          358 :                 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
   14675              :                   {
   14676          234 :                     while (TREE_CODE (var) == INDIRECT_REF
   14677          234 :                            || TREE_CODE (var) == ARRAY_REF)
   14678           17 :                       var = TREE_OPERAND (var, 0);
   14679              :                   }
   14680          358 :                 x = build_receiver_ref (var, false, ctx);
   14681              :               }
   14682              : 
   14683         2412 :             if (is_array_data)
   14684              :               {
   14685          601 :                 bool is_ref = omp_privatize_by_reference (var);
   14686          601 :                 do_optional_check = true;
   14687              :                 /* First, we copy the descriptor data from the host; then
   14688              :                    we update its data to point to the target address.  */
   14689          601 :                 new_var = lookup_decl (var, ctx);
   14690          601 :                 new_var = DECL_VALUE_EXPR (new_var);
   14691          601 :                 tree v = new_var;
   14692          601 :                 tree v2 = var;
   14693          601 :                 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
   14694          601 :                     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR)
   14695          601 :                   v2 = maybe_lookup_decl_in_outer_ctx (var, ctx);
   14696              : 
   14697          601 :                 if (is_ref)
   14698              :                   {
   14699          396 :                     v2 = build_fold_indirect_ref (v2);
   14700          396 :                     v = create_tmp_var_raw (TREE_TYPE (v2), get_name (var));
   14701          396 :                     gimple_add_tmp_var (v);
   14702          396 :                     TREE_ADDRESSABLE (v) = 1;
   14703          396 :                     gimplify_assign (v, v2, &assign_body);
   14704          396 :                     tree rhs = build_fold_addr_expr (v);
   14705          396 :                     gimple_seq_add_stmt (&assign_body,
   14706          396 :                                          gimple_build_assign (new_var, rhs));
   14707              :                   }
   14708              :                 else
   14709          205 :                   gimplify_assign (new_var, v2, &assign_body);
   14710              : 
   14711          601 :                 v2 = lang_hooks.decls.omp_array_data (unshare_expr (v), false);
   14712          601 :                 gcc_assert (v2);
   14713          601 :                 gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
   14714         1202 :                 gimple_seq_add_stmt (&assign_body,
   14715          601 :                                      gimple_build_assign (v2, x));
   14716              :               }
   14717         1811 :             else if (is_variable_sized (var))
   14718              :               {
   14719           12 :                 tree pvar = DECL_VALUE_EXPR (var);
   14720           12 :                 gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
   14721           12 :                 pvar = TREE_OPERAND (pvar, 0);
   14722           12 :                 gcc_assert (DECL_P (pvar));
   14723           12 :                 new_var = lookup_decl (pvar, ctx);
   14724           12 :                 gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
   14725           24 :                 gimple_seq_add_stmt (&assign_body,
   14726           12 :                                      gimple_build_assign (new_var, x));
   14727              :               }
   14728         1799 :             else if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
   14729          537 :                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
   14730         1477 :                       && !omp_privatize_by_reference (var)
   14731          504 :                       && !omp_is_allocatable_or_ptr (var))
   14732         3233 :                      || TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
   14733              :               {
   14734          389 :                 new_var = lookup_decl (var, ctx);
   14735          389 :                 new_var = DECL_VALUE_EXPR (new_var);
   14736          389 :                 gcc_assert (TREE_CODE (new_var) == MEM_REF);
   14737          389 :                 new_var = TREE_OPERAND (new_var, 0);
   14738          389 :                 gcc_assert (DECL_P (new_var));
   14739          389 :                 gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
   14740          778 :                 gimple_seq_add_stmt (&assign_body,
   14741          389 :                                      gimple_build_assign (new_var, x));
   14742              :               }
   14743              :             else
   14744              :               {
   14745         1410 :                 tree type = TREE_TYPE (var);
   14746         1410 :                 new_var = lookup_decl (var, ctx);
   14747         1410 :                 if (omp_privatize_by_reference (var))
   14748              :                   {
   14749         1040 :                     type = TREE_TYPE (type);
   14750         1040 :                     if (POINTER_TYPE_P (type)
   14751         1040 :                         && TREE_CODE (type) != ARRAY_TYPE
   14752         1399 :                         && ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_USE_DEVICE_ADDR
   14753           36 :                             && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
   14754          331 :                             || (omp_privatize_by_reference (var)
   14755          331 :                                 && omp_is_allocatable_or_ptr (var))))
   14756              :                       {
   14757          357 :                         tree v = create_tmp_var_raw (type, get_name (var));
   14758          357 :                         gimple_add_tmp_var (v);
   14759          357 :                         TREE_ADDRESSABLE (v) = 1;
   14760          357 :                         x = fold_convert (type, x);
   14761          357 :                         gimplify_expr (&x, &assign_body, NULL, is_gimple_val,
   14762              :                                        fb_rvalue);
   14763          357 :                         gimple_seq_add_stmt (&assign_body,
   14764          357 :                                              gimple_build_assign (v, x));
   14765          357 :                         x = build_fold_addr_expr (v);
   14766          357 :                         do_optional_check = true;
   14767              :                       }
   14768              :                   }
   14769         1410 :                 new_var = DECL_VALUE_EXPR (new_var);
   14770         1410 :                 x = fold_convert (TREE_TYPE (new_var), x);
   14771         1410 :                 gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
   14772         1410 :                 gimple_seq_add_stmt (&assign_body,
   14773         1410 :                                      gimple_build_assign (new_var, x));
   14774              :               }
   14775         2412 :             tree present;
   14776         1002 :             present = ((do_optional_check
   14777          958 :                         && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
   14778          952 :                         && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR)
   14779         2343 :                        ? omp_check_optional_argument (OMP_CLAUSE_DECL (c), true)
   14780              :                        : NULL_TREE);
   14781         2412 :             if (present)
   14782              :               {
   14783          480 :                 tree null_label = create_artificial_label (UNKNOWN_LOCATION);
   14784          480 :                 tree notnull_label = create_artificial_label (UNKNOWN_LOCATION);
   14785          480 :                 tree opt_arg_label = create_artificial_label (UNKNOWN_LOCATION);
   14786          480 :                 glabel *null_glabel = gimple_build_label (null_label);
   14787          480 :                 glabel *notnull_glabel = gimple_build_label (notnull_label);
   14788          480 :                 ggoto *opt_arg_ggoto = gimple_build_goto (opt_arg_label);
   14789          480 :                 gimplify_expr (&x, &new_body, NULL, is_gimple_val,
   14790              :                                            fb_rvalue);
   14791          480 :                 gimplify_expr (&present, &new_body, NULL, is_gimple_val,
   14792              :                                fb_rvalue);
   14793          480 :                 gcond *cond = gimple_build_cond_from_tree (present,
   14794              :                                                            notnull_label,
   14795              :                                                            null_label);
   14796          480 :                 gimple_seq_add_stmt (&new_body, cond);
   14797          480 :                 gimple_seq_add_stmt (&new_body, null_glabel);
   14798          480 :                 gimplify_assign (new_var, null_pointer_node, &new_body);
   14799          480 :                 gimple_seq_add_stmt (&new_body, opt_arg_ggoto);
   14800          480 :                 gimple_seq_add_stmt (&new_body, notnull_glabel);
   14801          480 :                 gimple_seq_add_seq (&new_body, assign_body);
   14802          480 :                 gimple_seq_add_stmt (&new_body,
   14803          480 :                                      gimple_build_label (opt_arg_label));
   14804              :               }
   14805              :             else
   14806         1932 :               gimple_seq_add_seq (&new_body, assign_body);
   14807              :             break;
   14808              :           }
   14809              :       /* Handle GOMP_MAP_FIRSTPRIVATE_{POINTER,REFERENCE} in second pass,
   14810              :          so that firstprivate vars holding OMP_CLAUSE_SIZE if needed
   14811              :          are already handled.  Similarly OMP_CLAUSE_PRIVATE for VLAs
   14812              :          or references to VLAs.  */
   14813       147079 :       for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
   14814       120779 :         switch (OMP_CLAUSE_CODE (c))
   14815              :           {
   14816              :             tree var, new_var, *allocate_ptr;
   14817              :           default:
   14818       104952 :             break;
   14819        65669 :           case OMP_CLAUSE_MAP:
   14820        65669 :             if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
   14821        65669 :                 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
   14822              :               {
   14823         3724 :                 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
   14824         3724 :                 poly_int64 offset = 0;
   14825         3724 :                 gcc_assert (prev);
   14826         3724 :                 var = OMP_CLAUSE_DECL (c);
   14827         3724 :                 if (DECL_P (var)
   14828         3724 :                     && TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE
   14829         1131 :                     && is_global_var (maybe_lookup_decl_in_outer_ctx (var,
   14830              :                                                                       ctx))
   14831         3822 :                     && varpool_node::get_create (var)->offloadable)
   14832              :                   break;
   14833         3722 :                 if (TREE_CODE (var) == INDIRECT_REF
   14834         3722 :                     && TREE_CODE (TREE_OPERAND (var, 0)) == COMPONENT_REF)
   14835            0 :                   var = TREE_OPERAND (var, 0);
   14836         3722 :                 if (TREE_CODE (var) == COMPONENT_REF)
   14837              :                   {
   14838            0 :                     var = get_addr_base_and_unit_offset (var, &offset);
   14839            0 :                     gcc_assert (var != NULL_TREE && DECL_P (var));
   14840              :                   }
   14841         3722 :                 else if (DECL_SIZE (var)
   14842         3722 :                          && TREE_CODE (DECL_SIZE (var)) != INTEGER_CST)
   14843              :                   {
   14844          712 :                     tree var2 = DECL_VALUE_EXPR (var);
   14845          712 :                     gcc_assert (TREE_CODE (var2) == INDIRECT_REF);
   14846          712 :                     var2 = TREE_OPERAND (var2, 0);
   14847          712 :                     gcc_assert (DECL_P (var2));
   14848              :                     var = var2;
   14849              :                   }
   14850         3722 :                 tree new_var = lookup_decl (var, ctx), x;
   14851         3722 :                 tree type = TREE_TYPE (new_var);
   14852         3722 :                 bool is_ref;
   14853         3722 :                 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == INDIRECT_REF
   14854         3722 :                     && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
   14855              :                         == COMPONENT_REF))
   14856              :                   {
   14857            0 :                     type = TREE_TYPE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0));
   14858            0 :                     is_ref = true;
   14859            0 :                     new_var = build2 (MEM_REF, type,
   14860              :                                       build_fold_addr_expr (new_var),
   14861              :                                       build_int_cst (build_pointer_type (type),
   14862              :                                                      offset));
   14863              :                   }
   14864         3722 :                 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
   14865              :                   {
   14866            0 :                     type = TREE_TYPE (OMP_CLAUSE_DECL (c));
   14867            0 :                     is_ref = TREE_CODE (type) == REFERENCE_TYPE;
   14868            0 :                     new_var = build2 (MEM_REF, type,
   14869              :                                       build_fold_addr_expr (new_var),
   14870              :                                       build_int_cst (build_pointer_type (type),
   14871              :                                                      offset));
   14872              :                   }
   14873              :                 else
   14874         3722 :                   is_ref = omp_privatize_by_reference (var);
   14875         3722 :                 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
   14876              :                   is_ref = false;
   14877         6815 :                 bool ref_to_array = false;
   14878         6815 :                 bool ref_to_ptr = false;
   14879         3293 :                 if (is_ref)
   14880              :                   {
   14881          200 :                     type = TREE_TYPE (type);
   14882          200 :                     if (TREE_CODE (type) == ARRAY_TYPE)
   14883              :                       {
   14884          194 :                         type = build_pointer_type (type);
   14885          194 :                         ref_to_array = true;
   14886              :                       }
   14887              :                   }
   14888         3522 :                 else if (TREE_CODE (type) == ARRAY_TYPE)
   14889              :                   {
   14890          417 :                     tree decl2 = DECL_VALUE_EXPR (new_var);
   14891          417 :                     gcc_assert (TREE_CODE (decl2) == MEM_REF);
   14892          417 :                     decl2 = TREE_OPERAND (decl2, 0);
   14893          417 :                     gcc_assert (DECL_P (decl2));
   14894          417 :                     new_var = decl2;
   14895          417 :                     type = TREE_TYPE (new_var);
   14896              :                   }
   14897         3105 :                 else if (TREE_CODE (type) == REFERENCE_TYPE
   14898         3105 :                          && TREE_CODE (TREE_TYPE (type)) == POINTER_TYPE)
   14899              :                   {
   14900          156 :                     type = TREE_TYPE (type);
   14901          156 :                     ref_to_ptr = true;
   14902              :                   }
   14903         3722 :                 x = build_receiver_ref (prev, false, ctx);
   14904         3722 :                 x = fold_convert_loc (clause_loc, type, x);
   14905         3722 :                 if (!integer_zerop (OMP_CLAUSE_SIZE (c)))
   14906              :                   {
   14907          762 :                     tree bias = OMP_CLAUSE_SIZE (c);
   14908          762 :                     if (DECL_P (bias))
   14909          364 :                       bias = lookup_decl (bias, ctx);
   14910          762 :                     bias = fold_convert_loc (clause_loc, sizetype, bias);
   14911          762 :                     bias = fold_build1_loc (clause_loc, NEGATE_EXPR, sizetype,
   14912              :                                             bias);
   14913          762 :                     x = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
   14914          762 :                                          TREE_TYPE (x), x, bias);
   14915              :                   }
   14916         3722 :                 if (ref_to_array)
   14917          194 :                   x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
   14918         3722 :                 gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
   14919         3722 :                 if ((is_ref && !ref_to_array)
   14920         3716 :                     || ref_to_ptr)
   14921              :                   {
   14922          162 :                     tree t = create_tmp_var_raw (type, get_name (var));
   14923          162 :                     gimple_add_tmp_var (t);
   14924          162 :                     TREE_ADDRESSABLE (t) = 1;
   14925          162 :                     gimple_seq_add_stmt (&new_body,
   14926          162 :                                          gimple_build_assign (t, x));
   14927          162 :                     x = build_fold_addr_expr_loc (clause_loc, t);
   14928              :                   }
   14929         3722 :                 gimple_seq_add_stmt (&new_body,
   14930         3722 :                                      gimple_build_assign (new_var, x));
   14931         3722 :                 prev = NULL_TREE;
   14932              :               }
   14933        61945 :             else if (OMP_CLAUSE_CHAIN (c)
   14934        44402 :                      && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c))
   14935              :                         == OMP_CLAUSE_MAP
   14936       100844 :                      && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
   14937              :                          == GOMP_MAP_FIRSTPRIVATE_POINTER
   14938        35604 :                          || (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
   14939              :                              == GOMP_MAP_FIRSTPRIVATE_REFERENCE)))
   14940              :               prev = c;
   14941              :             break;
   14942          228 :           case OMP_CLAUSE_PRIVATE:
   14943          228 :             var = OMP_CLAUSE_DECL (c);
   14944          228 :             by_ref = omp_privatize_by_reference (var);
   14945          228 :             new_var = lookup_decl (var, ctx);
   14946          228 :             allocate_ptr = alloc_map.get (new_var);
   14947          228 :             if (is_variable_sized (var, by_ref)
   14948          228 :                 || (!allocate_ptr && by_ref && !is_gimple_omp_oacc (ctx->stmt)))
   14949              :               {
   14950           24 :                 if (allocate_ptr)
   14951              :                   {
   14952           12 :                     gimple_seq *allocate_seq = alloc_seq_map.get (new_var);
   14953           12 :                     gcc_assert (allocate_seq);
   14954           12 :                     gimple_seq_add_seq (&new_body, *allocate_seq);
   14955              :                   }
   14956           24 :                 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
   14957           24 :                 tree pvar = var;
   14958           24 :                 if (!by_ref)
   14959              :                   {
   14960            3 :                     pvar = DECL_VALUE_EXPR (var);
   14961            3 :                     gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
   14962            3 :                     pvar = TREE_OPERAND (pvar, 0);
   14963              :                   }
   14964           24 :                 gcc_assert (DECL_P (pvar));
   14965           24 :                 tree new_pvar = lookup_decl (pvar, ctx);
   14966           24 :                 tree x;
   14967           24 :                 if (!allocate_ptr)
   14968              :                   {
   14969           12 :                     tree atmp = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
   14970           12 :                     tree ty = TREE_TYPE (new_var);
   14971           12 :                     if (by_ref)
   14972           11 :                       ty = TREE_TYPE (ty);
   14973           12 :                     x = TYPE_SIZE_UNIT (ty);
   14974           12 :                     if (TREE_CONSTANT (x))
   14975              :                       break;
   14976            9 :                     tree al = size_int (TYPE_ALIGN (ty));
   14977            9 :                     x = build_call_expr_loc (clause_loc, atmp, 2, x, al);
   14978            9 :                     x = fold_convert_loc (clause_loc, TREE_TYPE (new_pvar), x);
   14979              :                   }
   14980              :                 else
   14981           12 :                   x = *allocate_ptr;
   14982           21 :                 gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
   14983           21 :                 gimple_seq_add_stmt (&new_body,
   14984           21 :                                      gimple_build_assign (new_pvar, x));
   14985              :               }
   14986              :             break;
   14987        15854 :           case OMP_CLAUSE_FIRSTPRIVATE:
   14988        15854 :             var = OMP_CLAUSE_DECL (c);
   14989        15854 :             by_ref = omp_privatize_by_reference (var);
   14990        15854 :             if (is_variable_sized (var, by_ref))
   14991              :               {
   14992           42 :                 tree new_var = lookup_decl (var, ctx);
   14993           42 :                 tree *allocate_ptr = alloc_map.get (new_var);
   14994           42 :                 if (!allocate_ptr)
   14995              :                   break;
   14996           15 :                 gimple_seq *allocate_seq = alloc_seq_map.get (new_var);
   14997           15 :                 gcc_assert (allocate_seq);
   14998           15 :                 gimple_seq_add_seq (&new_body, *allocate_seq);
   14999              : 
   15000           15 :                 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
   15001           15 :                 tree pvar = var;
   15002           15 :                 if (!by_ref)
   15003              :                   {
   15004            2 :                     pvar = DECL_VALUE_EXPR (var);
   15005            2 :                     gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
   15006            2 :                     pvar = TREE_OPERAND (pvar, 0);
   15007              :                   }
   15008           15 :                 gcc_assert (DECL_P (pvar));
   15009           15 :                 tree new_pvar = lookup_decl (pvar, ctx);
   15010           15 :                 tree x = fold_convert_loc (clause_loc, TREE_TYPE (new_pvar),
   15011           15 :                                            *allocate_ptr);
   15012           15 :                 gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
   15013           15 :                 gimple_seq_add_stmt (&new_body,
   15014           15 :                                      gimple_build_assign (new_pvar, x));
   15015              : 
   15016           15 :                 x = build_receiver_ref (var, true, ctx);
   15017           15 :                 new_var = unshare_expr (new_var);
   15018           15 :                 if (by_ref)
   15019           13 :                   new_var = build_fold_indirect_ref (new_var);
   15020           15 :                 x = lang_hooks.decls.omp_clause_copy_ctor (c, new_var, x);
   15021           15 :                 gimplify_and_add (x, &new_body);
   15022              :               }
   15023              :           }
   15024              : 
   15025        26300 :       gimple_seq fork_seq = NULL;
   15026        26300 :       gimple_seq join_seq = NULL;
   15027              : 
   15028        26300 :       if (offloaded && is_gimple_omp_oacc (ctx->stmt))
   15029              :         {
   15030              :           /* If there are reductions on the offloaded region itself, treat
   15031              :              them as a dummy GANG loop.  */
   15032         9395 :           tree level = build_int_cst (integer_type_node, GOMP_DIM_GANG);
   15033              : 
   15034         9395 :           gcall *private_marker = lower_oacc_private_marker (ctx);
   15035              : 
   15036         9395 :           if (private_marker)
   15037           30 :             gimple_call_set_arg (private_marker, 2, level);
   15038              : 
   15039         9395 :           lower_oacc_reductions (gimple_location (ctx->stmt), clauses, level,
   15040              :                                  false, NULL, private_marker, NULL, &fork_seq,
   15041              :                                  &join_seq, ctx);
   15042              :         }
   15043              : 
   15044        26300 :       gimple_seq_add_seq (&new_body, fork_seq);
   15045        26300 :       gimple_seq_add_seq (&new_body, tgt_body);
   15046        26300 :       gimple_seq_add_seq (&new_body, join_seq);
   15047        26300 :       gimple_seq_add_seq (&new_body, alloc_dlist);
   15048              : 
   15049        26300 :       if (offloaded)
   15050              :         {
   15051        22276 :           new_body = maybe_catch_exception (new_body);
   15052        22276 :           gimple_seq_add_stmt (&new_body, gimple_build_omp_return (false));
   15053              :         }
   15054        26300 :       gimple_omp_set_body (stmt, new_body);
   15055              :     }
   15056              : 
   15057        38655 :   gsi_insert_seq_before (gsi_p, gimple_omp_target_iterator_loops (stmt),
   15058              :                          GSI_SAME_STMT);
   15059        38655 :   gimple_omp_target_set_iterator_loops (stmt, NULL);
   15060        60931 :   bind = gimple_build_bind (NULL, NULL,
   15061        22276 :                             tgt_bind ? gimple_bind_block (tgt_bind)
   15062              :                                      : NULL_TREE);
   15063        76918 :   gsi_replace (gsi_p, dep_bind ? dep_bind : bind, true);
   15064        38655 :   gimple_bind_add_seq (bind, ilist);
   15065        38655 :   gimple_bind_add_stmt (bind, stmt);
   15066        38655 :   gimple_bind_add_seq (bind, olist);
   15067              : 
   15068        38655 :   pop_gimplify_context (NULL);
   15069              : 
   15070        38655 :   if (dep_bind)
   15071              :     {
   15072          392 :       gimple_bind_add_seq (dep_bind, dep_ilist);
   15073          392 :       gimple_bind_add_stmt (dep_bind, bind);
   15074          392 :       gimple_bind_add_seq (dep_bind, dep_olist);
   15075          392 :       pop_gimplify_context (dep_bind);
   15076              :     }
   15077        38655 : }
   15078              : 
   15079              : /* Generate code to implement the action-clauses (destroy, init, use) of an
   15080              :    OpenMP interop construct.  */
   15081              : 
   15082              : static void
   15083          492 : lower_omp_interop_action_clauses (gimple_seq *seq, vec<tree> &objs,
   15084              :                                   vec<tree> *interop_types = NULL,
   15085              :                                   vec<tree> *prefer_types = NULL)
   15086              : {
   15087          492 :   if (objs.length () == 0)
   15088          229 :     return;
   15089              : 
   15090          263 :   enum omp_clause_code action = OMP_CLAUSE_CODE (objs[0]);
   15091          263 :   if (action == OMP_CLAUSE_INIT)
   15092          402 :     gcc_checking_assert (objs.length () == interop_types->length ()
   15093              :                          && objs.length () == prefer_types->length ());
   15094              :   else
   15095          129 :     gcc_assert (prefer_types == NULL && interop_types == NULL);
   15096              : 
   15097          263 :   tree ret_objs = NULL_TREE, ret_interop_types = NULL_TREE,
   15098          263 :        ret_prefer_types = NULL_TREE;
   15099              : 
   15100              :   /* Build an array of interop objects. */
   15101              : 
   15102          263 :   tree type_obj_pref = build_array_type_nelts (ptr_type_node, objs.length ());
   15103          263 :   ret_objs = create_tmp_var (type_obj_pref, "interopobjs");
   15104              : 
   15105          263 :   bool have_pref_type = false;
   15106          263 :   if (action == OMP_CLAUSE_INIT)
   15107              :     {
   15108          592 :       for (tree pref_type : prefer_types)
   15109          260 :         if (pref_type != NULL_TREE)
   15110              :           {
   15111              :             have_pref_type = true;
   15112              :             break;
   15113              :           }
   15114          134 :       tree type_tgtsync
   15115          134 :         = build_array_type_nelts (integer_type_node, objs.length ());
   15116          134 :       ret_interop_types = create_tmp_var (type_tgtsync, "tgt_tgtsync");
   15117          134 :       if (have_pref_type)
   15118           70 :         ret_prefer_types = create_tmp_var (type_obj_pref, "pref_type");
   15119              :       else
   15120              :         {
   15121           64 :           ret_prefer_types = null_pointer_node;
   15122           64 :           prefer_types->truncate (0);
   15123              :         }
   15124              :     }
   15125              : 
   15126          856 :   for (size_t i = 0; !objs.is_empty (); i++)
   15127              :     {
   15128          593 :       tree offset = build_int_cst (integer_type_node, i);
   15129          593 :       tree init = build4 (ARRAY_REF, ptr_type_node, ret_objs, offset, NULL_TREE,
   15130              :                           NULL_TREE);
   15131          593 :       tree obj = OMP_CLAUSE_DECL (objs.pop ());
   15132          593 :       if (TREE_CODE (TREE_TYPE (obj)) == REFERENCE_TYPE)
   15133            6 :         obj = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (obj)), obj);
   15134          593 :       if (action != OMP_CLAUSE_USE
   15135          593 :           && TREE_CODE (TREE_TYPE (obj)) != POINTER_TYPE)
   15136              :         /* For modifying actions, we need a pointer. */
   15137          483 :         obj = build_fold_addr_expr (obj);
   15138          110 :       else if (action == OMP_CLAUSE_USE
   15139          110 :                && TREE_CODE (TREE_TYPE (obj)) == POINTER_TYPE)
   15140              :         /* For use action, we need the value. */
   15141            2 :         obj = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (obj)), obj);
   15142          593 :       init = build2 (MODIFY_EXPR, ptr_type_node, init,
   15143              :                      fold_convert (ptr_type_node, obj));
   15144          593 :       gimplify_and_add (init, seq);
   15145              : 
   15146          593 :       if (action == OMP_CLAUSE_INIT)
   15147              :         {
   15148          384 :           init = build4 (ARRAY_REF, integer_type_node, ret_interop_types,
   15149              :                          offset, NULL_TREE, NULL_TREE);
   15150          384 :           init = build2 (MODIFY_EXPR, integer_type_node, init,
   15151          384 :                          interop_types->pop ());
   15152          384 :           gimplify_and_add (init, seq);
   15153              : 
   15154          384 :           if (have_pref_type)
   15155              :             {
   15156          194 :               tree prefer_type = prefer_types->pop ();
   15157          194 :               tree pref = (prefer_type == NULL_TREE
   15158          194 :                              ? null_pointer_node
   15159          194 :                              : build_fold_addr_expr (prefer_type));
   15160          194 :               init = build4 (ARRAY_REF, ptr_type_node, ret_prefer_types, offset,
   15161              :                              NULL_TREE, NULL_TREE);
   15162          194 :               init = build2 (MODIFY_EXPR, ptr_type_node, init, pref);
   15163          194 :               gimplify_and_add (init, seq);
   15164              :             }
   15165              :         }
   15166              :     }
   15167          263 :   if (action == OMP_CLAUSE_INIT)
   15168              :     {
   15169          134 :       if (have_pref_type)
   15170           70 :         ret_prefer_types = build_fold_addr_expr (ret_prefer_types);
   15171          134 :       ret_interop_types = build_fold_addr_expr (ret_interop_types);
   15172              :     }
   15173          263 :   ret_objs = build_fold_addr_expr (ret_objs);
   15174              : 
   15175          660 :   gcc_assert (objs.is_empty ()
   15176              :               && (!interop_types || interop_types->is_empty ())
   15177              :               && (!prefer_types || prefer_types->is_empty ()));
   15178              : 
   15179          263 :   objs.safe_push (ret_objs);
   15180          263 :   if (action == OMP_CLAUSE_INIT)
   15181              :     {
   15182          134 :       interop_types->safe_push (ret_interop_types);
   15183          134 :       prefer_types->safe_push (ret_prefer_types);
   15184              :     }
   15185              : }
   15186              : 
   15187              : /* Lower code for an OpenMP interop directive.  */
   15188              : 
   15189              : static void
   15190          164 : lower_omp_interop (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   15191              : {
   15192          164 :   push_gimplify_context ();
   15193              : 
   15194          164 :   tree block = make_node (BLOCK);
   15195          164 :   gbind *bind = gimple_build_bind (NULL, NULL, block);
   15196          164 :   gimple_seq bind_body = NULL;
   15197              : 
   15198              :   /* Emit call to GOMP_interop:
   15199              :       void
   15200              :       GOMP_interop (int device_num, int n_init, omp_interop_t **init,
   15201              :                     const void *target_targetsync, const void *prefer_type,
   15202              :                     int n_use, omp_interop_t *use, int n_destroy,
   15203              :                     omp_interop_t **destroy, unsigned int flags,
   15204              :                     void **depend)  */
   15205              : 
   15206          164 :   tree flags = NULL_TREE;
   15207          164 :   tree depend = null_pointer_node;
   15208          164 :   tree device_num = NULL_TREE;
   15209              : 
   15210          164 :   auto_vec<tree> init_objs, use_objs, destroy_objs, prefer_type,
   15211          164 :     target_targetsync;
   15212          164 :   gimple_seq dep_ilist = NULL, dep_olist = NULL;
   15213          164 :   tree clauses = gimple_omp_interop_clauses (gsi_stmt (*gsi_p));
   15214          868 :   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
   15215              :     {
   15216          704 :       switch (OMP_CLAUSE_CODE (c))
   15217              :         {
   15218          384 :         case OMP_CLAUSE_INIT:
   15219          384 :           {
   15220          384 :             init_objs.safe_push (c);
   15221          384 :             int target_targetsync_bits = 0;
   15222          384 :             if (OMP_CLAUSE_INIT_TARGET (c))
   15223          229 :               target_targetsync_bits |= GOMP_INTEROP_TARGET;
   15224          384 :             if (OMP_CLAUSE_INIT_TARGETSYNC (c))
   15225          204 :               target_targetsync_bits |= GOMP_INTEROP_TARGETSYNC;
   15226          384 :             tree t = build_int_cst (integer_type_node, target_targetsync_bits);
   15227          384 :             target_targetsync.safe_push (t);
   15228          384 :             prefer_type.safe_push (OMP_CLAUSE_INIT_PREFER_TYPE (c));
   15229              :           }
   15230          384 :           break;
   15231          106 :         case OMP_CLAUSE_USE:
   15232          106 :           use_objs.safe_push (c);
   15233          106 :           break;
   15234          103 :         case OMP_CLAUSE_DESTROY:
   15235          103 :           destroy_objs.safe_push (c);
   15236          103 :           break;
   15237           50 :         case OMP_CLAUSE_NOWAIT:
   15238           50 :           flags = build_int_cst (integer_type_node, GOMP_INTEROP_FLAG_NOWAIT);
   15239           50 :           break;
   15240           35 :         case OMP_CLAUSE_DEPEND:
   15241           35 :           {
   15242           35 :             tree *cp = gimple_omp_interop_clauses_ptr (gsi_stmt (*gsi_p));
   15243           35 :             lower_depend_clauses (cp, &dep_ilist, &dep_olist);
   15244           35 :             depend = OMP_CLAUSE_DECL (*cp);
   15245              :           }
   15246           35 :           break;
   15247           26 :         case OMP_CLAUSE_DEVICE:
   15248           26 :           device_num = OMP_CLAUSE_DEVICE_ID (c);
   15249           26 :           break;
   15250            0 :         default:
   15251            0 :           gcc_unreachable ();
   15252              :         }
   15253              :     }
   15254              : 
   15255          164 :   if (flags == NULL_TREE)
   15256          114 :     flags = build_int_cst (integer_type_node, 0);
   15257              : 
   15258          164 :   if (device_num == NULL_TREE)
   15259          138 :     device_num = build_int_cst (integer_type_node, GOMP_DEVICE_DEFAULT_OMP_61);
   15260              : 
   15261          298 :   tree n_init = build_int_cst (integer_type_node, init_objs.length ());
   15262          234 :   tree n_use = build_int_cst (integer_type_node, use_objs.length ());
   15263          223 :   tree n_destroy = build_int_cst (integer_type_node, destroy_objs.length ());
   15264              : 
   15265          164 :   lower_omp_interop_action_clauses (&bind_body, init_objs, &target_targetsync,
   15266              :                                     &prefer_type);
   15267          164 :   lower_omp_interop_action_clauses (&bind_body, use_objs);
   15268          164 :   lower_omp_interop_action_clauses (&bind_body, destroy_objs);
   15269              : 
   15270          164 :   gimple_seq_add_seq (&bind_body, dep_ilist);
   15271          164 :   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_INTEROP);
   15272          298 :   tree init_arg = init_objs.length () ? init_objs.pop () : null_pointer_node;
   15273          164 :   tree target_targetsync_arg = target_targetsync.length ()
   15274          134 :                                  ? target_targetsync.pop ()
   15275          164 :                                  : null_pointer_node;
   15276          164 :   tree prefer_type_arg
   15277          298 :     = prefer_type.length () ? prefer_type.pop () : null_pointer_node;
   15278          234 :   tree use_arg = use_objs.length () ? use_objs.pop () : null_pointer_node;
   15279          164 :   tree destroy_arg
   15280          223 :     = destroy_objs.length () ? destroy_objs.pop () : null_pointer_node;
   15281          164 :   gcall *call
   15282          164 :     = gimple_build_call (fn, 11, device_num, n_init, init_arg,
   15283              :                          target_targetsync_arg, prefer_type_arg, n_use, use_arg,
   15284              :                          n_destroy, destroy_arg, flags, depend);
   15285          164 :   gimple_seq_add_stmt (&bind_body, call);
   15286          164 :   gimple_seq_add_seq (&bind_body, dep_olist);
   15287              : 
   15288          164 :   gsi_replace (gsi_p, bind, true);
   15289          164 :   gimple_bind_set_body (bind, bind_body);
   15290          164 :   pop_gimplify_context (bind);
   15291          164 :   gimple_bind_append_vars (bind, ctx->block_vars);
   15292          164 :   BLOCK_VARS (block) = ctx->block_vars;
   15293          164 : }
   15294              : 
   15295              : /* Expand code for an OpenMP teams directive.  */
   15296              : 
   15297              : static void
   15298         6558 : lower_omp_teams (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   15299              : {
   15300         6558 :   gomp_teams *teams_stmt = as_a <gomp_teams *> (gsi_stmt (*gsi_p));
   15301         6558 :   push_gimplify_context ();
   15302              : 
   15303         6558 :   tree block = make_node (BLOCK);
   15304         6558 :   gbind *bind = gimple_build_bind (NULL, NULL, block);
   15305         6558 :   gsi_replace (gsi_p, bind, true);
   15306         6558 :   gimple_seq bind_body = NULL;
   15307         6558 :   gimple_seq dlist = NULL;
   15308         6558 :   gimple_seq olist = NULL;
   15309              : 
   15310         6558 :   tree num_teams = omp_find_clause (gimple_omp_teams_clauses (teams_stmt),
   15311         6558 :                                     OMP_CLAUSE_NUM_TEAMS);
   15312         6558 :   tree num_teams_lower = NULL_TREE;
   15313         6558 :   if (num_teams == NULL_TREE)
   15314         5900 :     num_teams = build_int_cst (unsigned_type_node, 0);
   15315              :   else
   15316              :     {
   15317          658 :       num_teams_lower = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (num_teams);
   15318          658 :       if (num_teams_lower)
   15319              :         {
   15320          150 :           num_teams_lower = fold_convert (unsigned_type_node, num_teams_lower);
   15321          150 :           gimplify_expr (&num_teams_lower, &bind_body, NULL, is_gimple_val,
   15322              :                          fb_rvalue);
   15323              :         }
   15324          658 :       num_teams = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (num_teams);
   15325              :       // FIXME: Handle dim(x), cf. omp-expand.cc's get_target_arguments.  */
   15326          658 :       if (TREE_CODE (num_teams) == TREE_LIST && TREE_CHAIN (num_teams))
   15327              :         {
   15328            3 :           num_teams_lower = TREE_VALUE (num_teams);
   15329            3 :           num_teams = TREE_VALUE (TREE_CHAIN (num_teams));
   15330              :         }
   15331          655 :       else if (TREE_CODE (num_teams) == TREE_LIST)
   15332            2 :         num_teams = TREE_VALUE (num_teams);
   15333          658 :       num_teams = fold_convert (unsigned_type_node, num_teams);
   15334          658 :       gimplify_expr (&num_teams, &bind_body, NULL, is_gimple_val, fb_rvalue);
   15335              :     }
   15336         6558 :   if (num_teams_lower == NULL_TREE)
   15337         6405 :     num_teams_lower = num_teams;
   15338         6558 :   tree thread_limit = omp_find_clause (gimple_omp_teams_clauses (teams_stmt),
   15339         6558 :                                        OMP_CLAUSE_THREAD_LIMIT);
   15340         6558 :   if (thread_limit == NULL_TREE)
   15341         6077 :     thread_limit = build_int_cst (unsigned_type_node, 0);
   15342              :   else
   15343              :     {
   15344          481 :       thread_limit = OMP_CLAUSE_THREAD_LIMIT_EXPR (thread_limit);
   15345              :       // FIXME: Handle dim(x), cf. omp-expand.cc's get_target_arguments.  */
   15346          481 :       if (TREE_CODE (thread_limit) == TREE_LIST)
   15347           69 :         thread_limit = TREE_VALUE (thread_limit);
   15348          481 :       thread_limit = fold_convert (unsigned_type_node, thread_limit);
   15349          481 :       gimplify_expr (&thread_limit, &bind_body, NULL, is_gimple_val,
   15350              :                      fb_rvalue);
   15351              :     }
   15352         6558 :   location_t loc = gimple_location (teams_stmt);
   15353         6558 :   tree decl = builtin_decl_explicit (BUILT_IN_GOMP_TEAMS4);
   15354         6558 :   tree rettype = TREE_TYPE (TREE_TYPE (decl));
   15355         6558 :   tree first = create_tmp_var (rettype);
   15356         6558 :   gimple_seq_add_stmt (&bind_body,
   15357         6558 :                        gimple_build_assign (first, build_one_cst (rettype)));
   15358         6558 :   tree llabel = create_artificial_label (loc);
   15359         6558 :   gimple_seq_add_stmt (&bind_body, gimple_build_label (llabel));
   15360         6558 :   gimple *call
   15361         6558 :     = gimple_build_call (decl, 4, num_teams_lower, num_teams, thread_limit,
   15362              :                          first);
   15363         6558 :   gimple_set_location (call, loc);
   15364         6558 :   tree temp = create_tmp_var (rettype);
   15365         6558 :   gimple_call_set_lhs (call, temp);
   15366         6558 :   gimple_seq_add_stmt (&bind_body, call);
   15367              : 
   15368         6558 :   tree tlabel = create_artificial_label (loc);
   15369         6558 :   tree flabel = create_artificial_label (loc);
   15370         6558 :   gimple *cond = gimple_build_cond (NE_EXPR, temp, build_zero_cst (rettype),
   15371              :                                     tlabel, flabel);
   15372         6558 :   gimple_seq_add_stmt (&bind_body, cond);
   15373         6558 :   gimple_seq_add_stmt (&bind_body, gimple_build_label (tlabel));
   15374         6558 :   gimple_seq_add_stmt (&bind_body,
   15375         6558 :                        gimple_build_assign (first, build_zero_cst (rettype)));
   15376              : 
   15377         6558 :   lower_rec_input_clauses (gimple_omp_teams_clauses (teams_stmt),
   15378              :                            &bind_body, &dlist, ctx, NULL);
   15379         6558 :   lower_omp (gimple_omp_body_ptr (teams_stmt), ctx);
   15380         6558 :   lower_reduction_clauses (gimple_omp_teams_clauses (teams_stmt), &olist,
   15381              :                            NULL, ctx);
   15382         6558 :   gimple_seq_add_stmt (&bind_body, teams_stmt);
   15383              : 
   15384         6558 :   gimple_seq_add_seq (&bind_body, gimple_omp_body (teams_stmt));
   15385         6558 :   gimple_omp_set_body (teams_stmt, NULL);
   15386         6558 :   gimple_seq_add_seq (&bind_body, olist);
   15387         6558 :   gimple_seq_add_seq (&bind_body, dlist);
   15388         6558 :   gimple_seq_add_stmt (&bind_body, gimple_build_omp_return (true));
   15389         6558 :   gimple_seq_add_stmt (&bind_body, gimple_build_goto (llabel));
   15390         6558 :   gimple_seq_add_stmt (&bind_body, gimple_build_label (flabel));
   15391         6558 :   gimple_bind_set_body (bind, bind_body);
   15392              : 
   15393         6558 :   pop_gimplify_context (bind);
   15394              : 
   15395         6558 :   gimple_bind_append_vars (bind, ctx->block_vars);
   15396         6558 :   BLOCK_VARS (block) = ctx->block_vars;
   15397         6558 :   if (BLOCK_VARS (block))
   15398          945 :     TREE_USED (block) = 1;
   15399         6558 : }
   15400              : 
   15401              : /* Callback for lower_omp_1.  Return non-NULL if *tp needs to be
   15402              :    regimplified.  If DATA is non-NULL, lower_omp_1 is outside
   15403              :    of OMP context, but with make_addressable_vars set.  */
   15404              : 
   15405              : static tree
   15406      3284393 : lower_omp_regimplify_p (tree *tp, int *walk_subtrees,
   15407              :                         void *data)
   15408              : {
   15409      3284393 :   tree t = *tp;
   15410              : 
   15411              :   /* Any variable with DECL_VALUE_EXPR needs to be regimplified.  */
   15412      3284393 :   if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
   15413      1538384 :       && data == NULL
   15414      1477822 :       && DECL_HAS_VALUE_EXPR_P (t))
   15415              :     return t;
   15416              : 
   15417      3173208 :   if (make_addressable_vars
   15418       424545 :       && DECL_P (t)
   15419      3379091 :       && bitmap_bit_p (make_addressable_vars, DECL_UID (t)))
   15420              :     return t;
   15421              : 
   15422              :   /* If a global variable has been privatized, TREE_CONSTANT on
   15423              :      ADDR_EXPR might be wrong.  */
   15424      3170619 :   if (data == NULL && TREE_CODE (t) == ADDR_EXPR)
   15425       111007 :     recompute_tree_invariant_for_addr_expr (t);
   15426              : 
   15427      3170619 :   *walk_subtrees = !IS_TYPE_OR_DECL_P (t);
   15428      3170619 :   return NULL_TREE;
   15429              : }
   15430              : 
   15431              : /* Data to be communicated between lower_omp_regimplify_operands and
   15432              :    lower_omp_regimplify_operands_p.  */
   15433              : 
   15434              : struct lower_omp_regimplify_operands_data
   15435              : {
   15436              :   omp_context *ctx;
   15437              :   vec<tree> *decls;
   15438              : };
   15439              : 
   15440              : /* Helper function for lower_omp_regimplify_operands.  Find
   15441              :    omp_member_access_dummy_var vars and adjust temporarily their
   15442              :    DECL_VALUE_EXPRs if needed.  */
   15443              : 
   15444              : static tree
   15445       442018 : lower_omp_regimplify_operands_p (tree *tp, int *walk_subtrees,
   15446              :                                  void *data)
   15447              : {
   15448       442018 :   tree t = omp_member_access_dummy_var (*tp);
   15449       442018 :   if (t)
   15450              :     {
   15451            9 :       struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
   15452            9 :       lower_omp_regimplify_operands_data *ldata
   15453              :         = (lower_omp_regimplify_operands_data *) wi->info;
   15454            9 :       tree o = maybe_lookup_decl (t, ldata->ctx);
   15455            9 :       if (o == NULL_TREE)
   15456            5 :         o = maybe_lookup_decl_in_outer_ctx (t, ldata->ctx);
   15457            9 :       if (o != t)
   15458              :         {
   15459            9 :           ldata->decls->safe_push (DECL_VALUE_EXPR (*tp));
   15460            9 :           ldata->decls->safe_push (*tp);
   15461            9 :           tree v = unshare_and_remap (DECL_VALUE_EXPR (*tp), t, o);
   15462            9 :           SET_DECL_VALUE_EXPR (*tp, v);
   15463              :         }
   15464              :     }
   15465       442018 :   *walk_subtrees = !IS_TYPE_OR_DECL_P (*tp);
   15466       442018 :   return NULL_TREE;
   15467              : }
   15468              : 
   15469              : /* Wrapper around gimple_regimplify_operands that adjusts DECL_VALUE_EXPRs
   15470              :    of omp_member_access_dummy_var vars during regimplification.  */
   15471              : 
   15472              : static void
   15473       113582 : lower_omp_regimplify_operands (omp_context *ctx, gimple *stmt,
   15474              :                                gimple_stmt_iterator *gsi_p)
   15475              : {
   15476       113582 :   auto_vec<tree, 10> decls;
   15477       113582 :   if (ctx)
   15478              :     {
   15479       111060 :       struct walk_stmt_info wi;
   15480       111060 :       memset (&wi, '\0', sizeof (wi));
   15481       111060 :       struct lower_omp_regimplify_operands_data data;
   15482       111060 :       data.ctx = ctx;
   15483       111060 :       data.decls = &decls;
   15484       111060 :       wi.info = &data;
   15485       111060 :       walk_gimple_op (stmt, lower_omp_regimplify_operands_p, &wi);
   15486              :     }
   15487       113582 :   gimple_regimplify_operands (stmt, gsi_p);
   15488       340755 :   while (!decls.is_empty ())
   15489              :     {
   15490            9 :       tree t = decls.pop ();
   15491            9 :       tree v = decls.pop ();
   15492            9 :       SET_DECL_VALUE_EXPR (t, v);
   15493              :     }
   15494       113582 : }
   15495              : 
   15496              : static void
   15497      2589307 : lower_omp_1 (gimple_stmt_iterator *gsi_p, omp_context *ctx)
   15498              : {
   15499      2589307 :   gimple *stmt = gsi_stmt (*gsi_p);
   15500      2589307 :   struct walk_stmt_info wi;
   15501      2589307 :   gcall *call_stmt;
   15502              : 
   15503      2589307 :   if (gimple_has_location (stmt))
   15504      2124350 :     input_location = gimple_location (stmt);
   15505              : 
   15506      2589307 :   if (make_addressable_vars)
   15507       154508 :     memset (&wi, '\0', sizeof (wi));
   15508              : 
   15509              :   /* If we have issued syntax errors, avoid doing any heavy lifting.
   15510              :      Just replace the OMP directives with a NOP to avoid
   15511              :      confusing RTL expansion.  */
   15512      2589307 :   if (seen_error () && is_gimple_omp (stmt))
   15513              :     {
   15514        11744 :       gsi_replace (gsi_p, gimple_build_nop (), true);
   15515        11744 :       return;
   15516              :     }
   15517              : 
   15518      2577563 :   switch (gimple_code (stmt))
   15519              :     {
   15520       159984 :     case GIMPLE_COND:
   15521       159984 :       {
   15522       159984 :         gcond *cond_stmt = as_a <gcond *> (stmt);
   15523        91693 :         if ((ctx || make_addressable_vars)
   15524       163589 :             && (walk_tree (gimple_cond_lhs_ptr (cond_stmt),
   15525              :                            lower_omp_regimplify_p,
   15526              :                            ctx ? NULL : &wi, NULL)
   15527       138238 :                 || walk_tree (gimple_cond_rhs_ptr (cond_stmt),
   15528              :                               lower_omp_regimplify_p,
   15529              :                               ctx ? NULL : &wi, NULL)))
   15530         1248 :           lower_omp_regimplify_operands (ctx, cond_stmt, gsi_p);
   15531              :       }
   15532              :       break;
   15533           20 :     case GIMPLE_CATCH:
   15534           20 :       lower_omp (gimple_catch_handler_ptr (as_a <gcatch *> (stmt)), ctx);
   15535           20 :       break;
   15536            0 :     case GIMPLE_EH_FILTER:
   15537            0 :       lower_omp (gimple_eh_filter_failure_ptr (stmt), ctx);
   15538            0 :       break;
   15539        26767 :     case GIMPLE_TRY:
   15540        26767 :       lower_omp (gimple_try_eval_ptr (stmt), ctx);
   15541        26762 :       lower_omp (gimple_try_cleanup_ptr (stmt), ctx);
   15542        26762 :       break;
   15543            2 :     case GIMPLE_ASSUME:
   15544            2 :       lower_omp (gimple_assume_body_ptr (stmt), ctx);
   15545            2 :       break;
   15546            5 :     case GIMPLE_TRANSACTION:
   15547            5 :       lower_omp (gimple_transaction_body_ptr (as_a <gtransaction *> (stmt)),
   15548              :                  ctx);
   15549            5 :       break;
   15550       176417 :     case GIMPLE_BIND:
   15551       176417 :       if (ctx && is_gimple_omp_oacc (ctx->stmt))
   15552              :         {
   15553        17761 :           tree vars = gimple_bind_vars (as_a <gbind *> (stmt));
   15554        17761 :           oacc_privatization_scan_decl_chain (ctx, vars);
   15555              :         }
   15556       176417 :       lower_omp (gimple_bind_body_ptr (as_a <gbind *> (stmt)), ctx);
   15557       176410 :       maybe_remove_omp_member_access_dummy_vars (as_a <gbind *> (stmt));
   15558       176410 :       break;
   15559        20946 :     case GIMPLE_OMP_PARALLEL:
   15560        20946 :     case GIMPLE_OMP_TASK:
   15561        20946 :       ctx = maybe_lookup_ctx (stmt);
   15562        20946 :       gcc_assert (ctx);
   15563        20946 :       if (ctx->cancellable)
   15564          146 :         ctx->cancel_label = create_artificial_label (UNKNOWN_LOCATION);
   15565        20946 :       lower_omp_taskreg (gsi_p, ctx);
   15566        20946 :       break;
   15567        49458 :     case GIMPLE_OMP_FOR:
   15568        49458 :       ctx = maybe_lookup_ctx (stmt);
   15569        49458 :       gcc_assert (ctx);
   15570        49458 :       if (ctx->cancellable)
   15571           84 :         ctx->cancel_label = create_artificial_label (UNKNOWN_LOCATION);
   15572        49458 :       lower_omp_for (gsi_p, ctx);
   15573        49458 :       break;
   15574          386 :     case GIMPLE_OMP_SECTIONS:
   15575          386 :       ctx = maybe_lookup_ctx (stmt);
   15576          386 :       gcc_assert (ctx);
   15577          386 :       if (ctx->cancellable)
   15578           19 :         ctx->cancel_label = create_artificial_label (UNKNOWN_LOCATION);
   15579          386 :       lower_omp_sections (gsi_p, ctx);
   15580          386 :       break;
   15581          161 :     case GIMPLE_OMP_SCOPE:
   15582          161 :       ctx = maybe_lookup_ctx (stmt);
   15583          161 :       gcc_assert (ctx);
   15584          161 :       lower_omp_scope (gsi_p, ctx);
   15585          161 :       break;
   15586          659 :     case GIMPLE_OMP_DISPATCH:
   15587          659 :       ctx = maybe_lookup_ctx (stmt);
   15588          659 :       gcc_assert (ctx);
   15589          659 :       lower_omp_dispatch (gsi_p, ctx);
   15590          659 :       break;
   15591          164 :     case GIMPLE_OMP_INTEROP:
   15592          164 :       ctx = maybe_lookup_ctx (stmt);
   15593          164 :       gcc_assert (ctx);
   15594          164 :       lower_omp_interop (gsi_p, ctx);
   15595          164 :       break;
   15596         1146 :     case GIMPLE_OMP_SINGLE:
   15597         1146 :       ctx = maybe_lookup_ctx (stmt);
   15598         1146 :       gcc_assert (ctx);
   15599         1146 :       lower_omp_single (gsi_p, ctx);
   15600         1146 :       break;
   15601          658 :     case GIMPLE_OMP_STRUCTURED_BLOCK:
   15602              :       /* We have already done error checking at this point, so these nodes
   15603              :          can be completely removed and replaced with their body.  */
   15604          658 :       ctx = maybe_lookup_ctx (stmt);
   15605          658 :       gcc_assert (ctx);
   15606          658 :       lower_omp (gimple_omp_body_ptr (stmt), ctx);
   15607          658 :       gsi_replace_with_seq (gsi_p, gimple_omp_body (stmt), true);
   15608          658 :       break;
   15609         1127 :     case GIMPLE_OMP_MASTER:
   15610         1127 :     case GIMPLE_OMP_MASKED:
   15611         1127 :       ctx = maybe_lookup_ctx (stmt);
   15612         1127 :       gcc_assert (ctx);
   15613         1127 :       lower_omp_master (gsi_p, ctx);
   15614         1127 :       break;
   15615          536 :     case GIMPLE_OMP_TASKGROUP:
   15616          536 :       ctx = maybe_lookup_ctx (stmt);
   15617          536 :       gcc_assert (ctx);
   15618          536 :       lower_omp_taskgroup (gsi_p, ctx);
   15619          536 :       break;
   15620         1128 :     case GIMPLE_OMP_ORDERED:
   15621         1128 :       ctx = maybe_lookup_ctx (stmt);
   15622         1128 :       gcc_assert (ctx);
   15623         1128 :       lower_omp_ordered (gsi_p, ctx);
   15624         1128 :       break;
   15625         1336 :     case GIMPLE_OMP_SCAN:
   15626         1336 :       ctx = maybe_lookup_ctx (stmt);
   15627         1336 :       gcc_assert (ctx);
   15628         1336 :       lower_omp_scan (gsi_p, ctx);
   15629         1336 :       break;
   15630          317 :     case GIMPLE_OMP_CRITICAL:
   15631          317 :       ctx = maybe_lookup_ctx (stmt);
   15632          317 :       gcc_assert (ctx);
   15633          317 :       lower_omp_critical (gsi_p, ctx);
   15634          317 :       break;
   15635         2566 :     case GIMPLE_OMP_ATOMIC_LOAD:
   15636           90 :       if ((ctx || make_addressable_vars)
   15637         2566 :           && walk_tree (gimple_omp_atomic_load_rhs_ptr (
   15638              :                           as_a <gomp_atomic_load *> (stmt)),
   15639              :                         lower_omp_regimplify_p, ctx ? NULL : &wi, NULL))
   15640         1442 :         lower_omp_regimplify_operands (ctx, stmt, gsi_p);
   15641              :       break;
   15642        38655 :     case GIMPLE_OMP_TARGET:
   15643        38655 :       ctx = maybe_lookup_ctx (stmt);
   15644        38655 :       gcc_assert (ctx);
   15645        38655 :       lower_omp_target (gsi_p, ctx);
   15646        38655 :       break;
   15647         9136 :     case GIMPLE_OMP_TEAMS:
   15648         9136 :       ctx = maybe_lookup_ctx (stmt);
   15649         9136 :       gcc_assert (ctx);
   15650         9136 :       if (gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
   15651         2578 :         lower_omp_taskreg (gsi_p, ctx);
   15652              :       else
   15653         6558 :         lower_omp_teams (gsi_p, ctx);
   15654              :       break;
   15655       139323 :     case GIMPLE_CALL:
   15656       139323 :       tree fndecl;
   15657       139323 :       call_stmt = as_a <gcall *> (stmt);
   15658       139323 :       fndecl = gimple_call_fndecl (call_stmt);
   15659       139323 :       if (fndecl
   15660       139323 :           && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
   15661        40159 :         switch (DECL_FUNCTION_CODE (fndecl))
   15662              :           {
   15663          742 :           case BUILT_IN_GOMP_BARRIER:
   15664          742 :             if (ctx == NULL)
   15665              :               break;
   15666              :             /* FALLTHRU */
   15667         1070 :           case BUILT_IN_GOMP_CANCEL:
   15668         1070 :           case BUILT_IN_GOMP_CANCELLATION_POINT:
   15669         1070 :             omp_context *cctx;
   15670         1070 :             cctx = ctx;
   15671         1070 :             if (gimple_code (cctx->stmt) == GIMPLE_OMP_SECTION)
   15672           43 :               cctx = cctx->outer;
   15673         1070 :             gcc_assert (gimple_call_lhs (call_stmt) == NULL_TREE);
   15674         1070 :             if (!cctx->cancellable)
   15675              :               {
   15676          688 :                 if (DECL_FUNCTION_CODE (fndecl)
   15677              :                     == BUILT_IN_GOMP_CANCELLATION_POINT)
   15678              :                   {
   15679            0 :                     stmt = gimple_build_nop ();
   15680            0 :                     gsi_replace (gsi_p, stmt, false);
   15681              :                   }
   15682              :                 break;
   15683              :               }
   15684          382 :             if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_GOMP_BARRIER)
   15685              :               {
   15686           20 :                 fndecl = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER_CANCEL);
   15687           20 :                 gimple_call_set_fndecl (call_stmt, fndecl);
   15688           20 :                 gimple_call_set_fntype (call_stmt, TREE_TYPE (fndecl));
   15689              :               }
   15690          382 :             tree lhs;
   15691          382 :             lhs = create_tmp_var (TREE_TYPE (TREE_TYPE (fndecl)));
   15692          382 :             gimple_call_set_lhs (call_stmt, lhs);
   15693          382 :             tree fallthru_label;
   15694          382 :             fallthru_label = create_artificial_label (UNKNOWN_LOCATION);
   15695          382 :             gimple *g;
   15696          382 :             g = gimple_build_label (fallthru_label);
   15697          382 :             gsi_insert_after (gsi_p, g, GSI_SAME_STMT);
   15698          382 :             g = gimple_build_cond (NE_EXPR, lhs,
   15699          382 :                                    fold_convert (TREE_TYPE (lhs),
   15700              :                                                  boolean_false_node),
   15701              :                                    cctx->cancel_label, fallthru_label);
   15702          382 :             gsi_insert_after (gsi_p, g, GSI_SAME_STMT);
   15703          382 :             break;
   15704              :           default:
   15705              :             break;
   15706              :           }
   15707       139288 :       goto regimplify;
   15708              : 
   15709              :     case GIMPLE_ASSIGN:
   15710      1499534 :       for (omp_context *up = ctx; up; up = up->outer)
   15711              :         {
   15712       706179 :           if (gimple_code (up->stmt) == GIMPLE_OMP_ORDERED
   15713              :               || gimple_code (up->stmt) == GIMPLE_OMP_CRITICAL
   15714              :               || gimple_code (up->stmt) == GIMPLE_OMP_TASKGROUP
   15715              :               || gimple_code (up->stmt) == GIMPLE_OMP_SCOPE
   15716              :               || gimple_code (up->stmt) == GIMPLE_OMP_SECTION
   15717              :               || gimple_code (up->stmt) == GIMPLE_OMP_SCAN
   15718              :               || (gimple_code (up->stmt) == GIMPLE_OMP_TARGET
   15719       231056 :                   && (gimple_omp_target_kind (up->stmt)
   15720              :                       == GF_OMP_TARGET_KIND_DATA)))
   15721        90764 :             continue;
   15722       615415 :           else if (!up->lastprivate_conditional_map)
   15723              :             break;
   15724         4632 :           tree lhs = get_base_address (gimple_assign_lhs (stmt));
   15725         4632 :           if (TREE_CODE (lhs) == MEM_REF
   15726           44 :               && DECL_P (TREE_OPERAND (lhs, 0))
   15727         4673 :               && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs,
   15728              :                                                      0))) == REFERENCE_TYPE)
   15729           21 :             lhs = TREE_OPERAND (lhs, 0);
   15730         4632 :           if (DECL_P (lhs))
   15731         3573 :             if (tree *v = up->lastprivate_conditional_map->get (lhs))
   15732              :               {
   15733          398 :                 tree clauses;
   15734          398 :                 if (up->combined_into_simd_safelen1)
   15735              :                   {
   15736           54 :                     up = up->outer;
   15737           54 :                     if (gimple_code (up->stmt) == GIMPLE_OMP_SCAN)
   15738            3 :                       up = up->outer;
   15739              :                   }
   15740          398 :                 if (gimple_code (up->stmt) == GIMPLE_OMP_FOR)
   15741          314 :                   clauses = gimple_omp_for_clauses (up->stmt);
   15742              :                 else
   15743           84 :                   clauses = gimple_omp_sections_clauses (up->stmt);
   15744          398 :                 tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
   15745          398 :                 if (!OMP_CLAUSE__CONDTEMP__ITER (c))
   15746          352 :                   c = omp_find_clause (OMP_CLAUSE_CHAIN (c),
   15747              :                                        OMP_CLAUSE__CONDTEMP_);
   15748          398 :                 gcc_assert (OMP_CLAUSE__CONDTEMP__ITER (c));
   15749          398 :                 gimple *g = gimple_build_assign (*v, OMP_CLAUSE_DECL (c));
   15750          398 :                 gsi_insert_after (gsi_p, g, GSI_SAME_STMT);
   15751              :               }
   15752              :         }
   15753              :       /* FALLTHRU */
   15754              : 
   15755      2085989 :     default:
   15756      2085989 :     regimplify:
   15757      1103225 :       if ((ctx || make_addressable_vars)
   15758      2161990 :           && walk_gimple_op (stmt, lower_omp_regimplify_p,
   15759              :                              ctx ? NULL : &wi))
   15760              :         {
   15761              :           /* Just remove clobbers, this should happen only if we have
   15762              :              "privatized" local addressable variables in SIMD regions,
   15763              :              the clobber isn't needed in that case and gimplifying address
   15764              :              of the ARRAY_REF into a pointer and creating MEM_REF based
   15765              :              clobber would create worse code than we get with the clobber
   15766              :              dropped.  */
   15767       111084 :           if (gimple_clobber_p (stmt))
   15768              :             {
   15769          192 :               gsi_replace (gsi_p, gimple_build_nop (), true);
   15770          192 :               break;
   15771              :             }
   15772       110892 :           lower_omp_regimplify_operands (ctx, stmt, gsi_p);
   15773              :         }
   15774              :       break;
   15775              :     }
   15776              : }
   15777              : 
   15778              : static void
   15779       430918 : lower_omp (gimple_seq *body, omp_context *ctx)
   15780              : {
   15781       430918 :   location_t saved_location = input_location;
   15782       430918 :   gimple_stmt_iterator gsi;
   15783      3400529 :   for (gsi = gsi_start (*body); !gsi_end_p (gsi); gsi_next (&gsi))
   15784      2589307 :     lower_omp_1 (&gsi, ctx);
   15785              :   /* During gimplification, we haven't folded statements inside offloading
   15786              :      or taskreg regions (gimplify.cc:maybe_fold_stmt); do that now.  */
   15787       430902 :   if (target_nesting_level || taskreg_nesting_level)
   15788       705733 :     for (gsi = gsi_start (*body); !gsi_end_p (gsi); gsi_next (&gsi))
   15789       470069 :       fold_stmt (&gsi);
   15790       430902 :   input_location = saved_location;
   15791       430902 : }
   15792              : 
   15793              : /* Main entry point.  */
   15794              : 
   15795              : static unsigned int
   15796      3019898 : execute_lower_omp (void)
   15797              : {
   15798      3019898 :   gimple_seq body;
   15799      3019898 :   int i;
   15800      3019898 :   omp_context *ctx;
   15801              : 
   15802              :   /* This pass always runs, to provide PROP_gimple_lomp.
   15803              :      But often, there is nothing to do.  */
   15804      3019898 :   if (flag_openacc == 0 && flag_openmp == 0
   15805      2961259 :       && flag_openmp_simd == 0)
   15806              :     return 0;
   15807              : 
   15808        60935 :   all_contexts = splay_tree_new (splay_tree_compare_pointers, 0,
   15809              :                                  delete_omp_context);
   15810              : 
   15811        60935 :   body = gimple_body (current_function_decl);
   15812              : 
   15813        60935 :   scan_omp (&body, NULL);
   15814        60935 :   gcc_assert (taskreg_nesting_level == 0);
   15815        88260 :   FOR_EACH_VEC_ELT (taskreg_contexts, i, ctx)
   15816        27325 :     finish_taskreg_scan (ctx);
   15817        60935 :   taskreg_contexts.release ();
   15818              : 
   15819        60935 :   if (all_contexts->root)
   15820              :     {
   15821        27076 :       if (make_addressable_vars)
   15822          624 :         push_gimplify_context ();
   15823        27076 :       lower_omp (&body, NULL);
   15824        27072 :       if (make_addressable_vars)
   15825          624 :         pop_gimplify_context (NULL);
   15826              :     }
   15827              : 
   15828        60931 :   if (all_contexts)
   15829              :     {
   15830        60931 :       splay_tree_delete (all_contexts);
   15831        60931 :       all_contexts = NULL;
   15832              :     }
   15833        60931 :   BITMAP_FREE (make_addressable_vars);
   15834        60931 :   BITMAP_FREE (global_nonaddressable_vars);
   15835              : 
   15836              :   /* If current function is a method, remove artificial dummy VAR_DECL created
   15837              :      for non-static data member privatization, they aren't needed for
   15838              :      debuginfo nor anything else, have been already replaced everywhere in the
   15839              :      IL and cause problems with LTO.  */
   15840        60931 :   if (DECL_ARGUMENTS (current_function_decl)
   15841        34529 :       && DECL_ARTIFICIAL (DECL_ARGUMENTS (current_function_decl))
   15842        71356 :       && (TREE_CODE (TREE_TYPE (DECL_ARGUMENTS (current_function_decl)))
   15843              :           == POINTER_TYPE))
   15844         9855 :     remove_member_access_dummy_vars (DECL_INITIAL (current_function_decl));
   15845              : 
   15846        61925 :   for (auto task_stmt : task_cpyfns)
   15847          514 :     finalize_task_copyfn (task_stmt);
   15848        60931 :   task_cpyfns.release ();
   15849        60931 :   return 0;
   15850              : }
   15851              : 
   15852              : namespace {
   15853              : 
   15854              : const pass_data pass_data_lower_omp =
   15855              : {
   15856              :   GIMPLE_PASS, /* type */
   15857              :   "omplower", /* name */
   15858              :   OPTGROUP_OMP, /* optinfo_flags */
   15859              :   TV_NONE, /* tv_id */
   15860              :   PROP_gimple_any, /* properties_required */
   15861              :   PROP_gimple_lomp | PROP_gimple_lomp_dev, /* properties_provided */
   15862              :   0, /* properties_destroyed */
   15863              :   0, /* todo_flags_start */
   15864              :   0, /* todo_flags_finish */
   15865              : };
   15866              : 
   15867              : class pass_lower_omp : public gimple_opt_pass
   15868              : {
   15869              : public:
   15870       302985 :   pass_lower_omp (gcc::context *ctxt)
   15871       605970 :     : gimple_opt_pass (pass_data_lower_omp, ctxt)
   15872              :   {}
   15873              : 
   15874              :   /* opt_pass methods: */
   15875      3019898 :   unsigned int execute (function *) final override
   15876              :   {
   15877      3019898 :     return execute_lower_omp ();
   15878              :   }
   15879              : 
   15880              : }; // class pass_lower_omp
   15881              : 
   15882              : } // anon namespace
   15883              : 
   15884              : gimple_opt_pass *
   15885       302985 : make_pass_lower_omp (gcc::context *ctxt)
   15886              : {
   15887       302985 :   return new pass_lower_omp (ctxt);
   15888              : }
   15889              : 
   15890              : /* The following is a utility to diagnose structured block violations.
   15891              :    It is not part of the "omplower" pass, as that's invoked too late.  It
   15892              :    should be invoked by the respective front ends after gimplification.  */
   15893              : 
   15894              : static splay_tree all_labels;
   15895              : 
   15896              : /* Check for mismatched contexts and generate an error if needed.  Return
   15897              :    true if an error is detected.  */
   15898              : 
   15899              : static bool
   15900       577287 : diagnose_sb_0 (gimple_stmt_iterator *gsi_p,
   15901              :                gimple *branch_ctx, gimple *label_ctx)
   15902              : {
   15903       577287 :   gcc_checking_assert (!branch_ctx || is_gimple_omp (branch_ctx));
   15904       577287 :   gcc_checking_assert (!label_ctx || is_gimple_omp (label_ctx));
   15905              : 
   15906       577287 :   if (label_ctx == branch_ctx)
   15907              :     return false;
   15908              : 
   15909          180 :   const char* kind = NULL;
   15910              : 
   15911          180 :   if (flag_openacc)
   15912              :     {
   15913            7 :       if ((branch_ctx && is_gimple_omp_oacc (branch_ctx))
   15914           16 :           || (label_ctx && is_gimple_omp_oacc (label_ctx)))
   15915              :         {
   15916              :           gcc_checking_assert (kind == NULL);
   15917              :           kind = "OpenACC";
   15918              :         }
   15919              :     }
   15920              :   if (kind == NULL)
   15921              :     {
   15922          164 :       gcc_checking_assert (flag_openmp || flag_openmp_simd);
   15923              :       kind = "OpenMP";
   15924              :     }
   15925              : 
   15926              :   /* Previously we kept track of the label's entire context in diagnose_sb_[12]
   15927              :      so we could traverse it and issue a correct "exit" or "enter" error
   15928              :      message upon a structured block violation.
   15929              : 
   15930              :      We built the context by building a list with tree_cons'ing, but there is
   15931              :      no easy counterpart in gimple tuples.  It seems like far too much work
   15932              :      for issuing exit/enter error messages.  If someone really misses the
   15933              :      distinct error message... patches welcome.  */
   15934              : 
   15935              : #if 0
   15936              :   /* Try to avoid confusing the user by producing and error message
   15937              :      with correct "exit" or "enter" verbiage.  We prefer "exit"
   15938              :      unless we can show that LABEL_CTX is nested within BRANCH_CTX.  */
   15939              :   if (branch_ctx == NULL)
   15940              :     exit_p = false;
   15941              :   else
   15942              :     {
   15943              :       while (label_ctx)
   15944              :         {
   15945              :           if (TREE_VALUE (label_ctx) == branch_ctx)
   15946              :             {
   15947              :               exit_p = false;
   15948              :               break;
   15949              :             }
   15950              :           label_ctx = TREE_CHAIN (label_ctx);
   15951              :         }
   15952              :     }
   15953              : 
   15954              :   if (exit_p)
   15955              :     error ("invalid exit from %s structured block", kind);
   15956              :   else
   15957              :     error ("invalid entry to %s structured block", kind);
   15958              : #endif
   15959              : 
   15960              :   /* If it's obvious we have an invalid entry, be specific about the error.  */
   15961          180 :   if (branch_ctx == NULL)
   15962           53 :     error ("invalid entry to %s structured block", kind);
   15963              :   else
   15964              :     {
   15965              :       /* Otherwise, be vague and lazy, but efficient.  */
   15966          127 :       error ("invalid branch to/from %s structured block", kind);
   15967              :     }
   15968              : 
   15969          180 :   gsi_replace (gsi_p, gimple_build_nop (), false);
   15970          180 :   return true;
   15971              : }
   15972              : 
   15973              : /* Pass 1: Create a minimal tree of structured blocks, and record
   15974              :    where each label is found.  */
   15975              : 
   15976              : static tree
   15977      3469008 : diagnose_sb_1 (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
   15978              :                struct walk_stmt_info *wi)
   15979              : {
   15980      3469008 :   gimple *context = (gimple *) wi->info;
   15981      3469008 :   gimple *inner_context;
   15982      3469008 :   gimple *stmt = gsi_stmt (*gsi_p);
   15983              : 
   15984      3469008 :   *handled_ops_p = true;
   15985              : 
   15986      3469008 :   switch (gimple_code (stmt))
   15987              :     {
   15988       337161 :     WALK_SUBSTMTS;
   15989              : 
   15990        87246 :     case GIMPLE_OMP_PARALLEL:
   15991        87246 :     case GIMPLE_OMP_TASK:
   15992        87246 :     case GIMPLE_OMP_SCOPE:
   15993        87246 :     case GIMPLE_OMP_SECTIONS:
   15994        87246 :     case GIMPLE_OMP_SINGLE:
   15995        87246 :     case GIMPLE_OMP_SECTION:
   15996        87246 :     case GIMPLE_OMP_STRUCTURED_BLOCK:
   15997        87246 :     case GIMPLE_OMP_MASTER:
   15998        87246 :     case GIMPLE_OMP_MASKED:
   15999        87246 :     case GIMPLE_OMP_ORDERED:
   16000        87246 :     case GIMPLE_OMP_SCAN:
   16001        87246 :     case GIMPLE_OMP_CRITICAL:
   16002        87246 :     case GIMPLE_OMP_TARGET:
   16003        87246 :     case GIMPLE_OMP_TEAMS:
   16004        87246 :     case GIMPLE_OMP_TASKGROUP:
   16005              :       /* The minimal context here is just the current OMP construct.  */
   16006        87246 :       inner_context = stmt;
   16007        87246 :       wi->info = inner_context;
   16008        87246 :       walk_gimple_seq (gimple_omp_body (stmt), diagnose_sb_1, NULL, wi);
   16009        87246 :       wi->info = context;
   16010        87246 :       break;
   16011              : 
   16012        55456 :     case GIMPLE_OMP_FOR:
   16013        55456 :       inner_context = stmt;
   16014        55456 :       wi->info = inner_context;
   16015              :       /* gimple_omp_for_{index,initial,final} are all DECLs; no need to
   16016              :          walk them.  */
   16017        55456 :       walk_gimple_seq (gimple_omp_for_pre_body (stmt),
   16018              :                        diagnose_sb_1, NULL, wi);
   16019        55456 :       walk_gimple_seq (gimple_omp_body (stmt), diagnose_sb_1, NULL, wi);
   16020        55456 :       wi->info = context;
   16021        55456 :       break;
   16022              : 
   16023       572944 :     case GIMPLE_LABEL:
   16024      1145888 :       splay_tree_insert (all_labels,
   16025       572944 :                          (splay_tree_key) gimple_label_label (
   16026       572944 :                                             as_a <glabel *> (stmt)),
   16027              :                          (splay_tree_value) context);
   16028       572944 :       break;
   16029              : 
   16030              :     default:
   16031              :       break;
   16032              :     }
   16033              : 
   16034      3469008 :   return NULL_TREE;
   16035              : }
   16036              : 
   16037              : /* Pass 2: Check each branch and see if its context differs from that of
   16038              :    the destination label's context.  */
   16039              : 
   16040              : static tree
   16041      3469008 : diagnose_sb_2 (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
   16042              :                struct walk_stmt_info *wi)
   16043              : {
   16044      3469008 :   gimple *context = (gimple *) wi->info;
   16045      3469008 :   splay_tree_node n;
   16046      3469008 :   gimple *stmt = gsi_stmt (*gsi_p);
   16047              : 
   16048      3469008 :   *handled_ops_p = true;
   16049              : 
   16050      3469008 :   switch (gimple_code (stmt))
   16051              :     {
   16052       337161 :     WALK_SUBSTMTS;
   16053              : 
   16054        87246 :     case GIMPLE_OMP_PARALLEL:
   16055        87246 :     case GIMPLE_OMP_TASK:
   16056        87246 :     case GIMPLE_OMP_SCOPE:
   16057        87246 :     case GIMPLE_OMP_SECTIONS:
   16058        87246 :     case GIMPLE_OMP_SINGLE:
   16059        87246 :     case GIMPLE_OMP_SECTION:
   16060        87246 :     case GIMPLE_OMP_STRUCTURED_BLOCK:
   16061        87246 :     case GIMPLE_OMP_MASTER:
   16062        87246 :     case GIMPLE_OMP_MASKED:
   16063        87246 :     case GIMPLE_OMP_ORDERED:
   16064        87246 :     case GIMPLE_OMP_SCAN:
   16065        87246 :     case GIMPLE_OMP_CRITICAL:
   16066        87246 :     case GIMPLE_OMP_TARGET:
   16067        87246 :     case GIMPLE_OMP_TEAMS:
   16068        87246 :     case GIMPLE_OMP_TASKGROUP:
   16069        87246 :       wi->info = stmt;
   16070        87246 :       walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), diagnose_sb_2, NULL, wi);
   16071        87246 :       wi->info = context;
   16072        87246 :       break;
   16073              : 
   16074        55456 :     case GIMPLE_OMP_FOR:
   16075        55456 :       wi->info = stmt;
   16076              :       /* gimple_omp_for_{index,initial,final} are all DECLs; no need to
   16077              :          walk them.  */
   16078        55456 :       walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt),
   16079              :                            diagnose_sb_2, NULL, wi);
   16080        55456 :       walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), diagnose_sb_2, NULL, wi);
   16081        55456 :       wi->info = context;
   16082        55456 :       break;
   16083              : 
   16084       211946 :     case GIMPLE_COND:
   16085       211946 :         {
   16086       211946 :           gcond *cond_stmt = as_a <gcond *> (stmt);
   16087       211946 :           tree lab = gimple_cond_true_label (cond_stmt);
   16088       211946 :           if (lab)
   16089              :             {
   16090       211946 :               n = splay_tree_lookup (all_labels,
   16091              :                                      (splay_tree_key) lab);
   16092       423892 :               diagnose_sb_0 (gsi_p, context,
   16093       211946 :                              n ? (gimple *) n->value : NULL);
   16094              :             }
   16095       211946 :           lab = gimple_cond_false_label (cond_stmt);
   16096       211946 :           if (lab)
   16097              :             {
   16098       211946 :               n = splay_tree_lookup (all_labels,
   16099              :                                      (splay_tree_key) lab);
   16100       423892 :               diagnose_sb_0 (gsi_p, context,
   16101       211946 :                              n ? (gimple *) n->value : NULL);
   16102              :             }
   16103              :         }
   16104              :       break;
   16105              : 
   16106       115594 :     case GIMPLE_GOTO:
   16107       115594 :       {
   16108       115594 :         tree lab = gimple_goto_dest (stmt);
   16109       115594 :         if (TREE_CODE (lab) != LABEL_DECL)
   16110              :           break;
   16111              : 
   16112       115594 :         n = splay_tree_lookup (all_labels, (splay_tree_key) lab);
   16113       115594 :         diagnose_sb_0 (gsi_p, context, n ? (gimple *) n->value : NULL);
   16114              :       }
   16115       115594 :       break;
   16116              : 
   16117          439 :     case GIMPLE_SWITCH:
   16118          439 :       {
   16119          439 :         gswitch *switch_stmt = as_a <gswitch *> (stmt);
   16120          439 :         unsigned int i;
   16121         2235 :         for (i = 0; i < gimple_switch_num_labels (switch_stmt); ++i)
   16122              :           {
   16123         1373 :             tree lab = CASE_LABEL (gimple_switch_label (switch_stmt, i));
   16124         1373 :             n = splay_tree_lookup (all_labels, (splay_tree_key) lab);
   16125         1373 :             if (n && diagnose_sb_0 (gsi_p, context, (gimple *) n->value))
   16126              :               break;
   16127              :           }
   16128              :       }
   16129              :       break;
   16130              : 
   16131          734 :     case GIMPLE_ASM:
   16132          734 :       {
   16133          734 :         gasm *asm_stmt = as_a <gasm *> (stmt);
   16134         1483 :         for (unsigned i = 0; i < gimple_asm_nlabels (asm_stmt); ++i)
   16135              :           {
   16136           21 :             tree lab = TREE_VALUE (gimple_asm_label_op (asm_stmt, i));
   16137           21 :             n = splay_tree_lookup (all_labels, (splay_tree_key) lab);
   16138           21 :             if (n && diagnose_sb_0 (gsi_p, context, (gimple *) n->value))
   16139              :               break;
   16140              :           }
   16141              :       }
   16142              :       break;
   16143              : 
   16144        36407 :     case GIMPLE_RETURN:
   16145        36407 :       diagnose_sb_0 (gsi_p, context, NULL);
   16146        36407 :       break;
   16147              : 
   16148              :     default:
   16149              :       break;
   16150              :     }
   16151              : 
   16152      3469008 :   return NULL_TREE;
   16153              : }
   16154              : 
   16155              : static unsigned int
   16156        60944 : diagnose_omp_structured_block_errors (void)
   16157              : {
   16158        60944 :   struct walk_stmt_info wi;
   16159        60944 :   gimple_seq body = gimple_body (current_function_decl);
   16160              : 
   16161        60944 :   all_labels = splay_tree_new (splay_tree_compare_pointers, 0, 0);
   16162              : 
   16163        60944 :   memset (&wi, 0, sizeof (wi));
   16164        60944 :   walk_gimple_seq (body, diagnose_sb_1, NULL, &wi);
   16165              : 
   16166        60944 :   memset (&wi, 0, sizeof (wi));
   16167        60944 :   wi.want_locations = true;
   16168        60944 :   walk_gimple_seq_mod (&body, diagnose_sb_2, NULL, &wi);
   16169              : 
   16170        60944 :   gimple_set_body (current_function_decl, body);
   16171              : 
   16172        60944 :   splay_tree_delete (all_labels);
   16173        60944 :   all_labels = NULL;
   16174              : 
   16175        60944 :   return 0;
   16176              : }
   16177              : 
   16178              : namespace {
   16179              : 
   16180              : const pass_data pass_data_diagnose_omp_blocks =
   16181              : {
   16182              :   GIMPLE_PASS, /* type */
   16183              :   "*diagnose_omp_blocks", /* name */
   16184              :   OPTGROUP_OMP, /* optinfo_flags */
   16185              :   TV_NONE, /* tv_id */
   16186              :   PROP_gimple_any, /* properties_required */
   16187              :   0, /* properties_provided */
   16188              :   0, /* properties_destroyed */
   16189              :   0, /* todo_flags_start */
   16190              :   0, /* todo_flags_finish */
   16191              : };
   16192              : 
   16193              : class pass_diagnose_omp_blocks : public gimple_opt_pass
   16194              : {
   16195              : public:
   16196       302985 :   pass_diagnose_omp_blocks (gcc::context *ctxt)
   16197       605970 :     : gimple_opt_pass (pass_data_diagnose_omp_blocks, ctxt)
   16198              :   {}
   16199              : 
   16200              :   /* opt_pass methods: */
   16201      3019912 :   bool gate (function *) final override
   16202              :   {
   16203      3019912 :     return flag_openacc || flag_openmp || flag_openmp_simd;
   16204              :   }
   16205        60944 :   unsigned int execute (function *) final override
   16206              :     {
   16207        60944 :       return diagnose_omp_structured_block_errors ();
   16208              :     }
   16209              : 
   16210              : }; // class pass_diagnose_omp_blocks
   16211              : 
   16212              : } // anon namespace
   16213              : 
   16214              : gimple_opt_pass *
   16215       302985 : make_pass_diagnose_omp_blocks (gcc::context *ctxt)
   16216              : {
   16217       302985 :   return new pass_diagnose_omp_blocks (ctxt);
   16218              : }
   16219              : 
   16220              : 
   16221              : #include "gt-omp-low.h"
        

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.