LCOV - code coverage report
Current view: top level - gcc - gimplify.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.5 % 10810 10219
Test Date: 2026-12-05 16:10:02 Functions: 97.9 % 242 237
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Tree lowering pass.  This pass converts the GENERIC functions-as-trees
       2              :    tree representation into the GIMPLE form.
       3              :    Copyright (C) 2002-2026 Free Software Foundation, Inc.
       4              :    Major work done by Sebastian Pop <s.pop@laposte.net>,
       5              :    Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
       6              : 
       7              : This file is part of GCC.
       8              : 
       9              : GCC is free software; you can redistribute it and/or modify it under
      10              : the terms of the GNU General Public License as published by the Free
      11              : Software Foundation; either version 3, or (at your option) any later
      12              : version.
      13              : 
      14              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      15              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      16              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      17              : for more details.
      18              : 
      19              : You should have received a copy of the GNU General Public License
      20              : along with GCC; see the file COPYING3.  If not see
      21              : <http://www.gnu.org/licenses/>.  */
      22              : 
      23              : #include "config.h"
      24              : #include "system.h"
      25              : #include "coretypes.h"
      26              : #include "backend.h"
      27              : #include "target.h"
      28              : #include "rtl.h"
      29              : #include "tree.h"
      30              : #include "memmodel.h"
      31              : #include "tm_p.h"
      32              : #include "gimple.h"
      33              : #include "gimple-predict.h"
      34              : #include "tree-pass.h"                /* FIXME: only for PROP_gimple_any */
      35              : #include "ssa.h"
      36              : #include "cgraph.h"
      37              : #include "tree-pretty-print.h"
      38              : #include "diagnostic-core.h"
      39              : #include "diagnostic.h"               /* For errorcount.  */
      40              : #include "alias.h"
      41              : #include "fold-const.h"
      42              : #include "calls.h"
      43              : #include "varasm.h"
      44              : #include "stmt.h"
      45              : #include "expr.h"
      46              : #include "gimple-iterator.h"
      47              : #include "gimple-fold.h"
      48              : #include "tree-eh.h"
      49              : #include "gimplify.h"
      50              : #include "stor-layout.h"
      51              : #include "print-tree.h"
      52              : #include "tree-iterator.h"
      53              : #include "tree-inline.h"
      54              : #include "langhooks.h"
      55              : #include "tree-cfg.h"
      56              : #include "tree-ssa.h"
      57              : #include "tree-hash-traits.h"
      58              : #include "omp-general.h"
      59              : #include "omp-low.h"
      60              : #include "gimple-low.h"
      61              : #include "gomp-constants.h"
      62              : #include "splay-tree.h"
      63              : #include "gimple-walk.h"
      64              : #include "langhooks-def.h"    /* FIXME: for lhd_set_decl_assembler_name */
      65              : #include "builtins.h"
      66              : #include "stringpool.h"
      67              : #include "attribs.h"
      68              : #include "asan.h"
      69              : #include "dbgcnt.h"
      70              : #include "omp-offload.h"
      71              : #include "context.h"
      72              : #include "tree-nested.h"
      73              : #include "gcc-urlifier.h"
      74              : #include "insn-config.h"
      75              : #include "recog.h"
      76              : #include "output.h"
      77              : #include "gimplify_reg_info.h"
      78              : #include "tree-ssa-loop-niter.h" /* For simplify_replace_tree.  */
      79              : 
      80              : /* Identifier for a basic condition, mapping it to other basic conditions of
      81              :    its Boolean expression.  Basic conditions given the same uid (in the same
      82              :    function) are parts of the same ANDIF/ORIF expression.  Used for condition
      83              :    coverage.  */
      84              : static unsigned nextconduid = 1;
      85              : 
      86              : /* Annotated gconds so that basic conditions in the same expression map to
      87              :    the same uid.  This is used for condition coverage.  */
      88              : static hash_map <tree, unsigned> *cond_uids;
      89              : 
      90              : /* Get a fresh identifier for a new condition expression.  This is used for
      91              :    condition coverage.  */
      92              : static unsigned
      93      5931748 : next_cond_uid ()
      94              : {
      95      5931748 :   return nextconduid++;
      96              : }
      97              : 
      98              : /* Reset the condition uid to the value it should have when compiling a new
      99              :    function.  0 is already the default/untouched value, so start at non-zero.
     100              :    A valid and set id should always be > 0.  This is used for condition
     101              :    coverage.  */
     102              : static void
     103      3020016 : reset_cond_uid ()
     104              : {
     105      3020016 :   nextconduid = 1;
     106            0 : }
     107              : 
     108              : /* Associate the condition STMT with the discriminator UID.  STMTs that are
     109              :    broken down with ANDIF/ORIF from the same Boolean expression should be given
     110              :    the same UID; 'if (a && b && c) { if (d || e) ... } ...' should yield the
     111              :    { a: 1, b: 1, c: 1, d: 2, e: 2 } when gimplification is done.  This is used
     112              :    for condition coverage.  */
     113              : static void
     114      1235372 : tree_associate_condition_with_expr (tree stmt, unsigned uid)
     115              : {
     116      1235372 :   if (!condition_coverage_flag)
     117              :     return;
     118              : 
     119          542 :   if (!cond_uids)
     120           68 :     cond_uids = new hash_map <tree, unsigned> ();
     121              : 
     122          542 :   cond_uids->put (stmt, uid);
     123              : }
     124              : 
     125              : /* Hash set of poisoned variables in a bind expr.  */
     126              : static hash_set<tree> *asan_poisoned_variables = NULL;
     127              : 
     128              : /* Hash set of already-resolved calls to OpenMP "declare variant"
     129              :    functions.  A call can resolve to the original function and
     130              :    we don't want to repeat the resolution multiple times.  */
     131              : static hash_set<tree> *omp_resolved_variant_calls = NULL;
     132              : 
     133              : enum gimplify_omp_var_data
     134              : {
     135              :   GOVD_SEEN = 0x000001,
     136              :   GOVD_EXPLICIT = 0x000002,
     137              :   GOVD_SHARED = 0x000004,
     138              :   GOVD_PRIVATE = 0x000008,
     139              :   GOVD_FIRSTPRIVATE = 0x000010,
     140              :   GOVD_LASTPRIVATE = 0x000020,
     141              :   GOVD_REDUCTION = 0x000040,
     142              :   GOVD_LOCAL = 0x00080,
     143              :   GOVD_MAP = 0x000100,
     144              :   GOVD_DEBUG_PRIVATE = 0x000200,
     145              :   GOVD_PRIVATE_OUTER_REF = 0x000400,
     146              :   GOVD_LINEAR = 0x000800,
     147              :   GOVD_ALIGNED = 0x001000,
     148              : 
     149              :   /* Flag for GOVD_MAP: don't copy back.  */
     150              :   GOVD_MAP_TO_ONLY = 0x002000,
     151              : 
     152              :   /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference.  */
     153              :   GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 0x004000,
     154              : 
     155              :   GOVD_MAP_0LEN_ARRAY = 0x008000,
     156              : 
     157              :   /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping.  */
     158              :   GOVD_MAP_ALWAYS_TO = 0x010000,
     159              : 
     160              :   /* Flag for shared vars that are or might be stored to in the region.  */
     161              :   GOVD_WRITTEN = 0x020000,
     162              : 
     163              :   /* Flag for GOVD_MAP, if it is a forced mapping.  */
     164              :   GOVD_MAP_FORCE = 0x040000,
     165              : 
     166              :   /* Flag for GOVD_MAP: must be present already.  */
     167              :   GOVD_MAP_FORCE_PRESENT = 0x080000,
     168              : 
     169              :   /* Flag for GOVD_MAP: only allocate.  */
     170              :   GOVD_MAP_ALLOC_ONLY = 0x100000,
     171              : 
     172              :   /* Flag for GOVD_MAP: only copy back.  */
     173              :   GOVD_MAP_FROM_ONLY = 0x200000,
     174              : 
     175              :   GOVD_NONTEMPORAL = 0x400000,
     176              : 
     177              :   /* Flag for GOVD_LASTPRIVATE: conditional modifier.  */
     178              :   GOVD_LASTPRIVATE_CONDITIONAL = 0x800000,
     179              : 
     180              :   GOVD_CONDTEMP = 0x1000000,
     181              : 
     182              :   /* Flag for GOVD_REDUCTION: inscan seen in {in,ex}clusive clause.  */
     183              :   GOVD_REDUCTION_INSCAN = 0x2000000,
     184              : 
     185              :   /* Flag for GOVD_FIRSTPRIVATE: OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT.  */
     186              :   GOVD_FIRSTPRIVATE_IMPLICIT = 0x4000000,
     187              : 
     188              :   /* Flag to indicate this an allocator for the uses_allocators clause.  */
     189              :   GOVD_USES_ALLOCATORS_ALLOCATOR = 0x8000000,
     190              : 
     191              :   GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
     192              :                            | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
     193              :                            | GOVD_LOCAL)
     194              : };
     195              : 
     196              : 
     197              : enum omp_region_type
     198              : {
     199              :   ORT_WORKSHARE = 0x00,
     200              :   ORT_TASKGROUP = 0x01,
     201              :   ORT_DISPATCH  = 0x02,
     202              :   ORT_SIMD      = 0x04,
     203              : 
     204              :   ORT_PARALLEL  = 0x08,
     205              :   ORT_COMBINED_PARALLEL = ORT_PARALLEL | 1,
     206              : 
     207              :   ORT_TASK      = 0x10,
     208              :   ORT_UNTIED_TASK = ORT_TASK | 1,
     209              :   ORT_TASKLOOP  = ORT_TASK | 2,
     210              :   ORT_UNTIED_TASKLOOP = ORT_UNTIED_TASK | 2,
     211              : 
     212              :   ORT_TEAMS     = 0x20,
     213              :   ORT_COMBINED_TEAMS = ORT_TEAMS | 1,
     214              :   ORT_HOST_TEAMS = ORT_TEAMS | 2,
     215              :   ORT_COMBINED_HOST_TEAMS = ORT_COMBINED_TEAMS | 2,
     216              : 
     217              :   /* Data region.  */
     218              :   ORT_TARGET_DATA = 0x40,
     219              : 
     220              :   /* Data region with offloading.  */
     221              :   ORT_TARGET    = 0x80,
     222              :   ORT_COMBINED_TARGET = ORT_TARGET | 1,
     223              :   ORT_IMPLICIT_TARGET = ORT_TARGET | 2,
     224              : 
     225              :   /* OpenACC variants.  */
     226              :   ORT_ACC       = 0x100,  /* A generic OpenACC region.  */
     227              :   ORT_ACC_DATA  = ORT_ACC | ORT_TARGET_DATA, /* Data construct.  */
     228              :   ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET,  /* Parallel construct */
     229              :   ORT_ACC_KERNELS  = ORT_ACC | ORT_TARGET | 2,  /* Kernels construct.  */
     230              :   ORT_ACC_SERIAL   = ORT_ACC | ORT_TARGET | 4,  /* Serial construct.  */
     231              :   ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 2,  /* Host data.  */
     232              : 
     233              :   /* Dummy OpenMP region, used to disable expansion of
     234              :      DECL_VALUE_EXPRs in taskloop pre body.  */
     235              :   ORT_NONE      = 0x200
     236              : };
     237              : 
     238              : /* Gimplify hashtable helper.  */
     239              : 
     240              : struct gimplify_hasher : free_ptr_hash <elt_t>
     241              : {
     242              :   static inline hashval_t hash (const elt_t *);
     243              :   static inline bool equal (const elt_t *, const elt_t *);
     244              : };
     245              : 
     246              : struct gimplify_ctx
     247              : {
     248              :   struct gimplify_ctx *prev_context;
     249              : 
     250              :   vec<gbind *> bind_expr_stack;
     251              :   tree temps;
     252              :   gimple_seq conditional_cleanups;
     253              :   tree exit_label;
     254              :   tree return_temp;
     255              : 
     256              :   vec<tree> case_labels;
     257              :   hash_set<tree> *live_switch_vars;
     258              :   /* The formal temporary table.  Should this be persistent?  */
     259              :   hash_table<gimplify_hasher> *temp_htab;
     260              : 
     261              :   int conditions;
     262              :   unsigned into_ssa : 1;
     263              :   unsigned allow_rhs_cond_expr : 1;
     264              :   unsigned in_cleanup_point_expr : 1;
     265              :   unsigned keep_stack : 1;
     266              :   unsigned save_stack : 1;
     267              :   unsigned in_switch_expr : 1;
     268              :   unsigned in_handler_expr : 1;
     269              : };
     270              : 
     271              : enum gimplify_defaultmap_kind
     272              : {
     273              :   GDMK_SCALAR,
     274              :   GDMK_SCALAR_TARGET, /* w/ Fortran's target attr, implicit mapping, only.  */
     275              :   GDMK_AGGREGATE,
     276              :   GDMK_ALLOCATABLE,
     277              :   GDMK_POINTER
     278              : };
     279              : 
     280              : struct gimplify_omp_ctx
     281              : {
     282              :   struct gimplify_omp_ctx *outer_context;
     283              :   splay_tree variables;
     284              :   hash_map<omp_name_type<tree>, tree> *implicit_mappers;
     285              :   hash_set<tree> *privatized_types;
     286              :   tree clauses;
     287              :   /* Iteration variables in an OMP_FOR.  */
     288              :   vec<tree> loop_iter_var;
     289              :   location_t location;
     290              :   enum omp_clause_default_kind default_kind;
     291              :   enum omp_region_type region_type;
     292              :   enum tree_code code;
     293              :   bool combined_loop;
     294              :   bool distribute;
     295              :   bool target_firstprivatize_array_bases;
     296              :   bool add_safelen1;
     297              :   bool order_concurrent;
     298              :   bool has_depend;
     299              :   bool in_for_exprs;
     300              :   bool in_call_args;
     301              :   int defaultmap[5];
     302              : };
     303              : 
     304              : static struct gimplify_ctx *gimplify_ctxp;
     305              : static struct gimplify_omp_ctx *gimplify_omp_ctxp;
     306              : static bool in_omp_construct;
     307              : 
     308              : /* Forward declaration.  */
     309              : static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
     310              : static hash_map<tree, tree> *oacc_declare_returns;
     311              : static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
     312              :                                            bool (*) (tree), fallback_t, bool);
     313              : static void prepare_gimple_addressable (tree *, gimple_seq *);
     314              : 
     315              : /* Shorter alias name for the above function for use in gimplify.cc
     316              :    only.  */
     317              : 
     318              : static inline void
     319     95483834 : gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
     320              : {
     321     95483834 :   gimple_seq_add_stmt_without_update (seq_p, gs);
     322              : }
     323              : 
     324              : /* Append sequence SRC to the end of sequence *DST_P.  If *DST_P is
     325              :    NULL, a new sequence is allocated.   This function is
     326              :    similar to gimple_seq_add_seq, but does not scan the operands.
     327              :    During gimplification, we need to manipulate statement sequences
     328              :    before the def/use vectors have been constructed.  */
     329              : 
     330              : static void
     331      9589360 : gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
     332              : {
     333      9589360 :   gimple_stmt_iterator si;
     334              : 
     335      9589360 :   if (src == NULL)
     336      4135847 :     return;
     337              : 
     338      5453513 :   si = gsi_last (*dst_p);
     339      5453513 :   gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
     340              : }
     341              : 
     342              : 
     343              : /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
     344              :    and popping gimplify contexts.  */
     345              : 
     346              : static struct gimplify_ctx *ctx_pool = NULL;
     347              : 
     348              : /* Return a gimplify context struct from the pool.  */
     349              : 
     350              : static inline struct gimplify_ctx *
     351      9246105 : ctx_alloc (void)
     352              : {
     353      9246105 :   struct gimplify_ctx * c = ctx_pool;
     354              : 
     355      9246105 :   if (c)
     356      8968544 :     ctx_pool = c->prev_context;
     357              :   else
     358       277561 :     c = XNEW (struct gimplify_ctx);
     359              : 
     360      9246105 :   memset (c, '\0', sizeof (*c));
     361      9246105 :   return c;
     362              : }
     363              : 
     364              : /* Put gimplify context C back into the pool.  */
     365              : 
     366              : static inline void
     367      9246101 : ctx_free (struct gimplify_ctx *c)
     368              : {
     369      9246101 :   c->prev_context = ctx_pool;
     370      9246101 :   ctx_pool = c;
     371              : }
     372              : 
     373              : /* Free allocated ctx stack memory.  */
     374              : 
     375              : void
     376       243813 : free_gimplify_stack (void)
     377              : {
     378       243813 :   struct gimplify_ctx *c;
     379              : 
     380       489639 :   while ((c = ctx_pool))
     381              :     {
     382       245826 :       ctx_pool = c->prev_context;
     383       245826 :       free (c);
     384              :     }
     385       243813 : }
     386              : 
     387              : 
     388              : /* Set up a context for the gimplifier.  */
     389              : 
     390              : void
     391      9246105 : push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
     392              : {
     393      9246105 :   struct gimplify_ctx *c = ctx_alloc ();
     394              : 
     395      9246105 :   c->prev_context = gimplify_ctxp;
     396      9246105 :   gimplify_ctxp = c;
     397      9246105 :   gimplify_ctxp->into_ssa = in_ssa;
     398      9246105 :   gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
     399      9246105 : }
     400              : 
     401              : /* Tear down a context for the gimplifier.  If BODY is non-null, then
     402              :    put the temporaries into the outer BIND_EXPR.  Otherwise, put them
     403              :    in the local_decls.
     404              : 
     405              :    BODY is not a sequence, but the first tuple in a sequence.  */
     406              : 
     407              : void
     408      9246101 : pop_gimplify_context (gimple *body)
     409              : {
     410      9246101 :   struct gimplify_ctx *c = gimplify_ctxp;
     411              : 
     412      9246101 :   gcc_assert (c
     413              :               && (!c->bind_expr_stack.exists ()
     414              :                   || c->bind_expr_stack.is_empty ()));
     415      9246101 :   c->bind_expr_stack.release ();
     416      9246101 :   gimplify_ctxp = c->prev_context;
     417              : 
     418      9246101 :   if (body)
     419      3151100 :     declare_vars (c->temps, body, false);
     420              :   else
     421      6095001 :     record_vars (c->temps);
     422              : 
     423      9246101 :   delete c->temp_htab;
     424      9246101 :   c->temp_htab = NULL;
     425      9246101 :   ctx_free (c);
     426      9246101 : }
     427              : 
     428              : /* Push a GIMPLE_BIND tuple onto the stack of bindings.  */
     429              : 
     430              : static void
     431      6123876 : gimple_push_bind_expr (gbind *bind_stmt)
     432              : {
     433      6123876 :   gimplify_ctxp->bind_expr_stack.reserve (8);
     434      6123876 :   gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
     435      6123876 : }
     436              : 
     437              : /* Pop the first element off the stack of bindings.  */
     438              : 
     439              : static void
     440      6123876 : gimple_pop_bind_expr (void)
     441              : {
     442            0 :   gimplify_ctxp->bind_expr_stack.pop ();
     443            0 : }
     444              : 
     445              : /* Return the first element of the stack of bindings.  */
     446              : 
     447              : gbind *
     448            0 : gimple_current_bind_expr (void)
     449              : {
     450            0 :   return gimplify_ctxp->bind_expr_stack.last ();
     451              : }
     452              : 
     453              : /* Return the stack of bindings created during gimplification.  */
     454              : 
     455              : vec<gbind *>
     456          370 : gimple_bind_expr_stack (void)
     457              : {
     458          370 :   return gimplify_ctxp->bind_expr_stack;
     459              : }
     460              : 
     461              : /* Return true iff there is a COND_EXPR between us and the innermost
     462              :    CLEANUP_POINT_EXPR.  This info is used by gimple_push_cleanup.  */
     463              : 
     464              : static bool
     465      2950851 : gimple_conditional_context (void)
     466              : {
     467      2950851 :   return gimplify_ctxp->conditions > 0;
     468              : }
     469              : 
     470              : /* Note that we've entered a COND_EXPR.  */
     471              : 
     472              : static void
     473      5932195 : gimple_push_condition (void)
     474              : {
     475              : #ifdef ENABLE_GIMPLE_CHECKING
     476      5932195 :   if (gimplify_ctxp->conditions == 0)
     477      3906151 :     gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
     478              : #endif
     479      5932195 :   ++(gimplify_ctxp->conditions);
     480      5932195 : }
     481              : 
     482              : /* Note that we've left a COND_EXPR.  If we're back at unconditional scope
     483              :    now, add any conditional cleanups we've seen to the prequeue.  */
     484              : 
     485              : static void
     486      5932195 : gimple_pop_condition (gimple_seq *pre_p)
     487              : {
     488      5932195 :   int conds = --(gimplify_ctxp->conditions);
     489              : 
     490      5932195 :   gcc_assert (conds >= 0);
     491      5932195 :   if (conds == 0)
     492              :     {
     493      3906151 :       gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
     494      3906151 :       gimplify_ctxp->conditional_cleanups = NULL;
     495              :     }
     496      5932195 : }
     497              : 
     498              : /* A stable comparison routine for use with splay trees and DECLs.  */
     499              : 
     500              : static int
     501     18925873 : splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
     502              : {
     503     18925873 :   tree a = (tree) xa;
     504     18925873 :   tree b = (tree) xb;
     505              : 
     506     18925873 :   return DECL_UID (a) - DECL_UID (b);
     507              : }
     508              : 
     509              : /* Create a new omp construct that deals with variable remapping.  */
     510              : 
     511              : static struct gimplify_omp_ctx *
     512       146771 : new_omp_context (enum omp_region_type region_type)
     513              : {
     514       146771 :   struct gimplify_omp_ctx *c;
     515              : 
     516       146771 :   c = XCNEW (struct gimplify_omp_ctx);
     517       146771 :   c->outer_context = gimplify_omp_ctxp;
     518       146771 :   c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
     519       146771 :   c->implicit_mappers = new hash_map<omp_name_type<tree>, tree>;
     520       146771 :   c->privatized_types = new hash_set<tree>;
     521       146771 :   c->location = input_location;
     522       146771 :   c->region_type = region_type;
     523       146771 :   if ((region_type & ORT_TASK) == 0)
     524              :     c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
     525              :   else
     526         6327 :     c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
     527       146771 :   c->defaultmap[GDMK_SCALAR] = GOVD_MAP;
     528       146771 :   c->defaultmap[GDMK_SCALAR_TARGET] = GOVD_MAP;
     529       146771 :   c->defaultmap[GDMK_AGGREGATE] = GOVD_MAP;
     530       146771 :   c->defaultmap[GDMK_ALLOCATABLE] = GOVD_MAP;
     531       146771 :   c->defaultmap[GDMK_POINTER] = GOVD_MAP;
     532              : 
     533       146771 :   return c;
     534              : }
     535              : 
     536              : /* Destroy an omp construct that deals with variable remapping.  */
     537              : 
     538              : static void
     539       146212 : delete_omp_context (struct gimplify_omp_ctx *c)
     540              : {
     541       146212 :   splay_tree_delete (c->variables);
     542       292424 :   delete c->privatized_types;
     543       292424 :   delete c->implicit_mappers;
     544       146212 :   c->loop_iter_var.release ();
     545       146212 :   XDELETE (c);
     546       146212 : }
     547              : 
     548              : static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
     549              : static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
     550              : 
     551              : /* Both gimplify the statement T and append it to *SEQ_P.  This function
     552              :    behaves exactly as gimplify_stmt, but you don't have to pass T as a
     553              :    reference.  */
     554              : 
     555              : void
     556     39240262 : gimplify_and_add (tree t, gimple_seq *seq_p)
     557              : {
     558     39240262 :   gimplify_stmt (&t, seq_p);
     559     39240262 : }
     560              : 
     561              : /* Gimplify statement T into sequence *SEQ_P, and return the first
     562              :    tuple in the sequence of generated tuples for this statement.
     563              :    Return NULL if gimplifying T produced no tuples.  */
     564              : 
     565              : static gimple *
     566       110552 : gimplify_and_return_first (tree t, gimple_seq *seq_p)
     567              : {
     568       110552 :   gimple_stmt_iterator last = gsi_last (*seq_p);
     569              : 
     570       110552 :   gimplify_and_add (t, seq_p);
     571              : 
     572       110552 :   if (!gsi_end_p (last))
     573              :     {
     574         5905 :       gsi_next (&last);
     575         5905 :       return gsi_stmt (last);
     576              :     }
     577              :   else
     578       104647 :     return gimple_seq_first_stmt (*seq_p);
     579              : }
     580              : 
     581              : /* Returns true iff T is a valid RHS for an assignment to an un-renamed
     582              :    LHS, or for a call argument.  */
     583              : 
     584              : static bool
     585       239077 : is_gimple_mem_rhs (tree t)
     586              : {
     587              :   /* If we're dealing with a renamable type, either source or dest must be
     588              :      a renamed variable.  */
     589       239077 :   if (is_gimple_reg_type (TREE_TYPE (t)))
     590       235699 :     return is_gimple_val (t);
     591              :   else
     592         3378 :     return is_gimple_val (t) || is_gimple_lvalue (t);
     593              : }
     594              : 
     595              : /* Return true if T is a CALL_EXPR or an expression that can be
     596              :    assigned to a temporary.  Note that this predicate should only be
     597              :    used during gimplification.  See the rationale for this in
     598              :    gimplify_modify_expr.  */
     599              : 
     600              : static bool
     601    100884364 : is_gimple_reg_rhs_or_call (tree t)
     602              : {
     603     74117803 :   return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
     604    100884364 :           || TREE_CODE (t) == CALL_EXPR);
     605              : }
     606              : 
     607              : /* Return true if T is a valid memory RHS or a CALL_EXPR.  Note that
     608              :    this predicate should only be used during gimplification.  See the
     609              :    rationale for this in gimplify_modify_expr.  */
     610              : 
     611              : static bool
     612     14839802 : is_gimple_mem_rhs_or_call (tree t)
     613              : {
     614              :   /* If we're dealing with a renamable type, either source or dest must be
     615              :      a renamed variable.  */
     616     14839802 :   if (is_gimple_reg_type (TREE_TYPE (t)))
     617     11320636 :     return is_gimple_val (t);
     618              :   else
     619      3519166 :     return (is_gimple_val (t)
     620      1777722 :             || is_gimple_lvalue (t)
     621      1245401 :             || (TREE_CODE (t) == CONSTRUCTOR && CONSTRUCTOR_NELTS (t) == 0)
     622      4764157 :             || TREE_CODE (t) == CALL_EXPR);
     623              : }
     624              : 
     625              : /* Create a temporary with a name derived from VAL.  Subroutine of
     626              :    lookup_tmp_var; nobody else should call this function.  */
     627              : 
     628              : static inline tree
     629      2238241 : create_tmp_from_val (tree val)
     630              : {
     631              :   /* Drop all qualifiers and address-space information from the value type.  */
     632      2238241 :   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
     633      2238241 :   tree var = create_tmp_var (type, get_name (val));
     634      2238241 :   return var;
     635              : }
     636              : 
     637              : /* Create a temporary to hold the value of VAL.  If IS_FORMAL, try to reuse
     638              :    an existing expression temporary.  If NOT_GIMPLE_REG, mark it as such.  */
     639              : 
     640              : static tree
     641      2480589 : lookup_tmp_var (tree val, bool is_formal, bool not_gimple_reg)
     642              : {
     643      2480589 :   tree ret;
     644              : 
     645              :   /* We cannot mark a formal temporary with DECL_NOT_GIMPLE_REG_P.  */
     646      2480589 :   gcc_assert (!is_formal || !not_gimple_reg);
     647              : 
     648              :   /* If not optimizing, never really reuse a temporary.  local-alloc
     649              :      won't allocate any variable that is used in more than one basic
     650              :      block, which means it will go into memory, causing much extra
     651              :      work in reload and final and poorer code generation, outweighing
     652              :      the extra memory allocation here.  */
     653      2480589 :   if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
     654              :     {
     655      1111539 :       ret = create_tmp_from_val (val);
     656      1111539 :       DECL_NOT_GIMPLE_REG_P (ret) = not_gimple_reg;
     657              :     }
     658              :   else
     659              :     {
     660      1369050 :       elt_t elt, *elt_p;
     661      1369050 :       elt_t **slot;
     662              : 
     663      1369050 :       elt.val = val;
     664      1369050 :       if (!gimplify_ctxp->temp_htab)
     665       437081 :         gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
     666      1369050 :       slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
     667      1369050 :       if (*slot == NULL)
     668              :         {
     669      1126702 :           elt_p = XNEW (elt_t);
     670      1126702 :           elt_p->val = val;
     671      1126702 :           elt_p->temp = ret = create_tmp_from_val (val);
     672      1126702 :           *slot = elt_p;
     673              :         }
     674              :       else
     675              :         {
     676       242348 :           elt_p = *slot;
     677       242348 :           ret = elt_p->temp;
     678              :         }
     679              :     }
     680              : 
     681      2480589 :   return ret;
     682              : }
     683              : 
     684              : /* Helper for get_formal_tmp_var and get_initialized_tmp_var.  */
     685              : 
     686              : static tree
     687     27310809 : internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
     688              :                       bool is_formal, bool allow_ssa, bool not_gimple_reg)
     689              : {
     690     27310809 :   tree t, mod;
     691              : 
     692              :   /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
     693              :      can create an INIT_EXPR and convert it into a GIMPLE_CALL below.  */
     694     27310809 :   gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
     695              :                  fb_rvalue);
     696              : 
     697     27310809 :   if (allow_ssa
     698     27039533 :       && gimplify_ctxp->into_ssa
     699     52833033 :       && is_gimple_reg_type (TREE_TYPE (val)))
     700              :     {
     701     24830221 :       t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
     702     24830221 :       if (! gimple_in_ssa_p (cfun))
     703              :         {
     704     21382869 :           const char *name = get_name (val);
     705     21382869 :           if (name)
     706      6902886 :             SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
     707              :         }
     708              :     }
     709              :   else
     710      2480588 :     t = lookup_tmp_var (val, is_formal, not_gimple_reg);
     711              : 
     712     27310809 :   mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
     713              : 
     714     27310809 :   SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
     715              : 
     716              :   /* gimplify_modify_expr might want to reduce this further.  */
     717     27310809 :   gimplify_and_add (mod, pre_p);
     718     27310809 :   ggc_free (mod);
     719              : 
     720              :   /* If we failed to gimplify VAL then we can end up with the temporary
     721              :      SSA name not having a definition.  In this case return a decl.  */
     722     27310809 :   if (TREE_CODE (t) == SSA_NAME && ! SSA_NAME_DEF_STMT (t))
     723            1 :     return lookup_tmp_var (val, is_formal, not_gimple_reg);
     724              : 
     725              :   return t;
     726              : }
     727              : 
     728              : /* Return a formal temporary variable initialized with VAL.  PRE_P is as
     729              :    in gimplify_expr.  Only use this function if:
     730              : 
     731              :    1) The value of the unfactored expression represented by VAL will not
     732              :       change between the initialization and use of the temporary, and
     733              :    2) The temporary will not be otherwise modified.
     734              : 
     735              :    For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
     736              :    and #2 means it is inappropriate for && temps.
     737              : 
     738              :    For other cases, use get_initialized_tmp_var instead.  */
     739              : 
     740              : tree
     741     26806873 : get_formal_tmp_var (tree val, gimple_seq *pre_p)
     742              : {
     743     26806873 :   return internal_get_tmp_var (val, pre_p, NULL, true, true, false);
     744              : }
     745              : 
     746              : /* Return a temporary variable initialized with VAL.  PRE_P and POST_P
     747              :    are as in gimplify_expr.  */
     748              : 
     749              : tree
     750       491790 : get_initialized_tmp_var (tree val, gimple_seq *pre_p,
     751              :                          gimple_seq *post_p /* = NULL */,
     752              :                          bool allow_ssa /* = true */)
     753              : {
     754       491790 :   return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa, false);
     755              : }
     756              : 
     757              : /* Declare all the variables in VARS in SCOPE.  If DEBUG_INFO is true,
     758              :    generate debug info for them; otherwise don't.  */
     759              : 
     760              : void
     761      3159544 : declare_vars (tree vars, gimple *gs, bool debug_info)
     762              : {
     763      3159544 :   tree last = vars;
     764      3159544 :   if (last)
     765              :     {
     766      1985549 :       tree temps, block;
     767              : 
     768      1985549 :       gbind *scope = as_a <gbind *> (gs);
     769              : 
     770      1985549 :       temps = nreverse (last);
     771              : 
     772      1985549 :       block = gimple_bind_block (scope);
     773      1985549 :       gcc_assert (!block || TREE_CODE (block) == BLOCK);
     774      1985549 :       if (!block || !debug_info)
     775              :         {
     776      1981611 :           DECL_CHAIN (last) = gimple_bind_vars (scope);
     777      1981611 :           gimple_bind_set_vars (scope, temps);
     778              :         }
     779              :       else
     780              :         {
     781              :           /* We need to attach the nodes both to the BIND_EXPR and to its
     782              :              associated BLOCK for debugging purposes.  The key point here
     783              :              is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
     784              :              is a subchain of the BIND_EXPR_VARS of the BIND_EXPR.  */
     785         3938 :           if (BLOCK_VARS (block))
     786         3733 :             BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
     787              :           else
     788              :             {
     789          205 :               gimple_bind_set_vars (scope,
     790              :                                     chainon (gimple_bind_vars (scope), temps));
     791          205 :               BLOCK_VARS (block) = temps;
     792              :             }
     793              :         }
     794              :     }
     795      3159544 : }
     796              : 
     797              : /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
     798              :    for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly.  Abort if
     799              :    no such upper bound can be obtained.  */
     800              : 
     801              : static void
     802            0 : force_constant_size (tree var)
     803              : {
     804              :   /* The only attempt we make is by querying the maximum size of objects
     805              :      of the variable's type.  */
     806              : 
     807            0 :   HOST_WIDE_INT max_size;
     808              : 
     809            0 :   gcc_assert (VAR_P (var));
     810              : 
     811            0 :   max_size = max_int_size_in_bytes (TREE_TYPE (var));
     812              : 
     813            0 :   gcc_assert (max_size >= 0);
     814              : 
     815            0 :   DECL_SIZE_UNIT (var)
     816            0 :     = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
     817            0 :   DECL_SIZE (var)
     818            0 :     = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
     819            0 : }
     820              : 
     821              : /* Push the temporary variable TMP into the current binding.  */
     822              : 
     823              : void
     824        30086 : gimple_add_tmp_var_fn (struct function *fn, tree tmp)
     825              : {
     826        30086 :   gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
     827              : 
     828              :   /* Later processing assumes that the object size is constant, which might
     829              :      not be true at this point.  Force the use of a constant upper bound in
     830              :      this case.  */
     831        30086 :   if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
     832            0 :     force_constant_size (tmp);
     833              : 
     834        30086 :   DECL_CONTEXT (tmp) = fn->decl;
     835        30086 :   DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
     836              : 
     837        30086 :   record_vars_into (tmp, fn->decl);
     838        30086 : }
     839              : 
     840              : /* Push the temporary variable TMP into the current binding.  */
     841              : 
     842              : void
     843     16928407 : gimple_add_tmp_var (tree tmp)
     844              : {
     845     16928407 :   gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
     846              : 
     847              :   /* Later processing assumes that the object size is constant, which might
     848              :      not be true at this point.  Force the use of a constant upper bound in
     849              :      this case.  */
     850     16928407 :   if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
     851            0 :     force_constant_size (tmp);
     852              : 
     853     16928407 :   DECL_CONTEXT (tmp) = current_function_decl;
     854     16928407 :   DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
     855              : 
     856     16928407 :   if (gimplify_ctxp)
     857              :     {
     858      5877152 :       DECL_CHAIN (tmp) = gimplify_ctxp->temps;
     859      5877152 :       gimplify_ctxp->temps = tmp;
     860              : 
     861              :       /* Mark temporaries local within the nearest enclosing parallel.  */
     862      5877152 :       if (gimplify_omp_ctxp)
     863              :         {
     864              :           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
     865       585726 :           int flag = GOVD_LOCAL | GOVD_SEEN;
     866              :           while (ctx
     867       585726 :                  && (ctx->region_type == ORT_WORKSHARE
     868              :                      || ctx->region_type == ORT_TASKGROUP
     869       428007 :                      || ctx->region_type == ORT_SIMD
     870       368654 :                      || ctx->region_type == ORT_ACC))
     871              :             {
     872       223234 :               if (ctx->region_type == ORT_SIMD
     873        59353 :                   && TREE_ADDRESSABLE (tmp)
     874           95 :                   && !TREE_STATIC (tmp))
     875              :                 {
     876           95 :                   if (TREE_CODE (DECL_SIZE_UNIT (tmp)) != INTEGER_CST)
     877            0 :                     ctx->add_safelen1 = true;
     878           95 :                   else if (ctx->in_for_exprs)
     879              :                     flag = GOVD_PRIVATE;
     880              :                   else
     881              :                     flag = GOVD_PRIVATE | GOVD_SEEN;
     882              :                   break;
     883              :                 }
     884       223139 :               ctx = ctx->outer_context;
     885              :             }
     886       362492 :           if (ctx)
     887       334806 :             omp_add_variable (ctx, tmp, flag);
     888              :         }
     889              :     }
     890     11051255 :   else if (cfun)
     891     11051255 :     record_vars (tmp);
     892              :   else
     893              :     {
     894            0 :       gimple_seq body_seq;
     895              : 
     896              :       /* This case is for nested functions.  We need to expose the locals
     897              :          they create.  */
     898            0 :       body_seq = gimple_body (current_function_decl);
     899            0 :       declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
     900              :     }
     901     16928407 : }
     902              : 
     903              : /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
     904              :    any nested functions.  */
     905              : 
     906              : static void
     907      3048124 : unshare_body (tree fndecl)
     908              : {
     909      3048124 :   struct cgraph_node *cgn = cgraph_node::get (fndecl);
     910              :   /* If the language requires deep unsharing, we need a pointer set to make
     911              :      sure we don't repeatedly unshare subtrees of unshareable nodes.  */
     912      3048124 :   hash_set<tree> *visited
     913      3048124 :     = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
     914              : 
     915      3048124 :   copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
     916      3048124 :   copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
     917      3048124 :   copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
     918              : 
     919      3052320 :   delete visited;
     920              : 
     921      3048124 :   if (cgn)
     922      6143002 :     for (cgn = first_nested_function (cgn); cgn;
     923        27594 :          cgn = next_nested_function (cgn))
     924        27594 :       unshare_body (cgn->decl);
     925      3048124 : }
     926              : 
     927              : /* Callback for walk_tree to unmark the visited trees rooted at *TP.
     928              :    Subtrees are walked until the first unvisited node is encountered.  */
     929              : 
     930              : static tree
     931    290582921 : unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
     932              : {
     933    290582921 :   tree t = *tp;
     934              : 
     935              :   /* If this node has been visited, unmark it and keep looking.  */
     936    290582921 :   if (TREE_VISITED (t))
     937    203834287 :     TREE_VISITED (t) = 0;
     938              : 
     939              :   /* Otherwise, don't look any deeper.  */
     940              :   else
     941     86748634 :     *walk_subtrees = 0;
     942              : 
     943    290582921 :   return NULL_TREE;
     944              : }
     945              : 
     946              : /* Unmark the visited trees rooted at *TP.  */
     947              : 
     948              : static inline void
     949      9144372 : unmark_visited (tree *tp)
     950              : {
     951      9144372 :   walk_tree (tp, unmark_visited_r, NULL, NULL);
     952      9144372 : }
     953              : 
     954              : /* Likewise, but mark all trees as not visited.  */
     955              : 
     956              : static void
     957      3048124 : unvisit_body (tree fndecl)
     958              : {
     959      3048124 :   struct cgraph_node *cgn = cgraph_node::get (fndecl);
     960              : 
     961      3048124 :   unmark_visited (&DECL_SAVED_TREE (fndecl));
     962      3048124 :   unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
     963      3048124 :   unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
     964              : 
     965      3048124 :   if (cgn)
     966      6115408 :     for (cgn = first_nested_function (cgn);
     967      3071501 :          cgn; cgn = next_nested_function (cgn))
     968        27594 :       unvisit_body (cgn->decl);
     969      3048124 : }
     970              : 
     971              : /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
     972              :    one, OR_ELSE otherwise.  The location of a STATEMENT_LISTs
     973              :    comprising at least one DEBUG_BEGIN_STMT followed by exactly one
     974              :    EXPR is the location of the EXPR.  */
     975              : 
     976              : static location_t
     977      1211429 : rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
     978              : {
     979      1211429 :   if (!expr)
     980              :     return or_else;
     981              : 
     982      1211429 :   if (EXPR_HAS_LOCATION (expr))
     983       845245 :     return EXPR_LOCATION (expr);
     984              : 
     985       366184 :   if (TREE_CODE (expr) != STATEMENT_LIST)
     986              :     return or_else;
     987              : 
     988            0 :   tree_stmt_iterator i = tsi_start (expr);
     989              : 
     990            0 :   bool found = false;
     991            0 :   while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
     992              :     {
     993            0 :       found = true;
     994            0 :       tsi_next (&i);
     995              :     }
     996              : 
     997            0 :   if (!found || !tsi_one_before_end_p (i))
     998              :     return or_else;
     999              : 
    1000            0 :   return rexpr_location (tsi_stmt (i), or_else);
    1001              : }
    1002              : 
    1003              : /* Return TRUE iff EXPR (maybe recursively) has a location; see
    1004              :    rexpr_location for the potential recursion.  */
    1005              : 
    1006              : static inline bool
    1007       530945 : rexpr_has_location (tree expr)
    1008              : {
    1009       321253 :   return rexpr_location (expr) != UNKNOWN_LOCATION;
    1010              : }
    1011              : 
    1012              : 
    1013              : /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
    1014              :    contain statements and have a value.  Assign its value to a temporary
    1015              :    and give it void_type_node.  Return the temporary, or NULL_TREE if
    1016              :    WRAPPER was already void.  */
    1017              : 
    1018              : tree
    1019     20857740 : voidify_wrapper_expr (tree wrapper, tree temp)
    1020              : {
    1021     20857740 :   tree type = TREE_TYPE (wrapper);
    1022     20857740 :   if (type && !VOID_TYPE_P (type))
    1023              :     {
    1024              :       tree *p;
    1025              : 
    1026              :       /* Set p to point to the body of the wrapper.  Loop until we find
    1027              :          something that isn't a wrapper.  */
    1028       795986 :       for (p = &wrapper; p && *p; )
    1029              :         {
    1030       795986 :           switch (TREE_CODE (*p))
    1031              :             {
    1032         3148 :             case BIND_EXPR:
    1033         3148 :               TREE_SIDE_EFFECTS (*p) = 1;
    1034         3148 :               TREE_TYPE (*p) = void_type_node;
    1035              :               /* For a BIND_EXPR, the body is operand 1.  */
    1036         3148 :               p = &BIND_EXPR_BODY (*p);
    1037         3148 :               break;
    1038              : 
    1039       368363 :             case CLEANUP_POINT_EXPR:
    1040       368363 :             case TRY_FINALLY_EXPR:
    1041       368363 :             case TRY_CATCH_EXPR:
    1042       368363 :               TREE_SIDE_EFFECTS (*p) = 1;
    1043       368363 :               TREE_TYPE (*p) = void_type_node;
    1044       368363 :               p = &TREE_OPERAND (*p, 0);
    1045       368363 :               break;
    1046              : 
    1047        17987 :             case STATEMENT_LIST:
    1048        17987 :               {
    1049        17987 :                 tree_stmt_iterator i = tsi_last (*p);
    1050        17987 :                 TREE_SIDE_EFFECTS (*p) = 1;
    1051        17987 :                 TREE_TYPE (*p) = void_type_node;
    1052        17987 :                 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
    1053              :               }
    1054        17987 :               break;
    1055              : 
    1056              :             case COMPOUND_EXPR:
    1057              :               /* Advance to the last statement.  Set all container types to
    1058              :                  void.  */
    1059        63951 :               for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
    1060              :                 {
    1061        31983 :                   TREE_SIDE_EFFECTS (*p) = 1;
    1062        31983 :                   TREE_TYPE (*p) = void_type_node;
    1063              :                 }
    1064              :               break;
    1065              : 
    1066           76 :             case TRANSACTION_EXPR:
    1067           76 :               TREE_SIDE_EFFECTS (*p) = 1;
    1068           76 :               TREE_TYPE (*p) = void_type_node;
    1069           76 :               p = &TRANSACTION_EXPR_BODY (*p);
    1070           76 :               break;
    1071              : 
    1072       374444 :             default:
    1073              :               /* Assume that any tree upon which voidify_wrapper_expr is
    1074              :                  directly called is a wrapper, and that its body is op0.  */
    1075       374444 :               if (p == &wrapper)
    1076              :                 {
    1077           33 :                   TREE_SIDE_EFFECTS (*p) = 1;
    1078           33 :                   TREE_TYPE (*p) = void_type_node;
    1079           33 :                   p = &TREE_OPERAND (*p, 0);
    1080           33 :                   break;
    1081              :                 }
    1082       374411 :               goto out;
    1083              :             }
    1084              :         }
    1085              : 
    1086            0 :     out:
    1087       374411 :       if (p == NULL || IS_EMPTY_STMT (*p))
    1088              :         temp = NULL_TREE;
    1089       374411 :       else if (temp)
    1090              :         {
    1091              :           /* The wrapper is on the RHS of an assignment that we're pushing
    1092              :              down.  */
    1093         2048 :           gcc_assert (TREE_CODE (temp) == INIT_EXPR
    1094              :                       || TREE_CODE (temp) == MODIFY_EXPR);
    1095         2048 :           TREE_OPERAND (temp, 1) = *p;
    1096         2048 :           *p = temp;
    1097              :         }
    1098              :       else
    1099              :         {
    1100       372363 :           temp = create_tmp_var (type, "retval");
    1101       372363 :           *p = build2 (INIT_EXPR, type, temp, *p);
    1102              :         }
    1103              : 
    1104              :       return temp;
    1105              :     }
    1106              : 
    1107              :   return NULL_TREE;
    1108              : }
    1109              : 
    1110              : /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
    1111              :    a temporary through which they communicate.  */
    1112              : 
    1113              : static void
    1114         7945 : build_stack_save_restore (gcall **save, gcall **restore)
    1115              : {
    1116         7945 :   tree tmp_var;
    1117              : 
    1118        15890 :   *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
    1119         7945 :   tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
    1120         7945 :   gimple_call_set_lhs (*save, tmp_var);
    1121              : 
    1122         7945 :   *restore
    1123         7945 :     = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
    1124              :                          1, tmp_var);
    1125         7945 : }
    1126              : 
    1127              : /* Generate IFN_ASAN_MARK call that poisons shadow memory of the DECL
    1128              :    variable.  */
    1129              : 
    1130              : static tree
    1131          472 : build_asan_poison_call_expr (tree decl)
    1132              : {
    1133              :   /* Do not poison variables that have size equal to zero.  */
    1134          472 :   tree unit_size = DECL_SIZE_UNIT (decl);
    1135          472 :   if (zerop (unit_size))
    1136              :     return NULL_TREE;
    1137              : 
    1138          472 :   tree base = build_fold_addr_expr (decl);
    1139              : 
    1140          472 :   return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
    1141              :                                        void_type_node, 3,
    1142              :                                        build_int_cst (integer_type_node,
    1143              :                                                       ASAN_MARK_POISON),
    1144              :                                        base, unit_size);
    1145              : }
    1146              : 
    1147              : /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
    1148              :    on POISON flag, shadow memory of a DECL variable.  The call will be
    1149              :    put on location identified by IT iterator, where BEFORE flag drives
    1150              :    position where the stmt will be put.  */
    1151              : 
    1152              : static void
    1153         5095 : asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
    1154              :                       bool before)
    1155              : {
    1156         5095 :   tree unit_size = DECL_SIZE_UNIT (decl);
    1157         5095 :   tree base = build_fold_addr_expr (decl);
    1158              : 
    1159              :   /* Do not poison variables that have size equal to zero.  */
    1160         5095 :   if (zerop (unit_size))
    1161         5095 :     return;
    1162              : 
    1163              :   /* It's necessary to have all stack variables aligned to ASAN granularity
    1164              :      bytes.  */
    1165         5081 :   gcc_assert (!hwassist_sanitize_p () || hwassist_sanitize_stack_p ());
    1166         5081 :   unsigned shadow_granularity
    1167         5081 :     = (hwassist_sanitize_p ()
    1168         5081 :        ? HWASAN_TAG_GRANULE_SIZE : ASAN_SHADOW_GRANULARITY);
    1169         5081 :   if (DECL_ALIGN_UNIT (decl) <= shadow_granularity)
    1170         4879 :     SET_DECL_ALIGN (decl, BITS_PER_UNIT * shadow_granularity);
    1171              : 
    1172         5081 :   HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
    1173              : 
    1174         5081 :   gimple *g
    1175         5081 :     = gimple_build_call_internal (IFN_ASAN_MARK, 3,
    1176         5081 :                                   build_int_cst (integer_type_node, flags),
    1177              :                                   base, unit_size);
    1178              : 
    1179         5081 :   if (before)
    1180         2652 :     gsi_insert_before (it, g, GSI_NEW_STMT);
    1181              :   else
    1182         2429 :     gsi_insert_after (it, g, GSI_NEW_STMT);
    1183              : }
    1184              : 
    1185              : /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
    1186              :    either poisons or unpoisons a DECL.  Created statement is appended
    1187              :    to SEQ_P gimple sequence.  */
    1188              : 
    1189              : static void
    1190         4623 : asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
    1191              : {
    1192         4623 :   gimple_stmt_iterator it = gsi_last (*seq_p);
    1193         4623 :   bool before = false;
    1194              : 
    1195         4623 :   if (gsi_end_p (it))
    1196         2443 :     before = true;
    1197              : 
    1198         4623 :   asan_poison_variable (decl, poison, &it, before);
    1199         4623 : }
    1200              : 
    1201              : /* Sort pair of VAR_DECLs A and B by DECL_UID.  */
    1202              : 
    1203              : static int
    1204          135 : sort_by_decl_uid (const void *a, const void *b)
    1205              : {
    1206          135 :   const tree *t1 = (const tree *)a;
    1207          135 :   const tree *t2 = (const tree *)b;
    1208              : 
    1209          135 :   int uid1 = DECL_UID (*t1);
    1210          135 :   int uid2 = DECL_UID (*t2);
    1211              : 
    1212          135 :   if (uid1 < uid2)
    1213              :     return -1;
    1214           54 :   else if (uid1 > uid2)
    1215              :     return 1;
    1216              :   else
    1217            0 :     return 0;
    1218              : }
    1219              : 
    1220              : /* Generate IFN_ASAN_MARK internal call for all VARIABLES
    1221              :    depending on POISON flag.  Created statement is appended
    1222              :    to SEQ_P gimple sequence.  */
    1223              : 
    1224              : static void
    1225      1049368 : asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
    1226              : {
    1227      1049368 :   unsigned c = variables->elements ();
    1228      1049368 :   if (c == 0)
    1229      1049094 :     return;
    1230              : 
    1231          274 :   auto_vec<tree> sorted_variables (c);
    1232              : 
    1233          274 :   for (hash_set<tree>::iterator it = variables->begin ();
    1234          569 :        it != variables->end (); ++it)
    1235          295 :     sorted_variables.safe_push (*it);
    1236              : 
    1237          274 :   sorted_variables.qsort (sort_by_decl_uid);
    1238              : 
    1239          274 :   unsigned i;
    1240          274 :   tree var;
    1241          843 :   FOR_EACH_VEC_ELT (sorted_variables, i, var)
    1242              :     {
    1243          295 :       asan_poison_variable (var, poison, seq_p);
    1244              : 
    1245              :       /* Add use_after_scope_memory attribute for the variable in order
    1246              :          to prevent re-written into SSA.  */
    1247          295 :       if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
    1248          295 :                              DECL_ATTRIBUTES (var)))
    1249           97 :         DECL_ATTRIBUTES (var)
    1250          194 :           = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
    1251              :                        integer_one_node,
    1252           97 :                        DECL_ATTRIBUTES (var));
    1253              :     }
    1254          274 : }
    1255              : 
    1256              : /* Gimplify a BIND_EXPR.  Just voidify and recurse.  */
    1257              : 
    1258              : static enum gimplify_status
    1259      6121620 : gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
    1260              : {
    1261      6121620 :   tree bind_expr = *expr_p;
    1262      6121620 :   bool old_keep_stack = gimplify_ctxp->keep_stack;
    1263      6121620 :   bool old_save_stack = gimplify_ctxp->save_stack;
    1264      6121620 :   tree t;
    1265      6121620 :   gbind *bind_stmt;
    1266      6121620 :   gimple_seq body, cleanup;
    1267      6121620 :   gcall *stack_save;
    1268      6121620 :   location_t start_locus = 0, end_locus = 0;
    1269      6121620 :   tree ret_clauses = NULL;
    1270              : 
    1271      6121620 :   tree temp = voidify_wrapper_expr (bind_expr, NULL);
    1272              : 
    1273              :   /* Mark variables seen in this bind expr.  */
    1274     13241082 :   for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
    1275              :     {
    1276      7119462 :       if (VAR_P (t))
    1277              :         {
    1278      6437390 :           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
    1279      6437390 :           tree attr;
    1280              : 
    1281      6437390 :           if (flag_openmp
    1282       271047 :               && !is_global_var (t)
    1283       261932 :               && !TREE_STATIC (t)
    1284       261932 :               && DECL_CONTEXT (t) == current_function_decl
    1285       261932 :               && TREE_USED (t)
    1286       259820 :               && (attr = lookup_attribute ("omp allocate", DECL_ATTRIBUTES (t)))
    1287              :                  != NULL_TREE
    1288              :               /* In C++, bad attributes are marked instead of removed.  */
    1289      6438667 :               && TREE_PURPOSE (TREE_VALUE (attr)) != error_mark_node)
    1290              :             {
    1291         1253 :               gcc_assert (!DECL_HAS_VALUE_EXPR_P (t));
    1292         1253 :               tree alloc = TREE_PURPOSE (TREE_VALUE (attr));
    1293         1253 :               tree align = TREE_VALUE (TREE_VALUE (attr));
    1294              :               /* The C++ front end smuggles a location through the chain field,
    1295              :                  this conflicts with Fortran handling, clear it.  */
    1296         1253 :               if (TREE_CHAIN (TREE_VALUE (attr)) != NULL_TREE
    1297         1253 :                   && TREE_CODE (TREE_CHAIN (TREE_VALUE (attr))) == NOP_EXPR)
    1298         1175 :                 TREE_CHAIN (TREE_VALUE (attr)) = NULL_TREE;
    1299              : 
    1300              :               /* Allocate directives that appear in a target region must specify
    1301              :                  an allocator clause unless a requires directive with the
    1302              :                  dynamic_allocators clause is present in the same compilation
    1303              :                  unit.  */
    1304         1253 :               bool missing_dyn_alloc = false;
    1305         1253 :               if ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS) == 0)
    1306              :                 {
    1307              :                   /* This comes too early for omp_discover_declare_target...,
    1308              :                      but should at least catch the most common cases.  */
    1309         1241 :                   missing_dyn_alloc
    1310         1241 :                     = (alloc == NULL_TREE
    1311         1241 :                        && cgraph_node::get (current_function_decl)->offloadable);
    1312         1241 :                   for (struct gimplify_omp_ctx *ctx2 = ctx;
    1313         1251 :                        ctx2 && !missing_dyn_alloc; ctx2 = ctx2->outer_context)
    1314           29 :                     if (ctx2->code == OMP_TARGET)
    1315              :                       {
    1316           19 :                         if (alloc == NULL_TREE)
    1317              :                           missing_dyn_alloc = true;
    1318           14 :                         else if (TREE_CODE (alloc) != INTEGER_CST)
    1319              :                           {
    1320            4 :                             tree alloc2 = alloc;
    1321            4 :                             if (TREE_CODE (alloc2) == MEM_REF
    1322            4 :                                 || TREE_CODE (alloc2) == INDIRECT_REF)
    1323            0 :                               alloc2 = TREE_OPERAND (alloc2, 0);
    1324            4 :                             tree c2;
    1325            4 :                             for (c2 = ctx2->clauses; c2;
    1326            0 :                                  c2 = OMP_CLAUSE_CHAIN (c2))
    1327            2 :                               if (OMP_CLAUSE_CODE (c2)
    1328              :                                   == OMP_CLAUSE_USES_ALLOCATORS)
    1329              :                                 {
    1330            2 :                                   tree t2
    1331            2 :                                     = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c2);
    1332            2 :                                   if (operand_equal_p (alloc2, t2))
    1333              :                                     break;
    1334              :                                 }
    1335            4 :                             if (c2 == NULL_TREE)
    1336            2 :                               error_at (EXPR_LOC_OR_LOC (
    1337              :                                           alloc, DECL_SOURCE_LOCATION (t)),
    1338              :                                         "%qE in %<allocator%> clause inside a "
    1339              :                                         "target region must be specified in an "
    1340              :                                         "%<uses_allocators%> clause on the "
    1341              :                                         "%<target%> directive", alloc2);
    1342              :                           }
    1343              :                         break;
    1344              :                       }
    1345              :                 }
    1346         1236 :               if (missing_dyn_alloc)
    1347           10 :                 error_at (DECL_SOURCE_LOCATION (t),
    1348              :                           "%<allocate%> directive for %qD inside a target "
    1349              :                           "region must specify an %<allocator%> clause", t);
    1350              :               /* Skip for omp_default_mem_alloc (= 1),
    1351              :                  unless align is present.  For C/C++, there should be always a
    1352              :                  statement list following if TREE_USED, except for, e.g., using
    1353              :                  this decl in a static_assert; in that case, only a single
    1354              :                  DECL_EXPR remains, which can be skipped here.  */
    1355         1243 :               else if (!errorcount
    1356         1186 :                        && (align != NULL_TREE
    1357         1186 :                            || alloc == NULL_TREE
    1358          758 :                            || !integer_onep (alloc))
    1359         2362 :                        && (lang_GNU_Fortran ()
    1360         1091 :                            || (TREE_CODE (BIND_EXPR_BODY (bind_expr))
    1361              :                                != DECL_EXPR)))
    1362              :                 {
    1363              :                   /* Fortran might already use a pointer type internally;
    1364              :                      use that pointer except for type(C_ptr) and type(C_funptr);
    1365              :                      note that normal proc pointers are rejected.  */
    1366         1119 :                   tree type = TREE_TYPE (t);
    1367         1119 :                   tree tmp, v;
    1368         1119 :                   if (lang_GNU_Fortran ()
    1369           28 :                       && POINTER_TYPE_P (type)
    1370            8 :                       && TREE_TYPE (type) != void_type_node
    1371         1125 :                       && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
    1372              :                     {
    1373            6 :                       type = TREE_TYPE (type);
    1374            6 :                       v = t;
    1375              :                     }
    1376              :                   else
    1377              :                     {
    1378         1113 :                       tmp = build_pointer_type (type);
    1379         1113 :                       v = create_tmp_var (tmp, get_name (t));
    1380         1113 :                       DECL_IGNORED_P (v) = 0;
    1381         1113 :                       DECL_ATTRIBUTES (v)
    1382         1113 :                         = tree_cons (get_identifier ("omp allocate var"),
    1383              :                                      build_tree_list (NULL_TREE, t),
    1384              :                                      remove_attribute ("omp allocate",
    1385         1113 :                                                        DECL_ATTRIBUTES (t)));
    1386         1113 :                       tmp = build_fold_indirect_ref (v);
    1387         1113 :                       TREE_THIS_NOTRAP (tmp) = 1;
    1388         1113 :                       SET_DECL_VALUE_EXPR (t, tmp);
    1389         1113 :                       DECL_HAS_VALUE_EXPR_P (t) = 1;
    1390              :                     }
    1391         1119 :                   tree sz = TYPE_SIZE_UNIT (type);
    1392              :                   /* The size to use in Fortran might not match TYPE_SIZE_UNIT;
    1393              :                      hence, for some decls, a size variable is saved in the
    1394              :                      attributes; use it, if available.  */
    1395         1119 :                   if (TREE_CHAIN (TREE_VALUE (attr))
    1396           28 :                       && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)))
    1397         1125 :                       && TREE_PURPOSE (
    1398              :                            TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)))))
    1399              :                     {
    1400            6 :                       sz = TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)));
    1401            6 :                       sz = TREE_PURPOSE (sz);
    1402              :                     }
    1403         1119 :                   if (alloc == NULL_TREE)
    1404          227 :                     alloc = build_zero_cst (ptr_type_node);
    1405         1119 :                   if (align == NULL_TREE)
    1406          801 :                     align = build_int_cst (size_type_node, DECL_ALIGN_UNIT (t));
    1407              :                   else
    1408          318 :                     align = build_int_cst (size_type_node,
    1409          318 :                                            MAX (tree_to_uhwi (align),
    1410              :                                                 DECL_ALIGN_UNIT (t)));
    1411         1119 :                   location_t loc = DECL_SOURCE_LOCATION (t);
    1412         1119 :                   tmp = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
    1413         1119 :                   tmp = build_call_expr_loc (loc, tmp, 3, align, sz, alloc);
    1414         1119 :                   tmp = fold_build2_loc (loc, MODIFY_EXPR, TREE_TYPE (v), v,
    1415         1119 :                                          fold_convert (TREE_TYPE (v), tmp));
    1416         1119 :                   gcc_assert (BIND_EXPR_BODY (bind_expr) != NULL_TREE);
    1417              :                   /* Ensure that either TREE_CHAIN (TREE_VALUE (attr) is set
    1418              :                      and GOMP_FREE added here or that DECL_HAS_VALUE_EXPR_P (t)
    1419              :                      is set, using in a condition much further below.  */
    1420         1125 :                   gcc_assert (DECL_HAS_VALUE_EXPR_P (t)
    1421              :                               || TREE_CHAIN (TREE_VALUE (attr)));
    1422         1119 :                   if (TREE_CHAIN (TREE_VALUE (attr)))
    1423              :                     {
    1424              :                       /* Fortran is special as it does not have properly nest
    1425              :                          declarations in blocks.  And as there is no
    1426              :                          initializer, there is also no expression to look for.
    1427              :                          Hence, the FE makes the statement list of the
    1428              :                          try-finally block available. We can put the GOMP_alloc
    1429              :                          at the top, unless an allocator or size expression
    1430              :                          requires to put it afterward; note that the size is
    1431              :                          always later in generated code; for strings, no
    1432              :                          size expr but still an expr might be available.
    1433              :                          As LTO does not handle a statement list, 'sl' has
    1434              :                          to be removed; done so by removing the attribute.  */
    1435           28 :                       DECL_ATTRIBUTES (t)
    1436           28 :                         = remove_attribute ("omp allocate",
    1437           28 :                                             DECL_ATTRIBUTES (t));
    1438           28 :                       tree sl = TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)));
    1439           28 :                       tree_stmt_iterator e = tsi_start (sl);
    1440           28 :                       tree needle = NULL_TREE;
    1441           28 :                       if (TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr))))
    1442              :                         {
    1443            6 :                           needle = TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)));
    1444            6 :                           needle = (TREE_VALUE (needle) ? TREE_VALUE (needle)
    1445              :                                                         : sz);
    1446              :                         }
    1447           22 :                       else if (TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr))))
    1448              :                         needle = sz;
    1449           22 :                       else if (DECL_P (alloc) && DECL_ARTIFICIAL (alloc))
    1450              :                         needle = alloc;
    1451              : 
    1452           10 :                       if (needle != NULL_TREE)
    1453              :                         {
    1454           49 :                           while (!tsi_end_p (e))
    1455              :                             {
    1456           49 :                               if (*e == needle
    1457           49 :                                   || (TREE_CODE (*e) == MODIFY_EXPR
    1458           49 :                                       && TREE_OPERAND (*e, 0) == needle))
    1459              :                                 break;
    1460           39 :                               ++e;
    1461              :                             }
    1462           10 :                           gcc_assert (!tsi_end_p (e));
    1463              :                         }
    1464           28 :                       tsi_link_after (&e, tmp, TSI_SAME_STMT);
    1465              : 
    1466              :                       /* As the cleanup is in BIND_EXPR_BODY, GOMP_free is added
    1467              :                          here; for C/C++ it will be added in the 'cleanup'
    1468              :                          section after gimplification. But Fortran already has
    1469              :                          a try-finally block.  */
    1470           28 :                       sl = TREE_VALUE (TREE_CHAIN (TREE_VALUE (attr)));
    1471           28 :                       e = tsi_last (sl);
    1472           28 :                       tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
    1473           28 :                       tmp = build_call_expr_loc (EXPR_LOCATION (*e), tmp, 2, v,
    1474              :                                                  build_zero_cst (ptr_type_node));
    1475           28 :                       tsi_link_after (&e, tmp, TSI_SAME_STMT);
    1476           28 :                       tmp = build_clobber (TREE_TYPE (v), CLOBBER_STORAGE_END);
    1477           28 :                       tmp = fold_build2_loc (loc, MODIFY_EXPR, TREE_TYPE (v), v,
    1478           28 :                                              fold_convert (TREE_TYPE (v), tmp));
    1479           28 :                       ++e;
    1480           28 :                       tsi_link_after (&e, tmp, TSI_SAME_STMT);
    1481              :                     }
    1482              :                   else
    1483              :                     {
    1484         1091 :                       gcc_assert (TREE_CODE (BIND_EXPR_BODY (bind_expr))
    1485              :                                   == STATEMENT_LIST);
    1486         1091 :                       tree_stmt_iterator e;
    1487         1091 :                       e = tsi_start (BIND_EXPR_BODY (bind_expr));
    1488         2344 :                       while (!tsi_end_p (e))
    1489              :                         {
    1490         1253 :                           if ((TREE_CODE (*e) == DECL_EXPR
    1491          818 :                                && TREE_OPERAND (*e, 0) == t)
    1492         1309 :                               || (TREE_CODE (*e) == CLEANUP_POINT_EXPR
    1493          373 :                                   && (TREE_CODE (TREE_OPERAND (*e, 0))
    1494              :                                       == DECL_EXPR)
    1495          371 :                                   && (TREE_OPERAND (TREE_OPERAND (*e, 0), 0)
    1496              :                                       == t)))
    1497              :                             break;
    1498          162 :                           ++e;
    1499              :                         }
    1500         1091 :                       gcc_assert (!tsi_end_p (e));
    1501         1091 :                       tsi_link_before (&e, tmp, TSI_SAME_STMT);
    1502              :                    }
    1503              :                 }
    1504              :             }
    1505              : 
    1506              :           /* Mark variable as local.  */
    1507      6437390 :           if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t))
    1508              :             {
    1509       103444 :               if (! DECL_SEEN_IN_BIND_EXPR_P (t)
    1510       103444 :                   || splay_tree_lookup (ctx->variables,
    1511              :                                         (splay_tree_key) t) == NULL)
    1512              :                 {
    1513       103444 :                   int flag = GOVD_LOCAL;
    1514       103444 :                   if (ctx->region_type == ORT_SIMD
    1515         4971 :                       && TREE_ADDRESSABLE (t)
    1516          123 :                       && !TREE_STATIC (t))
    1517              :                     {
    1518          111 :                       if (TREE_CODE (DECL_SIZE_UNIT (t)) != INTEGER_CST)
    1519            2 :                         ctx->add_safelen1 = true;
    1520              :                       else
    1521              :                         flag = GOVD_PRIVATE;
    1522              :                     }
    1523       103444 :                   omp_add_variable (ctx, t, flag | GOVD_SEEN);
    1524              :                 }
    1525              :               /* Static locals inside of target construct or offloaded
    1526              :                  routines need to be "omp declare target".  */
    1527       103444 :               if (TREE_STATIC (t))
    1528         1399 :                 for (; ctx; ctx = ctx->outer_context)
    1529         1214 :                   if ((ctx->region_type & ORT_TARGET) != 0)
    1530              :                     {
    1531          970 :                       if (!lookup_attribute ("omp declare target",
    1532          970 :                                              DECL_ATTRIBUTES (t)))
    1533              :                         {
    1534          958 :                           tree id = get_identifier ("omp declare target");
    1535          958 :                           DECL_ATTRIBUTES (t)
    1536          958 :                             = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
    1537          958 :                           varpool_node *node = varpool_node::get (t);
    1538          958 :                           if (node)
    1539              :                             {
    1540           33 :                               node->offloadable = 1;
    1541           33 :                               if (ENABLE_OFFLOADING && !DECL_EXTERNAL (t))
    1542              :                                 {
    1543              :                                   g->have_offload = true;
    1544              :                                   if (!in_lto_p)
    1545              :                                     vec_safe_push (offload_vars, t);
    1546              :                                 }
    1547              :                             }
    1548              :                         }
    1549              :                       break;
    1550              :                     }
    1551              :             }
    1552              : 
    1553      6437390 :           DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
    1554              : 
    1555      6437390 :           if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
    1556         1096 :             cfun->has_local_explicit_reg_vars = true;
    1557              :         }
    1558              :     }
    1559              : 
    1560     12243240 :   bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
    1561      6121620 :                                  BIND_EXPR_BLOCK (bind_expr));
    1562      6121620 :   gimple_push_bind_expr (bind_stmt);
    1563              : 
    1564      6121620 :   gimplify_ctxp->keep_stack = false;
    1565      6121620 :   gimplify_ctxp->save_stack = false;
    1566              : 
    1567              :   /* Gimplify the body into the GIMPLE_BIND tuple's body.  */
    1568      6121620 :   body = NULL;
    1569      6121620 :   gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
    1570      6121620 :   gimple_bind_set_body (bind_stmt, body);
    1571              : 
    1572              :   /* Source location wise, the cleanup code (stack_restore and clobbers)
    1573              :      belongs to the end of the block, so propagate what we have.  The
    1574              :      stack_save operation belongs to the beginning of block, which we can
    1575              :      infer from the bind_expr directly if the block has no explicit
    1576              :      assignment.  */
    1577      6121620 :   if (BIND_EXPR_BLOCK (bind_expr))
    1578              :     {
    1579      5989672 :       end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
    1580      5989672 :       start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
    1581              :     }
    1582      5989672 :   if (start_locus == 0)
    1583      6121620 :     start_locus = EXPR_LOCATION (bind_expr);
    1584              : 
    1585      6121620 :   cleanup = NULL;
    1586      6121620 :   stack_save = NULL;
    1587              : 
    1588              :   /* Add clobbers for all variables that go out of scope.  */
    1589     13241082 :   for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
    1590              :     {
    1591      7119462 :       if (VAR_P (t)
    1592      6437390 :           && !is_global_var (t)
    1593     13329998 :           && DECL_CONTEXT (t) == current_function_decl)
    1594              :         {
    1595      6210536 :           if (flag_openmp
    1596       261895 :               && DECL_HAS_VALUE_EXPR_P (t)
    1597         2800 :               && TREE_USED (t)
    1598      6213316 :               && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (t)))
    1599              :             {
    1600              :               /* For Fortran, TREE_CHAIN (TREE_VALUE (attr)) is set, which
    1601              :                  causes that the GOMP_free call is already added above;
    1602              :                  and "omp allocate" is removed from DECL_ATTRIBUTES.  */
    1603         1091 :               tree v = TREE_OPERAND (DECL_VALUE_EXPR (t), 0);
    1604         1091 :               tree tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
    1605         1091 :               tmp = build_call_expr_loc (end_locus, tmp, 2, v,
    1606              :                                          build_zero_cst (ptr_type_node));
    1607         1091 :               gimplify_and_add (tmp, &cleanup);
    1608         1091 :               gimple *clobber_stmt;
    1609         1091 :               tmp = build_clobber (TREE_TYPE (v), CLOBBER_STORAGE_END);
    1610         1091 :               clobber_stmt = gimple_build_assign (v, tmp);
    1611         1091 :               gimple_set_location (clobber_stmt, end_locus);
    1612         1091 :               gimplify_seq_add_stmt (&cleanup, clobber_stmt);
    1613              :             }
    1614      6210536 :           if (!DECL_HARD_REGISTER (t)
    1615      6209440 :               && !TREE_THIS_VOLATILE (t)
    1616      6169344 :               && !DECL_HAS_VALUE_EXPR_P (t)
    1617              :               /* Only care for variables that have to be in memory.  Others
    1618              :                  will be rewritten into SSA names, hence moved to the
    1619              :                  top-level.  */
    1620      6039778 :               && !is_gimple_reg (t)
    1621      7513097 :               && flag_stack_reuse != SR_NONE)
    1622              :             {
    1623      1297794 :               tree clobber = build_clobber (TREE_TYPE (t), CLOBBER_STORAGE_END);
    1624      1297794 :               gimple *clobber_stmt;
    1625      1297794 :               clobber_stmt = gimple_build_assign (t, clobber);
    1626      1297794 :               gimple_set_location (clobber_stmt, end_locus);
    1627      1297794 :               gimplify_seq_add_stmt (&cleanup, clobber_stmt);
    1628              :             }
    1629              : 
    1630      6210536 :           if (flag_openacc && oacc_declare_returns != NULL)
    1631              :             {
    1632          207 :               tree key = t;
    1633          207 :               if (DECL_HAS_VALUE_EXPR_P (key))
    1634              :                 {
    1635            8 :                   key = DECL_VALUE_EXPR (key);
    1636            8 :                   if (INDIRECT_REF_P (key))
    1637            8 :                     key = TREE_OPERAND (key, 0);
    1638              :                 }
    1639          207 :               tree *c = oacc_declare_returns->get (key);
    1640          207 :               if (c != NULL)
    1641              :                 {
    1642          116 :                   if (ret_clauses)
    1643           64 :                     OMP_CLAUSE_CHAIN (*c) = ret_clauses;
    1644              : 
    1645          116 :                   ret_clauses = unshare_expr (*c);
    1646              : 
    1647          116 :                   oacc_declare_returns->remove (key);
    1648              : 
    1649          116 :                   if (oacc_declare_returns->is_empty ())
    1650              :                     {
    1651           40 :                       delete oacc_declare_returns;
    1652           40 :                       oacc_declare_returns = NULL;
    1653              :                     }
    1654              :                 }
    1655              :             }
    1656              :         }
    1657              : 
    1658      7119462 :       if (asan_poisoned_variables != NULL
    1659      7119462 :           && asan_poisoned_variables->contains (t))
    1660              :         {
    1661         2164 :           asan_poisoned_variables->remove (t);
    1662         2164 :           asan_poison_variable (t, true, &cleanup);
    1663              :         }
    1664              : 
    1665      7119462 :       if (gimplify_ctxp->live_switch_vars != NULL
    1666      7119462 :           && gimplify_ctxp->live_switch_vars->contains (t))
    1667           55 :         gimplify_ctxp->live_switch_vars->remove (t);
    1668              :     }
    1669              : 
    1670              :   /* If the code both contains VLAs and calls alloca, then we cannot reclaim
    1671              :      the stack space allocated to the VLAs.  */
    1672      6121620 :   if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
    1673              :     {
    1674         7945 :       gcall *stack_restore;
    1675              : 
    1676              :       /* Save stack on entry and restore it on exit.  Add a try_finally
    1677              :          block to achieve this.  */
    1678         7945 :       build_stack_save_restore (&stack_save, &stack_restore);
    1679              : 
    1680         7945 :       gimple_set_location (stack_save, start_locus);
    1681         7945 :       gimple_set_location (stack_restore, end_locus);
    1682              : 
    1683         7945 :       gimplify_seq_add_stmt (&cleanup, stack_restore);
    1684              :     }
    1685              : 
    1686      6121620 :   if (ret_clauses)
    1687              :     {
    1688           52 :       gomp_target *stmt;
    1689           52 :       gimple_stmt_iterator si = gsi_start (cleanup);
    1690              : 
    1691           52 :       stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
    1692              :                                       ret_clauses);
    1693           52 :       gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
    1694              :     }
    1695              : 
    1696      6121620 :   if (cleanup)
    1697              :     {
    1698       798606 :       gtry *gs;
    1699       798606 :       gimple_seq new_body;
    1700              : 
    1701       798606 :       new_body = NULL;
    1702       798606 :       gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
    1703              :                              GIMPLE_TRY_FINALLY);
    1704              : 
    1705       798606 :       if (stack_save)
    1706         7945 :         gimplify_seq_add_stmt (&new_body, stack_save);
    1707       798606 :       gimplify_seq_add_stmt (&new_body, gs);
    1708       798606 :       gimple_bind_set_body (bind_stmt, new_body);
    1709              :     }
    1710              : 
    1711              :   /* keep_stack propagates all the way up to the outermost BIND_EXPR.  */
    1712      6121620 :   if (!gimplify_ctxp->keep_stack)
    1713      6106195 :     gimplify_ctxp->keep_stack = old_keep_stack;
    1714      6121620 :   gimplify_ctxp->save_stack = old_save_stack;
    1715              : 
    1716      6121620 :   gimple_pop_bind_expr ();
    1717              : 
    1718      6121620 :   gimplify_seq_add_stmt (pre_p, bind_stmt);
    1719              : 
    1720      6121620 :   if (temp)
    1721              :     {
    1722         1709 :       *expr_p = temp;
    1723         1709 :       return GS_OK;
    1724              :     }
    1725              : 
    1726              :   *expr_p = NULL_TREE;
    1727              :   return GS_ALL_DONE;
    1728              : }
    1729              : 
    1730              : /* Maybe add early return predict statement to PRE_P sequence.  */
    1731              : 
    1732              : static void
    1733      2303074 : maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
    1734              : {
    1735              :   /* If we are not in a conditional context, add PREDICT statement.  */
    1736      2303074 :   if (gimple_conditional_context ())
    1737              :     {
    1738       449210 :       gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
    1739              :                                               NOT_TAKEN);
    1740       449210 :       gimplify_seq_add_stmt (pre_p, predict);
    1741              :     }
    1742      2303074 : }
    1743              : 
    1744              : /* Gimplify a RETURN_EXPR.  If the expression to be returned is not a
    1745              :    GIMPLE value, it is assigned to a new temporary and the statement is
    1746              :    re-written to return the temporary.
    1747              : 
    1748              :    PRE_P points to the sequence where side effects that must happen before
    1749              :    STMT should be stored.  */
    1750              : 
    1751              : static enum gimplify_status
    1752      2303083 : gimplify_return_expr (tree stmt, gimple_seq *pre_p)
    1753              : {
    1754      2303083 :   greturn *ret;
    1755      2303083 :   tree ret_expr = TREE_OPERAND (stmt, 0);
    1756      2303083 :   tree result_decl, result;
    1757              : 
    1758      2303083 :   if (ret_expr == error_mark_node)
    1759              :     return GS_ERROR;
    1760              : 
    1761      2303074 :   if (!ret_expr
    1762      2211986 :       || TREE_CODE (ret_expr) == RESULT_DECL)
    1763              :     {
    1764       107037 :       maybe_add_early_return_predict_stmt (pre_p);
    1765       107037 :       greturn *ret = gimple_build_return (ret_expr);
    1766       107037 :       copy_warning (ret, stmt);
    1767       107037 :       gimplify_seq_add_stmt (pre_p, ret);
    1768       107037 :       return GS_ALL_DONE;
    1769              :     }
    1770              : 
    1771      2196037 :   if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
    1772              :     result_decl = NULL_TREE;
    1773      2195713 :   else if (TREE_CODE (ret_expr) == COMPOUND_EXPR)
    1774              :     {
    1775              :       /* Used in C++ for handling EH cleanup of the return value if a local
    1776              :          cleanup throws.  Assume the front-end knows what it's doing.  */
    1777         4936 :       result_decl = DECL_RESULT (current_function_decl);
    1778              :       /* But crash if we end up trying to modify ret_expr below.  */
    1779         4936 :       ret_expr = NULL_TREE;
    1780              :     }
    1781              :   else
    1782              :     {
    1783      2190777 :       result_decl = TREE_OPERAND (ret_expr, 0);
    1784              : 
    1785              :       /* See through a return by reference.  */
    1786      2190777 :       if (INDIRECT_REF_P (result_decl))
    1787        43401 :         result_decl = TREE_OPERAND (result_decl, 0);
    1788              : 
    1789      2190777 :       gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
    1790              :                    || TREE_CODE (ret_expr) == INIT_EXPR)
    1791              :                   && TREE_CODE (result_decl) == RESULT_DECL);
    1792              :     }
    1793              : 
    1794              :   /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
    1795              :      Recall that aggregate_value_p is FALSE for any aggregate type that is
    1796              :      returned in registers.  If we're returning values in registers, then
    1797              :      we don't want to extend the lifetime of the RESULT_DECL, particularly
    1798              :      across another call.  In addition, for those aggregates for which
    1799              :      hard_function_value generates a PARALLEL, we'll die during normal
    1800              :      expansion of structure assignments; there's special code in expand_return
    1801              :      to handle this case that does not exist in expand_expr.  */
    1802         4936 :   if (!result_decl)
    1803              :     result = NULL_TREE;
    1804      2195713 :   else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
    1805              :     {
    1806       182266 :       if (!poly_int_tree_p (DECL_SIZE (result_decl)))
    1807              :         {
    1808           48 :           if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
    1809            0 :             gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
    1810              :           /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
    1811              :              should be effectively allocated by the caller, i.e. all calls to
    1812              :              this function must be subject to the Return Slot Optimization.  */
    1813           48 :           gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
    1814           48 :           gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
    1815              :         }
    1816              :       result = result_decl;
    1817              :     }
    1818      2013447 :   else if (gimplify_ctxp->return_temp)
    1819              :     result = gimplify_ctxp->return_temp;
    1820              :   else
    1821              :     {
    1822      1511746 :       result = create_tmp_reg (TREE_TYPE (result_decl));
    1823              : 
    1824              :       /* ??? With complex control flow (usually involving abnormal edges),
    1825              :          we can wind up warning about an uninitialized value for this.  Due
    1826              :          to how this variable is constructed and initialized, this is never
    1827              :          true.  Give up and never warn.  */
    1828      1511746 :       suppress_warning (result, OPT_Wuninitialized);
    1829              : 
    1830      1511746 :       gimplify_ctxp->return_temp = result;
    1831              :     }
    1832              : 
    1833              :   /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
    1834              :      Then gimplify the whole thing.  */
    1835      2196037 :   if (result != result_decl)
    1836      2013447 :     TREE_OPERAND (ret_expr, 0) = result;
    1837              : 
    1838      2196037 :   gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
    1839              : 
    1840      2196037 :   maybe_add_early_return_predict_stmt (pre_p);
    1841      2196037 :   ret = gimple_build_return (result);
    1842      2196037 :   copy_warning (ret, stmt);
    1843      2196037 :   gimplify_seq_add_stmt (pre_p, ret);
    1844              : 
    1845      2196037 :   return GS_ALL_DONE;
    1846              : }
    1847              : 
    1848              : /* Gimplify a variable-length array DECL.  */
    1849              : 
    1850              : static void
    1851         9063 : gimplify_vla_decl (tree decl, gimple_seq *seq_p)
    1852              : {
    1853              :   /* This is a variable-sized decl.  Simplify its size and mark it
    1854              :      for deferred expansion.  */
    1855         9063 :   tree t, addr, ptr_type;
    1856              : 
    1857         9063 :   gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
    1858         9063 :   gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
    1859              : 
    1860              :   /* Don't mess with a DECL_VALUE_EXPR set by the front-end.  */
    1861         9063 :   if (DECL_HAS_VALUE_EXPR_P (decl))
    1862              :     return;
    1863              : 
    1864              :   /* All occurrences of this decl in final gimplified code will be
    1865              :      replaced by indirection.  Setting DECL_VALUE_EXPR does two
    1866              :      things: First, it lets the rest of the gimplifier know what
    1867              :      replacement to use.  Second, it lets the debug info know
    1868              :      where to find the value.  */
    1869         9051 :   ptr_type = build_pointer_type (TREE_TYPE (decl));
    1870         9051 :   addr = create_tmp_var (ptr_type, get_name (decl));
    1871         9051 :   DECL_IGNORED_P (addr) = 0;
    1872         9051 :   t = build_fold_indirect_ref (addr);
    1873         9051 :   TREE_THIS_NOTRAP (t) = 1;
    1874         9051 :   SET_DECL_VALUE_EXPR (decl, t);
    1875         9051 :   DECL_HAS_VALUE_EXPR_P (decl) = 1;
    1876              : 
    1877         9051 :   t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
    1878         9051 :                               max_int_size_in_bytes (TREE_TYPE (decl)));
    1879              :   /* The call has been built for a variable-sized object.  */
    1880         9051 :   CALL_ALLOCA_FOR_VAR_P (t) = 1;
    1881         9051 :   t = fold_convert (ptr_type, t);
    1882         9051 :   t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
    1883              : 
    1884         9051 :   gimplify_and_add (t, seq_p);
    1885              : 
    1886              :   /* Record the dynamic allocation associated with DECL if requested.  */
    1887         9051 :   if (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC)
    1888            0 :     record_dynamic_alloc (decl);
    1889              : }
    1890              : 
    1891              : /* A helper function to be called via walk_tree.  Mark all labels under *TP
    1892              :    as being forced.  To be called for DECL_INITIAL of static variables.  */
    1893              : 
    1894              : static tree
    1895       872315 : force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
    1896              : {
    1897       872315 :   if (TYPE_P (*tp))
    1898            0 :     *walk_subtrees = 0;
    1899       872315 :   if (TREE_CODE (*tp) == LABEL_DECL)
    1900              :     {
    1901         1026 :       FORCED_LABEL (*tp) = 1;
    1902         1026 :       cfun->has_forced_label_in_static = 1;
    1903              :     }
    1904              : 
    1905       872315 :   return NULL_TREE;
    1906              : }
    1907              : 
    1908              : /* Generate an initialization to automatic variable DECL based on INIT_TYPE.
    1909              :    Build a call to internal const function DEFERRED_INIT:
    1910              :    1st argument: SIZE of the DECL;
    1911              :    2nd argument: INIT_TYPE;
    1912              :    3rd argument: NAME of the DECL;
    1913              : 
    1914              :    as LHS = DEFERRED_INIT (SIZE of the DECL, INIT_TYPE, NAME of the DECL).  */
    1915              : 
    1916              : static void
    1917       162000 : gimple_add_init_for_auto_var (tree decl,
    1918              :                               enum auto_init_type init_type,
    1919              :                               gimple_seq *seq_p)
    1920              : {
    1921       162000 :   gcc_assert (auto_var_p (decl));
    1922       162000 :   gcc_assert (init_type > AUTO_INIT_UNINITIALIZED);
    1923              : 
    1924       162000 :   const location_t loc = DECL_SOURCE_LOCATION (decl);
    1925       162000 :   tree decl_size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
    1926       162000 :   tree init_type_node = build_int_cst (integer_type_node, (int) init_type);
    1927       162000 :   tree decl_name;
    1928              : 
    1929       162000 :   if (DECL_NAME (decl))
    1930        92430 :     decl_name = build_string_literal (DECL_NAME (decl));
    1931              :   else
    1932              :     {
    1933        69570 :       char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
    1934        69570 :       sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
    1935        69570 :       decl_name = build_string_literal (decl_name_anonymous);
    1936              :     }
    1937              : 
    1938       162000 :   tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
    1939       162000 :                                             TREE_TYPE (decl), 3,
    1940              :                                             decl_size, init_type_node,
    1941              :                                             decl_name);
    1942              : 
    1943       162000 :   gimplify_assign (decl, call, seq_p);
    1944       162000 : }
    1945              : 
    1946              : /* Generate padding initialization for automatic variable DECL.
    1947              :    C guarantees that brace-init with fewer initializers than members
    1948              :    aggregate will initialize the rest of the aggregate as-if it were
    1949              :    static initialization.  In turn static initialization guarantees
    1950              :    that padding is initialized to zero. So, we always initialize paddings
    1951              :    to zeroes regardless INIT_TYPE.
    1952              :    To do the padding initialization, we insert a call to
    1953              :    __builtin_clear_padding (&decl, 0, for_auto_init = true).
    1954              :    Note, we add an additional dummy argument for __builtin_clear_padding,
    1955              :    'for_auto_init' to distinguish whether this call is for automatic
    1956              :    variable initialization or not.
    1957              :    */
    1958              : static void
    1959          116 : gimple_add_padding_init_for_auto_var (tree decl, bool is_vla,
    1960              :                                       gimple_seq *seq_p)
    1961              : {
    1962          116 :   tree addr_of_decl = NULL_TREE;
    1963          116 :   tree fn = builtin_decl_explicit (BUILT_IN_CLEAR_PADDING);
    1964              : 
    1965          116 :   if (is_vla)
    1966              :     {
    1967              :       /* The temporary address variable for this vla should be
    1968              :          created in gimplify_vla_decl.  */
    1969            0 :       gcc_assert (DECL_HAS_VALUE_EXPR_P (decl));
    1970            0 :       gcc_assert (INDIRECT_REF_P (DECL_VALUE_EXPR (decl)));
    1971            0 :       addr_of_decl = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
    1972              :     }
    1973              :   else
    1974              :     {
    1975          116 :       mark_addressable (decl);
    1976          116 :       addr_of_decl = build_fold_addr_expr (decl);
    1977              :     }
    1978              : 
    1979          116 :   gimple *call = gimple_build_call (fn, 2, addr_of_decl,
    1980          116 :                                     build_one_cst (TREE_TYPE (addr_of_decl)));
    1981          116 :   gimplify_seq_add_stmt (seq_p, call);
    1982          116 : }
    1983              : 
    1984              : /* Return true if the DECL need to be automatically initialized by the
    1985              :    compiler.  */
    1986              : static bool
    1987      4386063 : var_needs_auto_init_p (tree decl)
    1988              : {
    1989      4386063 :   if (auto_var_p (decl)
    1990      4287130 :       && (TREE_CODE (decl) != VAR_DECL || !DECL_HARD_REGISTER (decl))
    1991      4286421 :       && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    1992       364101 :       && !lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl))
    1993       364093 :       && !lookup_attribute ("indeterminate", DECL_ATTRIBUTES (decl))
    1994       364084 :       && !OPAQUE_TYPE_P (TREE_TYPE (decl))
    1995      4750077 :       && !is_empty_type (TREE_TYPE (decl)))
    1996       336270 :     return true;
    1997              :   return false;
    1998              : }
    1999              : 
    2000              : /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
    2001              :    and initialization explicit.  */
    2002              : 
    2003              : static enum gimplify_status
    2004      6687131 : gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
    2005              : {
    2006      6687131 :   tree stmt = *stmt_p;
    2007      6687131 :   tree decl = DECL_EXPR_DECL (stmt);
    2008              : 
    2009      6687131 :   *stmt_p = NULL_TREE;
    2010              : 
    2011      6687131 :   if (TREE_TYPE (decl) == error_mark_node)
    2012              :     return GS_ERROR;
    2013              : 
    2014      6687044 :   if ((TREE_CODE (decl) == TYPE_DECL
    2015      6642294 :        || VAR_P (decl))
    2016     13326189 :       && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
    2017              :     {
    2018       721285 :       gimplify_type_sizes (TREE_TYPE (decl), seq_p);
    2019       721285 :       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
    2020        23535 :         gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
    2021              :     }
    2022              : 
    2023              :   /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
    2024              :      in case its size expressions contain problematic nodes like CALL_EXPR.  */
    2025      6687044 :   if (TREE_CODE (decl) == TYPE_DECL
    2026        44750 :       && DECL_ORIGINAL_TYPE (decl)
    2027      6692365 :       && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
    2028              :     {
    2029            0 :       gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
    2030            0 :       if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
    2031            0 :         gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
    2032              :     }
    2033              : 
    2034      6687044 :   if (VAR_P (decl) && !DECL_EXTERNAL (decl))
    2035              :     {
    2036      6639145 :       tree init = DECL_INITIAL (decl);
    2037      6639145 :       bool is_vla = false;
    2038              :       /* Check whether a decl has FE created VALUE_EXPR here BEFORE
    2039              :          gimplify_vla_decl creates VALUE_EXPR for a vla decl.
    2040              :          If the decl has VALUE_EXPR that was created by FE (usually
    2041              :          C++FE), it's a proxy variable, and FE already initialized
    2042              :          the VALUE_EXPR of it, we should not initialize it anymore.  */
    2043      6639145 :       bool decl_had_value_expr_p = DECL_HAS_VALUE_EXPR_P (decl);
    2044              : 
    2045      6639145 :       poly_uint64 size;
    2046      6639145 :       if (!poly_int_tree_p (DECL_SIZE_UNIT (decl), &size)
    2047      6639145 :           || (!TREE_STATIC (decl)
    2048      6511363 :               && flag_stack_check == GENERIC_STACK_CHECK
    2049          234 :               && maybe_gt (size,
    2050              :                            (unsigned HOST_WIDE_INT) STACK_CHECK_MAX_VAR_SIZE)))
    2051              :         {
    2052         8936 :           gimplify_vla_decl (decl, seq_p);
    2053         8936 :           is_vla = true;
    2054              :         }
    2055              : 
    2056      6639145 :       if (asan_poisoned_variables
    2057         5243 :           && !is_vla
    2058         5057 :           && TREE_ADDRESSABLE (decl)
    2059         2459 :           && !TREE_STATIC (decl)
    2060         2249 :           && !DECL_HAS_VALUE_EXPR_P (decl)
    2061         2172 :           && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
    2062         2172 :           && dbg_cnt (asan_use_after_scope)
    2063         2172 :           && !gimplify_omp_ctxp
    2064              :           /* GNAT introduces temporaries to hold return values of calls in
    2065              :              initializers of variables defined in other units, so the
    2066              :              declaration of the variable is discarded completely.  We do not
    2067              :              want to issue poison calls for such dropped variables.  */
    2068      6641309 :           && (DECL_SEEN_IN_BIND_EXPR_P (decl)
    2069            0 :               || (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)))
    2070              :         {
    2071         2164 :           asan_poisoned_variables->add (decl);
    2072         2164 :           asan_poison_variable (decl, false, seq_p);
    2073         2164 :           if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
    2074           55 :             gimplify_ctxp->live_switch_vars->add (decl);
    2075              :         }
    2076              : 
    2077              :       /* Some front ends do not explicitly declare all anonymous
    2078              :          artificial variables.  We compensate here by declaring the
    2079              :          variables, though it would be better if the front ends would
    2080              :          explicitly declare them.  */
    2081      6639145 :       if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
    2082      6639145 :           && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
    2083        18353 :         gimple_add_tmp_var (decl);
    2084              : 
    2085      6639145 :       if (init && init != error_mark_node)
    2086              :         {
    2087      3762108 :           if (!TREE_STATIC (decl))
    2088              :             {
    2089      3731495 :               DECL_INITIAL (decl) = NULL_TREE;
    2090      3731495 :               init = build2 (INIT_EXPR, void_type_node, decl, init);
    2091      3731495 :               gimplify_and_add (init, seq_p);
    2092      3731495 :               ggc_free (init);
    2093              :               /* Clear TREE_READONLY if we really have an initialization.  */
    2094      3731495 :               if (!DECL_INITIAL (decl)
    2095      3731495 :                   && !omp_privatize_by_reference (decl))
    2096      3706987 :                 TREE_READONLY (decl) = 0;
    2097              :             }
    2098              :           else
    2099              :             /* We must still examine initializers for static variables
    2100              :                as they may contain a label address.  */
    2101        30613 :             walk_tree (&init, force_labels_r, NULL, NULL);
    2102              :         }
    2103              :       /* When there is no explicit initializer, if the user requested,
    2104              :          We should insert an artificial initializer for this automatic
    2105              :          variable.  */
    2106      2877037 :       else if (var_needs_auto_init_p (decl)
    2107      2877037 :                && !decl_had_value_expr_p)
    2108              :         {
    2109       101946 :           gimple_add_init_for_auto_var (decl,
    2110              :                                         flag_auto_var_init,
    2111              :                                         seq_p);
    2112              :           /* The expanding of a call to the above .DEFERRED_INIT will apply
    2113              :              block initialization to the whole space covered by this variable.
    2114              :              As a result, all the paddings will be initialized to zeroes
    2115              :              for zero initialization and 0xFE byte-repeatable patterns for
    2116              :              pattern initialization.
    2117              :              In order to make the paddings as zeroes for pattern init, We
    2118              :              should add a call to __builtin_clear_padding to clear the
    2119              :              paddings to zero in compatible with CLANG.
    2120              :              We cannot insert this call if the variable is a gimple register
    2121              :              since __builtin_clear_padding will take the address of the
    2122              :              variable.  As a result, if a long double/_Complex long double
    2123              :              variable will spilled into stack later, its padding is 0XFE.  */
    2124       101946 :           if ((flag_auto_var_init & ~AUTO_INIT_CXX26) == AUTO_INIT_PATTERN
    2125          186 :               && !is_gimple_reg (decl)
    2126       102044 :               && clear_padding_type_may_have_padding_p (TREE_TYPE (decl)))
    2127           75 :             gimple_add_padding_init_for_auto_var (decl, is_vla, seq_p);
    2128              :         }
    2129              :     }
    2130              : 
    2131              :   return GS_ALL_DONE;
    2132              : }
    2133              : 
    2134              : /* Gimplify a LOOP_EXPR.  Normally this just involves gimplifying the body
    2135              :    and replacing the LOOP_EXPR with goto, but if the loop contains an
    2136              :    EXIT_EXPR, we need to append a label for it to jump to.  */
    2137              : 
    2138              : static enum gimplify_status
    2139       211390 : gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
    2140              : {
    2141       211390 :   tree saved_label = gimplify_ctxp->exit_label;
    2142       211390 :   tree start_label = create_artificial_label (UNKNOWN_LOCATION);
    2143              : 
    2144       211390 :   gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
    2145              : 
    2146       211390 :   gimplify_ctxp->exit_label = NULL_TREE;
    2147              : 
    2148       211390 :   gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
    2149              : 
    2150       211390 :   gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
    2151              : 
    2152       211390 :   if (gimplify_ctxp->exit_label)
    2153         6810 :     gimplify_seq_add_stmt (pre_p,
    2154         3405 :                            gimple_build_label (gimplify_ctxp->exit_label));
    2155              : 
    2156       211390 :   gimplify_ctxp->exit_label = saved_label;
    2157              : 
    2158       211390 :   *expr_p = NULL;
    2159       211390 :   return GS_ALL_DONE;
    2160              : }
    2161              : 
    2162              : /* Gimplify a statement list onto a sequence.  These may be created either
    2163              :    by an enlightened front-end, or by shortcut_cond_expr.  */
    2164              : 
    2165              : static enum gimplify_status
    2166      8720639 : gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
    2167              : {
    2168      8720639 :   tree temp = voidify_wrapper_expr (*expr_p, NULL);
    2169              : 
    2170      8720639 :   tree_stmt_iterator i = tsi_start (*expr_p);
    2171              : 
    2172     56423479 :   while (!tsi_end_p (i))
    2173              :     {
    2174     38982201 :       gimplify_stmt (tsi_stmt_ptr (i), pre_p);
    2175     38982201 :       tsi_delink (&i);
    2176              :     }
    2177              : 
    2178      8720639 :   if (temp)
    2179              :     {
    2180        14839 :       *expr_p = temp;
    2181        14839 :       return GS_OK;
    2182              :     }
    2183              : 
    2184              :   return GS_ALL_DONE;
    2185              : }
    2186              : 
    2187              : 
    2188              : /* Emit warning for the unreachable statement STMT if needed.
    2189              :    Return the gimple itself when the warning is emitted, otherwise
    2190              :    return NULL.  */
    2191              : static gimple *
    2192          180 : emit_warn_switch_unreachable (gimple *stmt)
    2193              : {
    2194          180 :   if (gimple_code (stmt) == GIMPLE_GOTO
    2195           50 :       && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
    2196          230 :       && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
    2197              :   /* Don't warn for compiler-generated gotos.  These occur
    2198              :      in Duff's devices, for example.  */
    2199              :     return NULL;
    2200              :   else
    2201          130 :     warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
    2202              :                 "statement will never be executed");
    2203          130 :   return stmt;
    2204              : }
    2205              : 
    2206              : /* Callback for walk_gimple_seq.  */
    2207              : 
    2208              : static tree
    2209        50982 : warn_switch_unreachable_and_auto_init_r (gimple_stmt_iterator *gsi_p,
    2210              :                                          bool *handled_ops_p,
    2211              :                                          struct walk_stmt_info *wi)
    2212              : {
    2213        50982 :   gimple *stmt = gsi_stmt (*gsi_p);
    2214        50982 :   bool unreachable_issued = wi->info != NULL;
    2215              : 
    2216        50982 :   *handled_ops_p = true;
    2217        50982 :   switch (gimple_code (stmt))
    2218              :     {
    2219          441 :     case GIMPLE_TRY:
    2220              :       /* A compiler-generated cleanup or a user-written try block.
    2221              :          If it's empty, don't dive into it--that would result in
    2222              :          worse location info.  */
    2223          441 :       if (gimple_try_eval (stmt) == NULL)
    2224              :         {
    2225            3 :           if (warn_switch_unreachable && !unreachable_issued)
    2226            3 :             wi->info = emit_warn_switch_unreachable (stmt);
    2227              : 
    2228              :           /* Stop when auto var init warning is not on.  */
    2229            3 :           if (!warn_trivial_auto_var_init)
    2230            3 :             return integer_zero_node;
    2231              :         }
    2232              :       /* Fall through.  */
    2233         2683 :     case GIMPLE_BIND:
    2234         2683 :     case GIMPLE_CATCH:
    2235         2683 :     case GIMPLE_EH_FILTER:
    2236         2683 :     case GIMPLE_TRANSACTION:
    2237              :       /* Walk the sub-statements.  */
    2238         2683 :       *handled_ops_p = false;
    2239         2683 :       break;
    2240              : 
    2241              :     case GIMPLE_DEBUG:
    2242              :       /* Ignore these.  We may generate them before declarations that
    2243              :          are never executed.  If there's something to warn about,
    2244              :          there will be non-debug stmts too, and we'll catch those.  */
    2245              :       break;
    2246              : 
    2247           60 :     case GIMPLE_ASSIGN:
    2248              :       /* See comment below in the GIMPLE_CALL case.  */
    2249           60 :       if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    2250           20 :           && gimple_assign_single_p (stmt)
    2251           79 :           && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
    2252              :         {
    2253            6 :           gimple *g = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
    2254            6 :           if (gimple_call_internal_p (g, IFN_DEFERRED_INIT))
    2255              :             break;
    2256              :         }
    2257           54 :       goto do_default;
    2258              : 
    2259        47137 :     case GIMPLE_LABEL:
    2260              :       /* Stop till the first Label.  */
    2261        47137 :       return integer_zero_node;
    2262           61 :     case GIMPLE_CALL:
    2263           61 :       if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
    2264              :         {
    2265            8 :           *handled_ops_p = false;
    2266            8 :           break;
    2267              :         }
    2268              :       /* Don't warn for compiler-generated initializations for
    2269              :          -ftrivial-auto-var-init for -Wswitch-unreachable.  Though
    2270              :          do warn for -Wtrivial-auto-var-init.
    2271              :          There are 3 cases:
    2272              :          case 1: a call to .DEFERRED_INIT;
    2273              :          case 2: a call to __builtin_clear_padding with the 2nd argument is
    2274              :                  present and non-zero;
    2275              :          case 3: a gimple assign store right after the call to .DEFERRED_INIT
    2276              :                  that has the LHS of .DEFERRED_INIT as the RHS as following:
    2277              :                   _1 = .DEFERRED_INIT (4, 2, &"i1"[0]);
    2278              :                   i1 = _1.
    2279              :          case 3 is handled above in the GIMPLE_ASSIGN case.  */
    2280           53 :       if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    2281           53 :           && gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
    2282              :         {
    2283           33 :           if (warn_trivial_auto_var_init)
    2284              :             {
    2285              :               /* Get the variable name from the 3rd argument of call.  */
    2286           12 :               tree var_name = gimple_call_arg (stmt, 2);
    2287           12 :               var_name = TREE_OPERAND (TREE_OPERAND (var_name, 0), 0);
    2288           12 :               const char *var_name_str = TREE_STRING_POINTER (var_name);
    2289              : 
    2290           12 :               warning_at (gimple_location (stmt), OPT_Wtrivial_auto_var_init,
    2291              :                           "%qs cannot be initialized with "
    2292              :                           "%<-ftrivial-auto-var_init%>", var_name_str);
    2293              :             }
    2294              :           break;
    2295              :        }
    2296           20 :       if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    2297            7 :           && gimple_call_builtin_p (stmt, BUILT_IN_CLEAR_PADDING)
    2298           23 :           && (bool) TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)))
    2299              :         break;
    2300              :       /* Fall through.  */
    2301          177 :     default:
    2302          177 :     do_default:
    2303              :       /* check the first "real" statement (not a decl/lexical scope/...), issue
    2304              :          warning if needed.  */
    2305          177 :       if (warn_switch_unreachable && !unreachable_issued)
    2306          177 :         wi->info = emit_warn_switch_unreachable (stmt);
    2307              :       /* Stop when auto var init warning is not on.  */
    2308          177 :       if (!warn_trivial_auto_var_init)
    2309          177 :         return integer_zero_node;
    2310              :       break;
    2311              :     }
    2312              :   return NULL_TREE;
    2313              : }
    2314              : 
    2315              : 
    2316              : /* Possibly warn about unreachable statements between switch's controlling
    2317              :    expression and the first case.  Also warn about -ftrivial-auto-var-init
    2318              :    cannot initialize the auto variable under such situation.
    2319              :    SEQ is the body of a switch expression.  */
    2320              : 
    2321              : static void
    2322        51297 : maybe_warn_switch_unreachable_and_auto_init (gimple_seq seq)
    2323              : {
    2324           64 :   if ((!warn_switch_unreachable && !warn_trivial_auto_var_init)
    2325              :       /* This warning doesn't play well with Fortran when optimizations
    2326              :          are on.  */
    2327        51233 :       || lang_GNU_Fortran ()
    2328        98788 :       || seq == NULL)
    2329         3977 :     return;
    2330              : 
    2331        47320 :   struct walk_stmt_info wi;
    2332              : 
    2333        47320 :   memset (&wi, 0, sizeof (wi));
    2334        47320 :   walk_gimple_seq (seq, warn_switch_unreachable_and_auto_init_r, NULL, &wi);
    2335              : }
    2336              : 
    2337              : 
    2338              : /* A label entry that pairs label and a location.  */
    2339              : struct label_entry
    2340              : {
    2341              :   tree label;
    2342              :   location_t loc;
    2343              : };
    2344              : 
    2345              : /* Find LABEL in vector of label entries VEC.  */
    2346              : 
    2347              : static struct label_entry *
    2348        12336 : find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
    2349              : {
    2350        12336 :   unsigned int i;
    2351        12336 :   struct label_entry *l;
    2352              : 
    2353        23562 :   FOR_EACH_VEC_ELT (*vec, i, l)
    2354        20198 :     if (l->label == label)
    2355              :       return l;
    2356              :   return NULL;
    2357              : }
    2358              : 
    2359              : /* Return true if LABEL, a LABEL_DECL, represents a case label
    2360              :    in a vector of labels CASES.  */
    2361              : 
    2362              : static bool
    2363        16478 : case_label_p (const vec<tree> *cases, tree label)
    2364              : {
    2365        16478 :   unsigned int i;
    2366        16478 :   tree l;
    2367              : 
    2368       165417 :   FOR_EACH_VEC_ELT (*cases, i, l)
    2369       165083 :     if (CASE_LABEL (l) == label)
    2370              :       return true;
    2371              :   return false;
    2372              : }
    2373              : 
    2374              : /* Find the last nondebug statement in a scope STMT.  */
    2375              : 
    2376              : static gimple *
    2377         2257 : last_stmt_in_scope (gimple *stmt)
    2378              : {
    2379         4196 :   if (!stmt)
    2380              :     return NULL;
    2381              : 
    2382         6834 :   auto last_stmt_in_seq = [] (gimple_seq s) 
    2383              :     {
    2384         2649 :       gimple_seq_node n;
    2385         5289 :       for (n = gimple_seq_last (s);
    2386         2654 :            n && (is_gimple_debug (n)
    2387         2641 :                  || (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    2388          109 :                      && gimple_call_internal_p (n, IFN_DEFERRED_INIT)));
    2389            5 :         n = n->prev)
    2390            7 :       if (n == s)
    2391              :         return (gimple *) NULL;
    2392              :       return (gimple *) n;
    2393              :     };
    2394              : 
    2395         4185 :   switch (gimple_code (stmt))
    2396              :     {
    2397         1391 :     case GIMPLE_BIND:
    2398         1391 :       {
    2399         1391 :         gbind *bind = as_a <gbind *> (stmt);
    2400         1391 :         stmt = last_stmt_in_seq (gimple_bind_body (bind));
    2401         1391 :         return last_stmt_in_scope (stmt);
    2402              :       }
    2403              : 
    2404          710 :     case GIMPLE_TRY:
    2405          710 :       {
    2406          710 :         gtry *try_stmt = as_a <gtry *> (stmt);
    2407          710 :         stmt = last_stmt_in_seq (gimple_try_eval (try_stmt));
    2408          710 :         gimple *last_eval = last_stmt_in_scope (stmt);
    2409          710 :         if (gimple_stmt_may_fallthru (last_eval)
    2410          567 :             && (last_eval == NULL
    2411          564 :                 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
    2412         1270 :             && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
    2413              :           {
    2414          548 :             stmt = last_stmt_in_seq (gimple_try_cleanup (try_stmt));
    2415          548 :             return last_stmt_in_scope (stmt);
    2416              :           }
    2417              :         else
    2418              :           return last_eval;
    2419              :       }
    2420              : 
    2421            0 :     case GIMPLE_DEBUG:
    2422            0 :       gcc_unreachable ();
    2423              : 
    2424              :     default:
    2425              :       return stmt;
    2426              :     }
    2427              : }
    2428              : 
    2429              : /* Collect labels that may fall through into LABELS and return the statement
    2430              :    preceding another case label, or a user-defined label.  Store a location
    2431              :    useful to give warnings at *PREVLOC (usually the location of the returned
    2432              :    statement or of its surrounding scope).  */
    2433              : 
    2434              : static gimple *
    2435        21093 : collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
    2436              :                             auto_vec <struct label_entry> *labels,
    2437              :                             location_t *prevloc)
    2438              : {
    2439        21093 :   gimple *prev = NULL;
    2440              : 
    2441        21093 :   *prevloc = UNKNOWN_LOCATION;
    2442       102184 :   do
    2443              :     {
    2444       102184 :       if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
    2445              :         {
    2446              :           /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
    2447              :              which starts on a GIMPLE_SWITCH and ends with a break label.
    2448              :              Handle that as a single statement that can fall through.  */
    2449         1452 :           gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
    2450         1452 :           gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
    2451         1452 :           gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
    2452         1446 :           if (last
    2453         1446 :               && gimple_code (first) == GIMPLE_SWITCH
    2454         1548 :               && gimple_code (last) == GIMPLE_LABEL)
    2455              :             {
    2456          102 :               tree label = gimple_label_label (as_a <glabel *> (last));
    2457          102 :               if (SWITCH_BREAK_LABEL_P (label))
    2458              :                 {
    2459          102 :                   prev = bind;
    2460          102 :                   gsi_next (gsi_p);
    2461          102 :                   continue;
    2462              :                 }
    2463              :             }
    2464              :         }
    2465       102082 :       if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
    2466       102082 :           || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
    2467              :         {
    2468              :           /* Nested scope.  Only look at the last statement of
    2469              :              the innermost scope.  */
    2470         1547 :           location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
    2471         1547 :           gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
    2472         1547 :           if (last)
    2473              :             {
    2474         1539 :               prev = last;
    2475              :               /* It might be a label without a location.  Use the
    2476              :                  location of the scope then.  */
    2477         1539 :               if (!gimple_has_location (prev))
    2478          671 :                 *prevloc = bind_loc;
    2479              :             }
    2480         1547 :           gsi_next (gsi_p);
    2481         1547 :           continue;
    2482         1547 :         }
    2483              : 
    2484              :       /* Ifs are tricky.  */
    2485       100535 :       if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
    2486              :         {
    2487         7256 :           gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
    2488         7256 :           tree false_lab = gimple_cond_false_label (cond_stmt);
    2489         7256 :           location_t if_loc = gimple_location (cond_stmt);
    2490              : 
    2491              :           /* If we have e.g.
    2492              :                if (i > 1) goto <D.2259>; else goto D;
    2493              :              we can't do much with the else-branch.  */
    2494         7256 :           if (!DECL_ARTIFICIAL (false_lab))
    2495              :             break;
    2496              : 
    2497              :           /* Go on until the false label, then one step back.  */
    2498        54990 :           for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
    2499              :             {
    2500        54990 :               gimple *stmt = gsi_stmt (*gsi_p);
    2501        54990 :               if (gimple_code (stmt) == GIMPLE_LABEL
    2502        54990 :                   && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
    2503              :                 break;
    2504              :             }
    2505              : 
    2506              :           /* Not found?  Oops.  */
    2507         7256 :           if (gsi_end_p (*gsi_p))
    2508              :             break;
    2509              : 
    2510              :           /* A dead label can't fall through.  */
    2511         7256 :           if (!UNUSED_LABEL_P (false_lab))
    2512              :             {
    2513         7209 :               struct label_entry l = { false_lab, if_loc };
    2514         7209 :               labels->safe_push (l);
    2515              :             }
    2516              : 
    2517              :           /* Go to the last statement of the then branch.  */
    2518         7256 :           gsi_prev (gsi_p);
    2519              : 
    2520              :           /* if (i != 0) goto <D.1759>; else goto <D.1760>;
    2521              :              <D.1759>:
    2522              :              <stmt>;
    2523              :              goto <D.1761>;
    2524              :              <D.1760>:
    2525              :            */
    2526         7256 :           if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
    2527         7256 :               && !gimple_has_location (gsi_stmt (*gsi_p)))
    2528              :             {
    2529              :               /* Look at the statement before, it might be
    2530              :                  attribute fallthrough, in which case don't warn.  */
    2531         1622 :               gsi_prev (gsi_p);
    2532         1622 :               bool fallthru_before_dest
    2533         1622 :                 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
    2534         1622 :               gsi_next (gsi_p);
    2535         1622 :               tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
    2536         1622 :               if (!fallthru_before_dest)
    2537              :                 {
    2538         1565 :                   struct label_entry l = { goto_dest, if_loc };
    2539         1565 :                   labels->safe_push (l);
    2540              :                 }
    2541              :             }
    2542              :           /* This case is about
    2543              :               if (1 != 0) goto <D.2022>; else goto <D.2023>;
    2544              :               <D.2022>:
    2545              :               n = n + 1; // #1
    2546              :               <D.2023>:  // #2
    2547              :               <D.1988>:  // #3
    2548              :              where #2 is UNUSED_LABEL_P and we want to warn about #1 falling
    2549              :              through to #3.  So set PREV to #1.  */
    2550         5634 :           else if (UNUSED_LABEL_P (false_lab))
    2551           47 :             prev = gsi_stmt (*gsi_p);
    2552              : 
    2553              :           /* And move back.  */
    2554         7256 :           gsi_next (gsi_p);
    2555              :         }
    2556              : 
    2557       100535 :       tree lab;
    2558              :       /* Remember the last statement.  Skip labels that are of no interest
    2559              :          to us.  */
    2560       100535 :       if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
    2561              :         {
    2562        12118 :           tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
    2563        12118 :           if (find_label_entry (labels, label))
    2564         8774 :             prev = gsi_stmt (*gsi_p);
    2565              :         }
    2566        88417 :       else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
    2567              :         ;
    2568        88417 :       else if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    2569        88417 :                && gimple_call_internal_p (gsi_stmt (*gsi_p),
    2570              :                                           IFN_DEFERRED_INIT))
    2571              :         ;
    2572        88411 :       else if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_PREDICT)
    2573              :         ;
    2574        87183 :       else if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    2575         2382 :                && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
    2576          348 :                && (lab = gimple_goto_dest (gsi_stmt (*gsi_p)))
    2577          348 :                && TREE_CODE (lab) == LABEL_DECL
    2578        87530 :                && VACUOUS_INIT_LABEL_P (lab))
    2579              :         ;
    2580        87183 :       else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
    2581        66903 :         prev = gsi_stmt (*gsi_p);
    2582       100535 :       gsi_next (gsi_p);
    2583              :     }
    2584       102184 :   while (!gsi_end_p (*gsi_p)
    2585              :          /* Stop if we find a case or a user-defined label.  */
    2586       200281 :          && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
    2587        21868 :              || !gimple_has_location (gsi_stmt (*gsi_p))));
    2588              : 
    2589        21093 :   if (prev && gimple_has_location (prev))
    2590        20640 :     *prevloc = gimple_location (prev);
    2591        21093 :   return prev;
    2592              : }
    2593              : 
    2594              : /* Return true if the switch fallthrough warning should occur.  LABEL is
    2595              :    the label statement that we're falling through to.  */
    2596              : 
    2597              : static bool
    2598        16999 : should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
    2599              : {
    2600        16999 :   gimple_stmt_iterator gsi = *gsi_p;
    2601              : 
    2602              :   /* Don't warn if the label is marked with a "falls through" comment.  */
    2603        16999 :   if (FALLTHROUGH_LABEL_P (label))
    2604              :     return false;
    2605              : 
    2606              :   /* Don't warn for non-case labels followed by a statement:
    2607              :        case 0:
    2608              :          foo ();
    2609              :        label:
    2610              :          bar ();
    2611              :      as these are likely intentional.  */
    2612        16219 :   if (!case_label_p (&gimplify_ctxp->case_labels, label))
    2613              :     {
    2614              :       tree l;
    2615          334 :       while (!gsi_end_p (gsi)
    2616          334 :              && ((gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
    2617          259 :                   && (l
    2618          259 :                       = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
    2619          259 :                   && !case_label_p (&gimplify_ctxp->case_labels, l))
    2620          153 :                  || (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    2621           31 :                      && gimple_call_internal_p (gsi_stmt (gsi),
    2622              :                                                 IFN_DEFERRED_INIT))))
    2623          173 :         gsi_next_nondebug (&gsi);
    2624          161 :       if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
    2625              :         return false;
    2626              :     }
    2627              : 
    2628              :   /* Don't warn for terminated branches, i.e. when the subsequent case labels
    2629              :      immediately breaks.  */
    2630        16144 :   gsi = *gsi_p;
    2631              : 
    2632              :   /* Skip all immediately following labels.  */
    2633        35963 :   while (!gsi_end_p (gsi)
    2634        35963 :          && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
    2635        16293 :              || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT
    2636        16084 :              || (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    2637          775 :                  && gimple_call_internal_p (gsi_stmt (gsi),
    2638              :                                             IFN_DEFERRED_INIT))))
    2639        19819 :     gsi_next_nondebug (&gsi);
    2640              : 
    2641              :   /* { ... something; default:; } */
    2642        16144 :   if (gsi_end_p (gsi)
    2643              :       /* { ... something; default: break; } or
    2644              :          { ... something; default: goto L; } */
    2645        16078 :       || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
    2646              :       /* { ... something; default: return; } */
    2647        31123 :       || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
    2648         1192 :     return false;
    2649              : 
    2650              :   return true;
    2651              : }
    2652              : 
    2653              : /* Callback for walk_gimple_seq.  */
    2654              : 
    2655              : static tree
    2656        22101 : warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
    2657              :                              struct walk_stmt_info *)
    2658              : {
    2659        22101 :   gimple *stmt = gsi_stmt (*gsi_p);
    2660              : 
    2661        22101 :   *handled_ops_p = true;
    2662        22101 :   switch (gimple_code (stmt))
    2663              :     {
    2664          299 :     case GIMPLE_TRY:
    2665          299 :     case GIMPLE_BIND:
    2666          299 :     case GIMPLE_CATCH:
    2667          299 :     case GIMPLE_EH_FILTER:
    2668          299 :     case GIMPLE_TRANSACTION:
    2669              :       /* Walk the sub-statements.  */
    2670          299 :       *handled_ops_p = false;
    2671          299 :       break;
    2672              : 
    2673              :     /* Find a sequence of form:
    2674              : 
    2675              :        GIMPLE_LABEL
    2676              :        [...]
    2677              :        <may fallthru stmt>
    2678              :        GIMPLE_LABEL
    2679              : 
    2680              :        and possibly warn.  */
    2681              :     case GIMPLE_LABEL:
    2682              :       {
    2683              :         /* Found a label.  Skip all immediately following labels.  */
    2684        49612 :         while (!gsi_end_p (*gsi_p)
    2685        49612 :                && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
    2686        28285 :           gsi_next_nondebug (gsi_p);
    2687              : 
    2688              :         /* There might be no more statements.  */
    2689        21327 :         if (gsi_end_p (*gsi_p))
    2690         4321 :           return integer_zero_node;
    2691              : 
    2692              :         /* Vector of labels that fall through.  */
    2693        21093 :         auto_vec <struct label_entry> labels;
    2694        21093 :         location_t prevloc;
    2695        21093 :         gimple *prev = collect_fallthrough_labels (gsi_p, &labels, &prevloc);
    2696              : 
    2697              :         /* There might be no more statements.  */
    2698        21093 :         if (gsi_end_p (*gsi_p))
    2699         4087 :           return integer_zero_node;
    2700              : 
    2701        17006 :         gimple *next = gsi_stmt (*gsi_p);
    2702        17006 :         tree label;
    2703              :         /* If what follows is a label, then we may have a fallthrough.  */
    2704        17006 :         if (gimple_code (next) == GIMPLE_LABEL
    2705        17006 :             && gimple_has_location (next)
    2706        17006 :             && (label = gimple_label_label (as_a <glabel *> (next)))
    2707        34012 :             && prev != NULL)
    2708              :           {
    2709        16999 :             struct label_entry *l;
    2710        16999 :             bool warned_p = false;
    2711        16999 :             auto_diagnostic_group d;
    2712        16999 :             if (!should_warn_for_implicit_fallthrough (gsi_p, label))
    2713              :               /* Quiet.  */;
    2714        14952 :             else if (gimple_code (prev) == GIMPLE_LABEL
    2715          218 :                      && (label = gimple_label_label (as_a <glabel *> (prev)))
    2716        15170 :                      && (l = find_label_entry (&labels, label)))
    2717          198 :               warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
    2718              :                                      "this statement may fall through");
    2719        14754 :             else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
    2720              :                      /* Try to be clever and don't warn when the statement
    2721              :                         can't actually fall through.  */
    2722        14174 :                      && gimple_stmt_may_fallthru (prev)
    2723        15055 :                      && prevloc != UNKNOWN_LOCATION)
    2724          301 :               warned_p = warning_at (prevloc,
    2725          301 :                                      OPT_Wimplicit_fallthrough_,
    2726              :                                      "this statement may fall through");
    2727          499 :             if (warned_p)
    2728          499 :               inform (gimple_location (next), "here");
    2729              : 
    2730              :             /* Mark this label as processed so as to prevent multiple
    2731              :                warnings in nested switches.  */
    2732        16999 :             FALLTHROUGH_LABEL_P (label) = true;
    2733              : 
    2734              :             /* So that next warn_implicit_fallthrough_r will start looking for
    2735              :                a new sequence starting with this label.  */
    2736        16999 :             gsi_prev (gsi_p);
    2737        16999 :           }
    2738         4087 :       }
    2739        17006 :       break;
    2740              :    default:
    2741              :       break;
    2742              :     }
    2743              :   return NULL_TREE;
    2744              : }
    2745              : 
    2746              : /* Warn when a switch case falls through.  */
    2747              : 
    2748              : static void
    2749        51297 : maybe_warn_implicit_fallthrough (gimple_seq seq)
    2750              : {
    2751        51297 :   if (!warn_implicit_fallthrough)
    2752        47117 :     return;
    2753              : 
    2754              :   /* This warning is meant for C/C++/ObjC/ObjC++ only.  */
    2755         4180 :   if (!(lang_GNU_C ()
    2756         1232 :         || lang_GNU_CXX ()
    2757            0 :         || lang_GNU_OBJC ()))
    2758              :     return;
    2759              : 
    2760         4180 :   struct walk_stmt_info wi;
    2761         4180 :   memset (&wi, 0, sizeof (wi));
    2762         4180 :   walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
    2763              : }
    2764              : 
    2765              : /* Callback for walk_gimple_seq.  */
    2766              : 
    2767              : static tree
    2768      3374348 : expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
    2769              :                       struct walk_stmt_info *wi)
    2770              : {
    2771      3374348 :   gimple *stmt = gsi_stmt (*gsi_p);
    2772              : 
    2773      3374348 :   *handled_ops_p = true;
    2774      3374348 :   switch (gimple_code (stmt))
    2775              :     {
    2776       149448 :     case GIMPLE_TRY:
    2777       149448 :     case GIMPLE_BIND:
    2778       149448 :     case GIMPLE_CATCH:
    2779       149448 :     case GIMPLE_EH_FILTER:
    2780       149448 :     case GIMPLE_TRANSACTION:
    2781              :       /* Walk the sub-statements.  */
    2782       149448 :       *handled_ops_p = false;
    2783       149448 :       break;
    2784       259712 :     case GIMPLE_CALL:
    2785       259712 :       static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
    2786       259712 :       if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
    2787              :         {
    2788         3962 :           location_t loc = gimple_location (stmt);
    2789         3962 :           gsi_remove (gsi_p, true);
    2790         3962 :           wi->removed_stmt = true;
    2791              : 
    2792              :           /* nothrow flag is added by genericize_c_loop to mark fallthrough
    2793              :              statement at the end of some loop's body.  Those should be
    2794              :              always diagnosed, either because they indeed don't precede
    2795              :              a case label or default label, or because the next statement
    2796              :              is not within the same iteration statement.  */
    2797         3962 :           if ((stmt->subcode & GF_CALL_NOTHROW) != 0)
    2798              :             {
    2799           12 :               pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
    2800              :                                "a case label or default label");
    2801           12 :               break;
    2802              :             }
    2803              : 
    2804         3950 :           if (gsi_end_p (*gsi_p))
    2805              :             {
    2806           32 :               static_cast<location_t *>(wi->info)[0] = BUILTINS_LOCATION;
    2807           32 :               static_cast<location_t *>(wi->info)[1] = loc;
    2808           32 :               break;
    2809              :             }
    2810              : 
    2811         3918 :           bool found = false;
    2812              : 
    2813         3918 :           gimple_stmt_iterator gsi2 = *gsi_p;
    2814         3918 :           stmt = gsi_stmt (gsi2);
    2815         3918 :           tree lab;
    2816         3918 :           if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    2817         1704 :               && gimple_code (stmt) == GIMPLE_GOTO
    2818           20 :               && (lab = gimple_goto_dest (stmt))
    2819           20 :               && TREE_CODE (lab) == LABEL_DECL
    2820         3938 :               && VACUOUS_INIT_LABEL_P (lab))
    2821              :             {
    2822              :               /* Handle for C++ artificial -ftrivial-auto-var-init=
    2823              :                  sequences.  Those look like:
    2824              :                  goto lab1;
    2825              :                  lab2:;
    2826              :                  v1 = .DEFERRED_INIT (...);
    2827              :                  v2 = .DEFERRED_INIT (...);
    2828              :                  lab3:;
    2829              :                  v3 = .DEFERRED_INIT (...);
    2830              :                  lab1:;
    2831              :                  In this case, a case/default label can be either in between
    2832              :                  the GIMPLE_GOTO and the corresponding GIMPLE_LABEL, if jumps
    2833              :                  from the switch condition to the case/default label cross
    2834              :                  vacuous initialization of some variables, or after the
    2835              :                  corresponding GIMPLE_LABEL, if those jumps don't cross
    2836              :                  any such initialization but there is an adjacent named label
    2837              :                  which crosses such initialization.  So, for the purpose of
    2838              :                  this function, just ignore the goto but until reaching the
    2839              :                  corresponding GIMPLE_LABEL allow also .DEFERRED_INIT
    2840              :                  calls.  */
    2841            5 :               gsi_next (&gsi2);
    2842              :             }
    2843         3913 :           else if (gimple_code (stmt) == GIMPLE_GOTO
    2844         3913 :                    && !gimple_has_location (stmt))
    2845              :             {
    2846              :               /* Go on until the artificial label.  */
    2847           57 :               tree goto_dest = gimple_goto_dest (stmt);
    2848          414 :               for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
    2849              :                 {
    2850          357 :                   if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
    2851          357 :                       && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
    2852              :                            == goto_dest)
    2853              :                     break;
    2854              :                 }
    2855              : 
    2856              :               /* Not found?  Stop.  */
    2857           57 :               if (gsi_end_p (gsi2))
    2858              :                 break;
    2859              : 
    2860              :               /* Look one past it.  */
    2861           57 :               gsi_next (&gsi2);
    2862              :             }
    2863              : 
    2864              :           /* We're looking for a case label or default label here.  */
    2865         4016 :           while (!gsi_end_p (gsi2))
    2866              :             {
    2867         4016 :               stmt = gsi_stmt (gsi2);
    2868         4016 :               if (gimple_code (stmt) == GIMPLE_LABEL)
    2869              :                 {
    2870         3973 :                   tree label = gimple_label_label (as_a <glabel *> (stmt));
    2871         3973 :                   if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
    2872              :                     {
    2873              :                       found = true;
    2874              :                       break;
    2875              :                     }
    2876              :                 }
    2877           43 :               else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
    2878              :                 ;
    2879           43 :               else if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
    2880           43 :                        && gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
    2881              :                 ;
    2882           43 :               else if (!is_gimple_debug (stmt))
    2883              :                 /* Anything else is not expected.  */
    2884              :                 break;
    2885           98 :               gsi_next (&gsi2);
    2886              :             }
    2887         3918 :           if (!found)
    2888           23 :             pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
    2889              :                      "a case label or default label");
    2890              :         }
    2891              :       break;
    2892      2965188 :     default:
    2893      2965188 :       static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
    2894      2965188 :       break;
    2895              :     }
    2896      3374348 :   return NULL_TREE;
    2897              : }
    2898              : 
    2899              : /* Expand all FALLTHROUGH () calls in SEQ.  */
    2900              : 
    2901              : static void
    2902        49199 : expand_FALLTHROUGH (gimple_seq *seq_p)
    2903              : {
    2904        49199 :   auto_urlify_attributes sentinel;
    2905              : 
    2906        49199 :   struct walk_stmt_info wi;
    2907        49199 :   location_t loc[2];
    2908        49199 :   memset (&wi, 0, sizeof (wi));
    2909        49199 :   loc[0] = UNKNOWN_LOCATION;
    2910        49199 :   loc[1] = UNKNOWN_LOCATION;
    2911        49199 :   wi.info = (void *) &loc[0];
    2912        49199 :   walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
    2913        49199 :   if (loc[0] != UNKNOWN_LOCATION)
    2914              :     /* We've found [[fallthrough]]; at the end of a switch, which the C++
    2915              :        standard says is ill-formed; see [dcl.attr.fallthrough].  */
    2916            8 :     pedwarn (loc[1], 0, "attribute %<fallthrough%> not preceding "
    2917              :              "a case label or default label");
    2918        49199 : }
    2919              : 
    2920              : 
    2921              : /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
    2922              :    branch to.  */
    2923              : 
    2924              : static enum gimplify_status
    2925        51297 : gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
    2926              : {
    2927        51297 :   tree switch_expr = *expr_p;
    2928        51297 :   gimple_seq switch_body_seq = NULL;
    2929        51297 :   enum gimplify_status ret;
    2930        51297 :   tree index_type = TREE_TYPE (switch_expr);
    2931        51297 :   if (index_type == NULL_TREE)
    2932        11488 :     index_type = TREE_TYPE (SWITCH_COND (switch_expr));
    2933              : 
    2934        51297 :   ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
    2935              :                        fb_rvalue);
    2936        51297 :   if (ret == GS_ERROR || ret == GS_UNHANDLED)
    2937              :     return ret;
    2938              : 
    2939        51297 :   if (SWITCH_BODY (switch_expr))
    2940              :     {
    2941        51297 :       vec<tree> labels;
    2942        51297 :       vec<tree> saved_labels;
    2943        51297 :       hash_set<tree> *saved_live_switch_vars = NULL;
    2944        51297 :       tree default_case = NULL_TREE;
    2945        51297 :       gswitch *switch_stmt;
    2946              : 
    2947              :       /* Save old labels, get new ones from body, then restore the old
    2948              :          labels.  Save all the things from the switch body to append after.  */
    2949        51297 :       saved_labels = gimplify_ctxp->case_labels;
    2950        51297 :       gimplify_ctxp->case_labels.create (8);
    2951              : 
    2952              :       /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR.  */
    2953        51297 :       saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
    2954        51297 :       tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
    2955        51297 :       if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
    2956        51013 :         gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
    2957              :       else
    2958          284 :         gimplify_ctxp->live_switch_vars = NULL;
    2959              : 
    2960        51297 :       bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
    2961        51297 :       gimplify_ctxp->in_switch_expr = true;
    2962              : 
    2963        51297 :       gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
    2964              : 
    2965        51297 :       gimplify_ctxp->in_switch_expr = old_in_switch_expr;
    2966        51297 :       maybe_warn_switch_unreachable_and_auto_init (switch_body_seq);
    2967        51297 :       maybe_warn_implicit_fallthrough (switch_body_seq);
    2968              :       /* Only do this for the outermost GIMPLE_SWITCH.  */
    2969        51297 :       if (!gimplify_ctxp->in_switch_expr)
    2970        49199 :         expand_FALLTHROUGH (&switch_body_seq);
    2971              : 
    2972        51297 :       labels = gimplify_ctxp->case_labels;
    2973        51297 :       gimplify_ctxp->case_labels = saved_labels;
    2974              : 
    2975        51297 :       if (gimplify_ctxp->live_switch_vars)
    2976              :         {
    2977        51013 :           gcc_assert (gimplify_ctxp->live_switch_vars->is_empty ());
    2978        51013 :           delete gimplify_ctxp->live_switch_vars;
    2979              :         }
    2980        51297 :       gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
    2981              : 
    2982        51297 :       preprocess_case_label_vec_for_gimple (labels, index_type,
    2983              :                                             &default_case);
    2984              : 
    2985        51297 :       bool add_bind = false;
    2986        51297 :       if (!default_case)
    2987              :         {
    2988        15189 :           glabel *new_default;
    2989              : 
    2990        15189 :           default_case
    2991        15189 :             = build_case_label (NULL_TREE, NULL_TREE,
    2992              :                                 create_artificial_label (UNKNOWN_LOCATION));
    2993        15189 :           if (old_in_switch_expr)
    2994              :             {
    2995         1088 :               SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
    2996         1088 :               add_bind = true;
    2997              :             }
    2998        15189 :           new_default = gimple_build_label (CASE_LABEL (default_case));
    2999        15189 :           gimplify_seq_add_stmt (&switch_body_seq, new_default);
    3000              :         }
    3001        36108 :       else if (old_in_switch_expr)
    3002              :         {
    3003         1010 :           gimple *last = gimple_seq_last_stmt (switch_body_seq);
    3004         1010 :           if (last && gimple_code (last) == GIMPLE_LABEL)
    3005              :             {
    3006          745 :               tree label = gimple_label_label (as_a <glabel *> (last));
    3007          745 :               if (SWITCH_BREAK_LABEL_P (label))
    3008        51297 :                 add_bind = true;
    3009              :             }
    3010              :         }
    3011              : 
    3012        51297 :       switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
    3013              :                                          default_case, labels);
    3014        51297 :       gimple_set_location (switch_stmt, EXPR_LOCATION (switch_expr));
    3015              :       /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
    3016              :          ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
    3017              :          wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
    3018              :          so that we can easily find the start and end of the switch
    3019              :          statement.  */
    3020        51297 :       if (add_bind)
    3021              :         {
    3022         1832 :           gimple_seq bind_body = NULL;
    3023         1832 :           gimplify_seq_add_stmt (&bind_body, switch_stmt);
    3024         1832 :           gimple_seq_add_seq (&bind_body, switch_body_seq);
    3025         1832 :           gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
    3026         1832 :           gimple_set_location (bind, EXPR_LOCATION (switch_expr));
    3027         1832 :           gimplify_seq_add_stmt (pre_p, bind);
    3028              :         }
    3029              :       else
    3030              :         {
    3031        49465 :           gimplify_seq_add_stmt (pre_p, switch_stmt);
    3032        49465 :           gimplify_seq_add_seq (pre_p, switch_body_seq);
    3033              :         }
    3034        51297 :       labels.release ();
    3035              :     }
    3036              :   else
    3037            0 :     gcc_unreachable ();
    3038              : 
    3039        51297 :   return GS_ALL_DONE;
    3040              : }
    3041              : 
    3042              : /* Gimplify the LABEL_EXPR pointed to by EXPR_P.  */
    3043              : 
    3044              : static enum gimplify_status
    3045      2649060 : gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
    3046              : {
    3047      2649060 :   gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
    3048              :               == current_function_decl);
    3049              : 
    3050      2649060 :   tree label = LABEL_EXPR_LABEL (*expr_p);
    3051      2649060 :   glabel *label_stmt = gimple_build_label (label);
    3052      2649060 :   gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
    3053      2649060 :   gimplify_seq_add_stmt (pre_p, label_stmt);
    3054              : 
    3055      2649060 :   if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
    3056           21 :     gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
    3057              :                                                       NOT_TAKEN));
    3058      2649039 :   else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
    3059           11 :     gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
    3060              :                                                       TAKEN));
    3061              : 
    3062      2649060 :   return GS_ALL_DONE;
    3063              : }
    3064              : 
    3065              : /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P.  */
    3066              : 
    3067              : static enum gimplify_status
    3068      1048651 : gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
    3069              : {
    3070      1048651 :   struct gimplify_ctx *ctxp;
    3071      1048651 :   glabel *label_stmt;
    3072              : 
    3073              :   /* Invalid programs can play Duff's Device type games with, for example,
    3074              :      #pragma omp parallel.  At least in the C front end, we don't
    3075              :      detect such invalid branches until after gimplification, in the
    3076              :      diagnose_omp_blocks pass.  */
    3077      1048661 :   for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
    3078      1048661 :     if (ctxp->case_labels.exists ())
    3079              :       break;
    3080              : 
    3081      1048651 :   tree label = CASE_LABEL (*expr_p);
    3082      1048651 :   label_stmt = gimple_build_label (label);
    3083      1048651 :   gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
    3084      1048651 :   ctxp->case_labels.safe_push (*expr_p);
    3085      1048651 :   gimplify_seq_add_stmt (pre_p, label_stmt);
    3086              : 
    3087      1048651 :   if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
    3088           16 :     gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
    3089              :                                                       NOT_TAKEN));
    3090      1048635 :   else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
    3091           20 :     gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
    3092              :                                                       TAKEN));
    3093              : 
    3094      1048651 :   return GS_ALL_DONE;
    3095              : }
    3096              : 
    3097              : /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
    3098              :    if necessary.  */
    3099              : 
    3100              : tree
    3101      1811653 : build_and_jump (tree *label_p)
    3102              : {
    3103      1811653 :   if (label_p == NULL)
    3104              :     /* If there's nowhere to jump, just fall through.  */
    3105              :     return NULL_TREE;
    3106              : 
    3107      1228689 :   if (*label_p == NULL_TREE)
    3108              :     {
    3109       703970 :       tree label = create_artificial_label (UNKNOWN_LOCATION);
    3110       703970 :       *label_p = label;
    3111              :     }
    3112              : 
    3113      1228689 :   return build1 (GOTO_EXPR, void_type_node, *label_p);
    3114              : }
    3115              : 
    3116              : /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
    3117              :    This also involves building a label to jump to and communicating it to
    3118              :    gimplify_loop_expr through gimplify_ctxp->exit_label.  */
    3119              : 
    3120              : static enum gimplify_status
    3121         3411 : gimplify_exit_expr (tree *expr_p)
    3122              : {
    3123         3411 :   tree cond = TREE_OPERAND (*expr_p, 0);
    3124         3411 :   tree expr;
    3125              : 
    3126         3411 :   expr = build_and_jump (&gimplify_ctxp->exit_label);
    3127         3411 :   expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
    3128         3411 :   *expr_p = expr;
    3129              : 
    3130         3411 :   return GS_OK;
    3131              : }
    3132              : 
    3133              : /* *EXPR_P is a COMPONENT_REF being used as an rvalue.  If its type is
    3134              :    different from its canonical type, wrap the whole thing inside a
    3135              :    NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
    3136              :    type.
    3137              : 
    3138              :    The canonical type of a COMPONENT_REF is the type of the field being
    3139              :    referenced--unless the field is a bit-field which can be read directly
    3140              :    in a smaller mode, in which case the canonical type is the
    3141              :    sign-appropriate type corresponding to that mode.  */
    3142              : 
    3143              : static void
    3144     19177870 : canonicalize_component_ref (tree *expr_p)
    3145              : {
    3146     19177870 :   tree expr = *expr_p;
    3147     19177870 :   tree type;
    3148              : 
    3149     19177870 :   gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
    3150              : 
    3151     19177870 :   if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
    3152      7727774 :     type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
    3153              :   else
    3154     11450096 :     type = TREE_TYPE (TREE_OPERAND (expr, 1));
    3155              : 
    3156              :   /* One could argue that all the stuff below is not necessary for
    3157              :      the non-bitfield case and declare it a FE error if type
    3158              :      adjustment would be needed.  */
    3159     19177870 :   if (TREE_TYPE (expr) != type)
    3160              :     {
    3161              : #ifdef ENABLE_TYPES_CHECKING
    3162       801174 :       tree old_type = TREE_TYPE (expr);
    3163              : #endif
    3164       801174 :       auto type_quals = TYPE_QUALS (type);
    3165       801174 :       gcc_checking_assert (/* Fields should lack address space
    3166              :                               qualification.  */
    3167              :                            ADDR_SPACE_GENERIC_P (type_quals.addr_space ()));
    3168              : 
    3169              :       /* We need to preserve qualifiers and propagate them from
    3170              :          operand 0.  */
    3171       801174 :       addr_space_t op_as;
    3172       801174 :       cv_qualifier op_cv;
    3173       801174 :       std::tie (op_cv, op_as)
    3174       801174 :         = TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0))).split ();
    3175       801174 :       type_quals |= op_cv;
    3176       801174 :       type_quals.set_as (op_as);
    3177       801174 :       if (TYPE_QUALS (type) != type_quals)
    3178       800925 :         type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
    3179              : 
    3180              :       /* Set the type of the COMPONENT_REF to the underlying type.  */
    3181       801174 :       TREE_TYPE (expr) = type;
    3182              : 
    3183              : #ifdef ENABLE_TYPES_CHECKING
    3184              :       /* It is now a FE error, if the conversion from the canonical
    3185              :          type to the original expression type is not useless.  */
    3186       801174 :       gcc_assert (useless_type_conversion_p (old_type, type));
    3187              : #endif
    3188              :     }
    3189     19177870 : }
    3190              : 
    3191              : /* If a NOP conversion is changing a pointer to array of foo to a pointer
    3192              :    to foo, embed that change in the ADDR_EXPR by converting
    3193              :       T array[U];
    3194              :       (T *)&array
    3195              :    ==>
    3196              :       &array[L]
    3197              :    where L is the lower bound.  For simplicity, only do this for constant
    3198              :    lower bound.
    3199              :    The constraint is that the type of &array[L] is trivially convertible
    3200              :    to T *.  */
    3201              : 
    3202              : static void
    3203       572180 : canonicalize_addr_expr (tree *expr_p)
    3204              : {
    3205       572180 :   tree expr = *expr_p;
    3206       572180 :   tree addr_expr = TREE_OPERAND (expr, 0);
    3207       572180 :   tree datype, ddatype, pddatype;
    3208              : 
    3209              :   /* We simplify only conversions from an ADDR_EXPR to a pointer type.  */
    3210      1144014 :   if (!POINTER_TYPE_P (TREE_TYPE (expr))
    3211       572180 :       || TREE_CODE (addr_expr) != ADDR_EXPR)
    3212              :     return;
    3213              : 
    3214              :   /* The addr_expr type should be a pointer to an array.  */
    3215          346 :   datype = TREE_TYPE (TREE_TYPE (addr_expr));
    3216          346 :   if (TREE_CODE (datype) != ARRAY_TYPE)
    3217              :     return;
    3218              : 
    3219              :   /* The pointer to element type shall be trivially convertible to
    3220              :      the expression pointer type.  */
    3221           28 :   ddatype = TREE_TYPE (datype);
    3222           28 :   pddatype = build_pointer_type (ddatype);
    3223           28 :   if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
    3224              :                                   pddatype))
    3225              :     return;
    3226              : 
    3227              :   /* The lower bound and element sizes must be constant.  */
    3228            0 :   if (!TYPE_SIZE_UNIT (ddatype)
    3229            0 :       || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
    3230            0 :       || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
    3231            0 :       || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
    3232              :     return;
    3233              : 
    3234              :   /* All checks succeeded.  Build a new node to merge the cast.  */
    3235            0 :   *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
    3236            0 :                     TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
    3237              :                     NULL_TREE, NULL_TREE);
    3238            0 :   *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
    3239              : 
    3240              :   /* We can have stripped a required restrict qualifier above.  */
    3241            0 :   if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
    3242            0 :     *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
    3243              : }
    3244              : 
    3245              : /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR.  Remove it and/or other conversions
    3246              :    underneath as appropriate.  */
    3247              : 
    3248              : static enum gimplify_status
    3249     13484866 : gimplify_conversion (tree *expr_p)
    3250              : {
    3251     13484866 :   location_t loc = EXPR_LOCATION (*expr_p);
    3252     13484866 :   gcc_assert (CONVERT_EXPR_P (*expr_p));
    3253              : 
    3254              :   /* Then strip away all but the outermost conversion.  */
    3255     13484866 :   STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
    3256              : 
    3257              :   /* And remove the outermost conversion if it's useless.  */
    3258     13484866 :   if (tree_ssa_useless_type_conversion (*expr_p))
    3259            0 :     *expr_p = TREE_OPERAND (*expr_p, 0);
    3260              : 
    3261              :   /* If we still have a conversion at the toplevel,
    3262              :      then canonicalize some constructs.  */
    3263     13484866 :   if (CONVERT_EXPR_P (*expr_p))
    3264              :     {
    3265     13484866 :       tree sub = TREE_OPERAND (*expr_p, 0);
    3266              : 
    3267              :       /* If a NOP conversion is changing the type of a COMPONENT_REF
    3268              :          expression, then canonicalize its type now in order to expose more
    3269              :          redundant conversions.  */
    3270     13484866 :       if (TREE_CODE (sub) == COMPONENT_REF)
    3271       282680 :         canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
    3272              : 
    3273              :       /* If a NOP conversion is changing a pointer to array of foo
    3274              :          to a pointer to foo, embed that change in the ADDR_EXPR.  */
    3275     13202186 :       else if (TREE_CODE (sub) == ADDR_EXPR)
    3276       572180 :         canonicalize_addr_expr (expr_p);
    3277              :     }
    3278              : 
    3279              :   /* If we have a conversion to a non-register type force the
    3280              :      use of a VIEW_CONVERT_EXPR instead.  */
    3281     13484866 :   if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
    3282           90 :     *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
    3283           90 :                                TREE_OPERAND (*expr_p, 0));
    3284              : 
    3285              :   /* Canonicalize CONVERT_EXPR to NOP_EXPR.  */
    3286     13484866 :   if (TREE_CODE (*expr_p) == CONVERT_EXPR)
    3287       236577 :     TREE_SET_CODE (*expr_p, NOP_EXPR);
    3288              : 
    3289     13484866 :   return GS_OK;
    3290              : }
    3291              : 
    3292              : /* Gimplify a VAR_DECL or PARM_DECL.  Return GS_OK if we expanded a
    3293              :    DECL_VALUE_EXPR, and it's worth re-examining things.  */
    3294              : 
    3295              : static enum gimplify_status
    3296    131449429 : gimplify_var_or_parm_decl (tree *expr_p)
    3297              : {
    3298    131449429 :   tree decl = *expr_p;
    3299              : 
    3300              :   /* ??? If this is a local variable, and it has not been seen in any
    3301              :      outer BIND_EXPR, then it's probably the result of a duplicate
    3302              :      declaration, for which we've already issued an error.  It would
    3303              :      be really nice if the front end wouldn't leak these at all.
    3304              :      Currently the only known culprit is C++ destructors, as seen
    3305              :      in g++.old-deja/g++.jason/binding.C.
    3306              :      Another possible culpit are size expressions for variably modified
    3307              :      types which are lost in the FE or not gimplified correctly.  */
    3308    131449429 :   if (VAR_P (decl)
    3309    102682319 :       && !DECL_SEEN_IN_BIND_EXPR_P (decl)
    3310     17516254 :       && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
    3311    131509896 :       && decl_function_context (decl) == current_function_decl)
    3312              :     {
    3313           77 :       gcc_assert (seen_error ());
    3314              :       return GS_ERROR;
    3315              :     }
    3316              : 
    3317              :   /* When within an OMP context, notice uses of variables.  */
    3318    131449352 :   if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
    3319              :     return GS_ALL_DONE;
    3320              : 
    3321              :   /* If the decl is an alias for another expression, substitute it now.  */
    3322    131443604 :   if (DECL_HAS_VALUE_EXPR_P (decl))
    3323              :     {
    3324       477633 :       *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
    3325       477633 :       return GS_OK;
    3326              :     }
    3327              : 
    3328              :   return GS_ALL_DONE;
    3329              : }
    3330              : 
    3331              : /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T.  */
    3332              : 
    3333              : static void
    3334    114631780 : recalculate_side_effects (tree t)
    3335              : {
    3336    114631780 :   enum tree_code code = TREE_CODE (t);
    3337    114631780 :   int len = TREE_OPERAND_LENGTH (t);
    3338    114631780 :   int i;
    3339              : 
    3340    114631780 :   switch (TREE_CODE_CLASS (code))
    3341              :     {
    3342       675858 :     case tcc_expression:
    3343       675858 :       switch (code)
    3344              :         {
    3345              :         case INIT_EXPR:
    3346              :         case MODIFY_EXPR:
    3347              :         case VA_ARG_EXPR:
    3348              :         case PREDECREMENT_EXPR:
    3349              :         case PREINCREMENT_EXPR:
    3350              :         case POSTDECREMENT_EXPR:
    3351              :         case POSTINCREMENT_EXPR:
    3352              :           /* All of these have side-effects, no matter what their
    3353              :              operands are.  */
    3354              :           return;
    3355              : 
    3356              :         default:
    3357              :           break;
    3358              :         }
    3359              :       /* Fall through.  */
    3360              : 
    3361    114631776 :     case tcc_comparison:  /* a comparison expression */
    3362    114631776 :     case tcc_unary:       /* a unary arithmetic expression */
    3363    114631776 :     case tcc_binary:      /* a binary arithmetic expression */
    3364    114631776 :     case tcc_reference:   /* a reference */
    3365    114631776 :     case tcc_vl_exp:        /* a function call */
    3366    114631776 :       TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
    3367    373607737 :       for (i = 0; i < len; ++i)
    3368              :         {
    3369    258975961 :           tree op = TREE_OPERAND (t, i);
    3370    258975961 :           if (op && TREE_SIDE_EFFECTS (op))
    3371      1365360 :             TREE_SIDE_EFFECTS (t) = 1;
    3372              :         }
    3373              :       break;
    3374              : 
    3375              :     case tcc_constant:
    3376              :       /* No side-effects.  */
    3377              :       return;
    3378              : 
    3379              :     case tcc_declaration:
    3380              :       /* These can have side-effects if TREE_THIS_VOLATILE,
    3381              :          but those should be set elsewhere, not in
    3382              :          recalculate_side_effects.  Can be triggered e.g. if
    3383              :          a comparison is folded into one of its operands.  */
    3384              :       return;
    3385              : 
    3386            2 :     default:
    3387            2 :       if (code == SSA_NAME)
    3388              :         /* No side-effects.  */
    3389              :         return;
    3390            0 :       gcc_unreachable ();
    3391              :    }
    3392              : }
    3393              : 
    3394              : /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
    3395              :    node *EXPR_P.
    3396              : 
    3397              :       compound_lval
    3398              :               : min_lval '[' val ']'
    3399              :               | min_lval '.' ID
    3400              :               | compound_lval '[' val ']'
    3401              :               | compound_lval '.' ID
    3402              : 
    3403              :    This is not part of the original SIMPLE definition, which separates
    3404              :    array and member references, but it seems reasonable to handle them
    3405              :    together.  Also, this way we don't run into problems with union
    3406              :    aliasing; gcc requires that for accesses through a union to alias, the
    3407              :    union reference must be explicit, which was not always the case when we
    3408              :    were splitting up array and member refs.
    3409              : 
    3410              :    PRE_P points to the sequence where side effects that must happen before
    3411              :      *EXPR_P should be stored.
    3412              : 
    3413              :    POST_P points to the sequence where side effects that must happen after
    3414              :      *EXPR_P should be stored.  */
    3415              : 
    3416              : static enum gimplify_status
    3417     31206600 : gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
    3418              :                         fallback_t fallback)
    3419              : {
    3420     31206600 :   tree *p;
    3421     31206600 :   enum gimplify_status ret = GS_ALL_DONE, tret;
    3422     31206600 :   int i;
    3423     31206600 :   location_t loc = EXPR_LOCATION (*expr_p);
    3424     31206600 :   tree expr = *expr_p;
    3425              : 
    3426              :   /* Create a stack of the subexpressions so later we can walk them in
    3427              :      order from inner to outer.  */
    3428     31206600 :   auto_vec<tree, 10> expr_stack;
    3429              : 
    3430              :   /* We can handle anything that get_inner_reference can deal with.  */
    3431     75364838 :   for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
    3432              :     {
    3433     44225614 :     restart:
    3434              :       /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs.  */
    3435     75432214 :       if (TREE_CODE (*p) == INDIRECT_REF)
    3436      4086813 :         *p = fold_indirect_ref_loc (loc, *p);
    3437              : 
    3438     75432214 :       if (handled_component_p (*p))
    3439              :         ;
    3440              :       /* Expand DECL_VALUE_EXPR now.  In some cases that may expose
    3441              :          additional COMPONENT_REFs.  */
    3442     13700897 :       else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
    3443     32730149 :                && gimplify_var_or_parm_decl (p) == GS_OK)
    3444        67376 :         goto restart;
    3445              :       else
    3446              :         break;
    3447              : 
    3448     44158238 :       expr_stack.safe_push (*p);
    3449              :     }
    3450              : 
    3451     31206600 :   gcc_assert (expr_stack.length ());
    3452              : 
    3453              :   /* Now EXPR_STACK is a stack of pointers to all the refs we've
    3454              :      walked through and P points to the innermost expression.
    3455              : 
    3456              :      Java requires that we elaborated nodes in source order.  That
    3457              :      means we must gimplify the inner expression followed by each of
    3458              :      the indices, in order.  But we can't gimplify the inner
    3459              :      expression until we deal with any variable bounds, sizes, or
    3460              :      positions in order to deal with PLACEHOLDER_EXPRs.
    3461              : 
    3462              :      The base expression may contain a statement expression that
    3463              :      has declarations used in size expressions, so has to be
    3464              :      gimplified before gimplifying the size expressions.
    3465              : 
    3466              :      So we do this in three steps.  First we deal with variable
    3467              :      bounds, sizes, and positions, then we gimplify the base and
    3468              :      ensure it is memory if needed, then we deal with the annotations
    3469              :      for any variables in the components and any indices, from left
    3470              :      to right.  */
    3471              : 
    3472     31206600 :   bool need_non_reg = false;
    3473     75364835 :   for (i = expr_stack.length () - 1; i >= 0; i--)
    3474              :     {
    3475     44158238 :       tree t = expr_stack[i];
    3476              : 
    3477     44158238 :       if (error_operand_p (TREE_OPERAND (t, 0)))
    3478              :         return GS_ERROR;
    3479              : 
    3480     44158235 :       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
    3481              :         {
    3482              :           /* Deal with the low bound and element type size and put them into
    3483              :              the ARRAY_REF.  If these values are set, they have already been
    3484              :              gimplified.  */
    3485      9293731 :           if (TREE_OPERAND (t, 2) == NULL_TREE)
    3486              :             {
    3487      9226622 :               tree low = unshare_expr (array_ref_low_bound (t));
    3488      9226622 :               if (!is_gimple_min_invariant (low))
    3489              :                 {
    3490            0 :                   TREE_OPERAND (t, 2) = low;
    3491              :                 }
    3492              :             }
    3493              : 
    3494      9293731 :           if (TREE_OPERAND (t, 3) == NULL_TREE)
    3495              :             {
    3496      9279676 :               tree elmt_size = array_ref_element_size (t);
    3497      9279676 :               if (!is_gimple_min_invariant (elmt_size))
    3498              :                 {
    3499         8805 :                   elmt_size = unshare_expr (elmt_size);
    3500         8805 :                   tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
    3501         8805 :                   tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
    3502              : 
    3503              :                   /* Divide the element size by the alignment of the element
    3504              :                      type (above).  */
    3505         8805 :                   elmt_size = size_binop_loc (loc, EXACT_DIV_EXPR,
    3506              :                                               elmt_size, factor);
    3507              : 
    3508         8805 :                   TREE_OPERAND (t, 3) = elmt_size;
    3509              :                 }
    3510              :             }
    3511              :           need_non_reg = true;
    3512              :         }
    3513     34864504 :       else if (TREE_CODE (t) == COMPONENT_REF)
    3514              :         {
    3515              :           /* Set the field offset into T and gimplify it.  */
    3516     33532734 :           if (TREE_OPERAND (t, 2) == NULL_TREE)
    3517              :             {
    3518     33532355 :               tree offset = component_ref_field_offset (t);
    3519     33532355 :               if (!is_gimple_min_invariant (offset))
    3520              :                 {
    3521          287 :                   offset = unshare_expr (offset);
    3522          287 :                   tree field = TREE_OPERAND (t, 1);
    3523          287 :                   tree factor
    3524          287 :                     = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
    3525              : 
    3526              :                   /* Divide the offset by its alignment.  */
    3527          287 :                   offset = size_binop_loc (loc, EXACT_DIV_EXPR,
    3528              :                                            offset, factor);
    3529              : 
    3530          287 :                   TREE_OPERAND (t, 2) = offset;
    3531              :                 }
    3532              :             }
    3533              :           need_non_reg = true;
    3534              :         }
    3535      1331770 :       else if (!is_gimple_reg_type (TREE_TYPE (t)))
    3536              :         /* When the result of an operation, in particular a VIEW_CONVERT_EXPR
    3537              :            is a non-register type then require the base object to be a
    3538              :            non-register as well.  */
    3539     43122244 :         need_non_reg = true;
    3540              :     }
    3541              : 
    3542              :   /* Step 2 is to gimplify the base expression.  Make sure lvalue is set
    3543              :      so as to match the min_lval predicate.  Failure to do so may result
    3544              :      in the creation of large aggregate temporaries.  */
    3545     31206597 :   tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
    3546              :                         fallback | fb_lvalue);
    3547     31206597 :   ret = MIN (ret, tret);
    3548     31206597 :   if (ret == GS_ERROR)
    3549              :     return GS_ERROR;
    3550              : 
    3551              :   /* Step 2a: if we have component references we do not support on
    3552              :      registers then make sure the base isn't a register.  Of course
    3553              :      we can only do so if an rvalue is OK.  */
    3554     31206597 :   if (need_non_reg && (fallback & fb_rvalue))
    3555     23176400 :     prepare_gimple_addressable (p, pre_p);
    3556              : 
    3557              : 
    3558              :   /* Step 3: gimplify size expressions and the indices and operands of
    3559              :      ARRAY_REF.  During this loop we also remove any useless conversions.
    3560              :      If we operate on a register also make sure to properly gimplify
    3561              :      to individual operations.  */
    3562              : 
    3563     31206597 :   bool reg_operations = is_gimple_reg (*p);
    3564    106571429 :   for (; expr_stack.length () > 0; )
    3565              :     {
    3566     44158235 :       tree t = expr_stack.pop ();
    3567              : 
    3568     44158235 :       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
    3569              :         {
    3570      9293731 :           gcc_assert (!reg_operations);
    3571              : 
    3572              :           /* Gimplify the low bound and element type size. */
    3573      9293731 :           tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
    3574              :                                 is_gimple_reg, fb_rvalue);
    3575      9293731 :           ret = MIN (ret, tret);
    3576              : 
    3577      9293731 :           tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
    3578              :                                 is_gimple_reg, fb_rvalue);
    3579      9293731 :           ret = MIN (ret, tret);
    3580              : 
    3581              :           /* Gimplify the dimension.  */
    3582      9293731 :           tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
    3583              :                                 is_gimple_val, fb_rvalue);
    3584      9293731 :           ret = MIN (ret, tret);
    3585              :         }
    3586     34864504 :       else if (TREE_CODE (t) == COMPONENT_REF)
    3587              :         {
    3588     33532734 :           gcc_assert (!reg_operations);
    3589              : 
    3590     33532734 :           tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
    3591              :                                 is_gimple_reg, fb_rvalue);
    3592     33532734 :           ret = MIN (ret, tret);
    3593              :         }
    3594      1331770 :       else if (reg_operations)
    3595              :         {
    3596       917061 :           tret = gimplify_expr (&TREE_OPERAND (t, 0), pre_p, post_p,
    3597              :                                 is_gimple_val, fb_rvalue);
    3598      1331770 :           ret = MIN (ret, tret);
    3599              :         }
    3600              : 
    3601     44158235 :       STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
    3602              : 
    3603              :       /* The innermost expression P may have originally had
    3604              :          TREE_SIDE_EFFECTS set which would have caused all the outer
    3605              :          expressions in *EXPR_P leading to P to also have had
    3606              :          TREE_SIDE_EFFECTS set.  */
    3607     44158235 :       recalculate_side_effects (t);
    3608              :     }
    3609              : 
    3610              :   /* If the outermost expression is a COMPONENT_REF, canonicalize its type.  */
    3611     31206597 :   if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
    3612              :     {
    3613     18895190 :       canonicalize_component_ref (expr_p);
    3614              :     }
    3615              : 
    3616     31206597 :   expr_stack.release ();
    3617              : 
    3618     31206597 :   gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
    3619              : 
    3620              :   return ret;
    3621     31206600 : }
    3622              : 
    3623              : /*  Gimplify the self modifying expression pointed to by EXPR_P
    3624              :     (++, --, +=, -=).
    3625              : 
    3626              :     PRE_P points to the list where side effects that must happen before
    3627              :         *EXPR_P should be stored.
    3628              : 
    3629              :     POST_P points to the list where side effects that must happen after
    3630              :         *EXPR_P should be stored.
    3631              : 
    3632              :     WANT_VALUE is nonzero iff we want to use the value of this expression
    3633              :         in another expression.
    3634              : 
    3635              :     ARITH_TYPE is the type the computation should be performed in.  */
    3636              : 
    3637              : enum gimplify_status
    3638      1156699 : gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
    3639              :                         bool want_value, tree arith_type)
    3640              : {
    3641      1156699 :   enum tree_code code;
    3642      1156699 :   tree lhs, lvalue, rhs, t1;
    3643      1156699 :   gimple_seq post = NULL, *orig_post_p = post_p;
    3644      1156699 :   bool postfix;
    3645      1156699 :   enum tree_code arith_code;
    3646      1156699 :   enum gimplify_status ret;
    3647      1156699 :   location_t loc = EXPR_LOCATION (*expr_p);
    3648              : 
    3649      1156699 :   code = TREE_CODE (*expr_p);
    3650              : 
    3651      1156699 :   gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
    3652              :               || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
    3653              : 
    3654              :   /* Prefix or postfix?  */
    3655      1156699 :   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
    3656              :     /* Faster to treat as prefix if result is not used.  */
    3657              :     postfix = want_value;
    3658              :   else
    3659              :     postfix = false;
    3660              : 
    3661              :   /* For postfix, make sure the inner expression's post side effects
    3662              :      are executed after side effects from this expression.  */
    3663       416143 :   if (postfix)
    3664      1156699 :     post_p = &post;
    3665              : 
    3666              :   /* Add or subtract?  */
    3667      1156699 :   if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
    3668              :     arith_code = PLUS_EXPR;
    3669              :   else
    3670        45978 :     arith_code = MINUS_EXPR;
    3671              : 
    3672              :   /* Gimplify the LHS into a GIMPLE lvalue.  */
    3673      1156699 :   lvalue = TREE_OPERAND (*expr_p, 0);
    3674      1156699 :   ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
    3675      1156699 :   if (ret == GS_ERROR)
    3676              :     return ret;
    3677              : 
    3678              :   /* Extract the operands to the arithmetic operation.  */
    3679      1156696 :   lhs = lvalue;
    3680      1156696 :   rhs = TREE_OPERAND (*expr_p, 1);
    3681              : 
    3682              :   /* For postfix operator, we evaluate the LHS to an rvalue and then use
    3683              :      that as the result value and in the postqueue operation.  */
    3684      1156696 :   if (postfix)
    3685              :     {
    3686       190059 :       ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
    3687       190059 :       if (ret == GS_ERROR)
    3688              :         return ret;
    3689              : 
    3690       190059 :       lhs = get_initialized_tmp_var (lhs, pre_p);
    3691              :     }
    3692              : 
    3693              :   /* For POINTERs increment, use POINTER_PLUS_EXPR.  */
    3694      1156696 :   if (POINTER_TYPE_P (TREE_TYPE (lhs)))
    3695              :     {
    3696       237566 :       rhs = convert_to_ptrofftype_loc (loc, rhs);
    3697       237566 :       if (arith_code == MINUS_EXPR)
    3698        10242 :         rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
    3699       237566 :       t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
    3700              :     }
    3701              :   else
    3702       919130 :     t1 = fold_convert (TREE_TYPE (*expr_p),
    3703              :                        fold_build2 (arith_code, arith_type,
    3704              :                                     fold_convert (arith_type, lhs),
    3705              :                                     fold_convert (arith_type, rhs)));
    3706              : 
    3707      1156696 :   if (postfix)
    3708              :     {
    3709       190059 :       gimplify_assign (lvalue, t1, pre_p);
    3710       190059 :       gimplify_seq_add_seq (orig_post_p, post);
    3711       190059 :       *expr_p = lhs;
    3712       190059 :       return GS_ALL_DONE;
    3713              :     }
    3714              :   else
    3715              :     {
    3716       966637 :       *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
    3717       966637 :       return GS_OK;
    3718              :     }
    3719              : }
    3720              : 
    3721              : /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR.  */
    3722              : 
    3723              : static void
    3724     84168732 : maybe_with_size_expr (tree *expr_p)
    3725              : {
    3726     84168732 :   tree expr = *expr_p;
    3727     84168732 :   tree type = TREE_TYPE (expr);
    3728     84168732 :   tree size;
    3729              : 
    3730              :   /* If we've already wrapped this or the type is error_mark_node, we can't do
    3731              :      anything.  */
    3732     84168732 :   if (TREE_CODE (expr) == WITH_SIZE_EXPR
    3733     84168607 :       || type == error_mark_node)
    3734              :     return;
    3735              : 
    3736              :   /* If the size isn't known or is a constant, we have nothing to do.  */
    3737     84168459 :   size = TYPE_SIZE_UNIT (type);
    3738     84168459 :   if (!size || poly_int_tree_p (size))
    3739              :     return;
    3740              : 
    3741              :   /* Otherwise, make a WITH_SIZE_EXPR.  */
    3742         1705 :   size = unshare_expr (size);
    3743         1705 :   size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
    3744         1705 :   *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
    3745              : }
    3746              : 
    3747              : /* Helper for gimplify_call_expr.  Gimplify a single argument *ARG_P
    3748              :    Store any side-effects in PRE_P.  CALL_LOCATION is the location of
    3749              :    the CALL_EXPR.  If ALLOW_SSA is set the actual parameter may be
    3750              :    gimplified to an SSA name.  */
    3751              : 
    3752              : enum gimplify_status
    3753     34947359 : gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
    3754              :               bool allow_ssa)
    3755              : {
    3756     34947359 :   bool (*test) (tree);
    3757     34947359 :   fallback_t fb;
    3758              : 
    3759              :   /* In general, we allow lvalues for function arguments to avoid
    3760              :      extra overhead of copying large aggregates out of even larger
    3761              :      aggregates into temporaries only to copy the temporaries to
    3762              :      the argument list.  Make optimizers happy by pulling out to
    3763              :      temporaries those types that fit in registers.  */
    3764     34947359 :   if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
    3765              :     test = is_gimple_val, fb = fb_rvalue;
    3766              :   else
    3767              :     {
    3768      1625263 :       test = is_gimple_lvalue, fb = fb_either;
    3769              :       /* Also strip a TARGET_EXPR that would force an extra copy.  */
    3770      1625263 :       if (TREE_CODE (*arg_p) == TARGET_EXPR)
    3771              :         {
    3772       340142 :           tree init = TARGET_EXPR_INITIAL (*arg_p);
    3773       340142 :           if (init
    3774       340139 :               && !VOID_TYPE_P (TREE_TYPE (init))
    3775              :               /* Currently, due to c++/116015, it is not desirable to
    3776              :                  strip a TARGET_EXPR whose initializer is a {}.  The
    3777              :                  problem is that if we do elide it, we also have to
    3778              :                  replace all the occurrences of the slot temporary in the
    3779              :                  initializer with the temporary created for the argument.
    3780              :                  But we do not have that temporary yet so the replacement
    3781              :                  would be quite awkward and it might be needed to resort
    3782              :                  back to a PLACEHOLDER_EXPR.  Note that stripping the
    3783              :                  TARGET_EXPR wouldn't help anyway, as gimplify_expr would
    3784              :                  just allocate a temporary to store the CONSTRUCTOR into.
    3785              :                  (FIXME PR116375.)
    3786              : 
    3787              :                  See convert_for_arg_passing for the C++ code that marks
    3788              :                  the TARGET_EXPR as eliding or not.  */
    3789       638264 :               && TREE_CODE (init) != CONSTRUCTOR)
    3790       258664 :             *arg_p = init;
    3791              :         }
    3792              :     }
    3793              : 
    3794              :   /* If this is a variable sized type, we must remember the size.  */
    3795     34947359 :   maybe_with_size_expr (arg_p);
    3796              : 
    3797              :   /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c.  */
    3798              :   /* Make sure arguments have the same location as the function call
    3799              :      itself.  */
    3800     34947359 :   protected_set_expr_location (*arg_p, call_location);
    3801              : 
    3802              :   /* There is a sequence point before a function call.  Side effects in
    3803              :      the argument list must occur before the actual call. So, when
    3804              :      gimplifying arguments, force gimplify_expr to use an internal
    3805              :      post queue which is then appended to the end of PRE_P.  */
    3806     34947359 :   return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
    3807              : }
    3808              : 
    3809              : /* Don't fold inside offloading or taskreg regions: it can break code by
    3810              :    adding decl references that weren't in the source.  We'll do it during
    3811              :    omplower pass instead.  */
    3812              : 
    3813              : static bool
    3814     61065877 : maybe_fold_stmt (gimple_stmt_iterator *gsi)
    3815              : {
    3816     61065877 :   struct gimplify_omp_ctx *ctx;
    3817     61622325 :   for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
    3818      1247342 :     if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
    3819              :       return false;
    3820       561473 :     else if ((ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
    3821              :       return false;
    3822              :   /* Delay folding of builtins until the IL is in consistent state
    3823              :      so the diagnostic machinery can do a better job.  */
    3824     60374983 :   if (gimple_call_builtin_p (gsi_stmt (*gsi)))
    3825              :     return false;
    3826     57947616 :   return fold_stmt (gsi);
    3827              : }
    3828              : 
    3829              : static tree
    3830              : expand_late_variant_directive (vec<struct omp_variant> all_candidates,
    3831              :                                tree construct_context);
    3832              : 
    3833              : 
    3834              : /* Helper function for calls to omp_dynamic_cond: find the current
    3835              :    enclosing block in the gimplification context.  */
    3836              : static tree
    3837          370 : find_supercontext (void)
    3838              : {
    3839          370 :   vec<gbind *>stack = gimple_bind_expr_stack ();
    3840          708 :   for (int i = stack.length () - 1; i >= 0; i++)
    3841              :     {
    3842          338 :       gbind *b = stack[i];
    3843          338 :       if (b->block)
    3844              :         return b->block;
    3845              :     }
    3846              :   return NULL_TREE;
    3847              : }
    3848              : 
    3849              : /* OpenMP: Handle the append_args and adjust_args clauses of
    3850              :    declare_variant for EXPR, which is a CALL_EXPR whose CALL_EXPR_FN
    3851              :    is the variant, within a dispatch construct with clauses DISPATCH_CLAUSES.
    3852              :    WANT_VALUE and POINTERIZE are as for expand_variant_call_expr.
    3853              : 
    3854              :    'append_args' causes interop objects are added after the last regular
    3855              :    (nonhidden, nonvariadic) arguments of the variant function.
    3856              :    'adjust_args' with need_device_{addr,ptr} converts the pointer target of
    3857              :    a pointer from a host to a device address. This uses either the default
    3858              :    device or the passed device number, which then sets the default device
    3859              :    address.  */
    3860              : static tree
    3861          471 : modify_call_for_omp_dispatch (tree expr, tree dispatch_clauses,
    3862              :                               bool want_value, bool pointerize)
    3863              : {
    3864          471 :   location_t loc = EXPR_LOCATION (expr);
    3865          471 :   tree fndecl = get_callee_fndecl (expr);
    3866              : 
    3867              :   /* Skip processing if we don't get the expected call form.  */
    3868          471 :   if (!fndecl)
    3869              :     return expr;
    3870              : 
    3871          471 :   tree init_code = NULL_TREE;
    3872          471 :   tree cleanup = NULL_TREE;
    3873          471 :   tree clobbers = NULL_TREE;
    3874          471 :   int nargs = call_expr_nargs (expr);
    3875          471 :   tree dispatch_device_num = NULL_TREE;
    3876          471 :   tree dispatch_interop = NULL_TREE;
    3877          471 :   tree dispatch_append_args = NULL_TREE;
    3878          471 :   int nfirst_args = 0;
    3879          471 :   tree dispatch_adjust_args_list
    3880          471 :     = lookup_attribute ("omp declare variant variant args",
    3881          471 :                         DECL_ATTRIBUTES (fndecl));
    3882              : 
    3883          471 :   if (dispatch_adjust_args_list)
    3884              :     {
    3885          389 :       dispatch_adjust_args_list = TREE_VALUE (dispatch_adjust_args_list);
    3886          389 :       dispatch_append_args = TREE_CHAIN (dispatch_adjust_args_list);
    3887          389 :       if (TREE_PURPOSE (dispatch_adjust_args_list) == NULL_TREE
    3888          389 :           && TREE_VALUE (dispatch_adjust_args_list) == NULL_TREE)
    3889              :         dispatch_adjust_args_list = NULL_TREE;
    3890              :     }
    3891          389 :   if (dispatch_append_args)
    3892              :     {
    3893          245 :       nfirst_args = tree_to_shwi (TREE_PURPOSE (dispatch_append_args));
    3894          245 :       dispatch_append_args = TREE_VALUE (dispatch_append_args);
    3895              :     }
    3896          471 :   dispatch_device_num = omp_find_clause (dispatch_clauses, OMP_CLAUSE_DEVICE);
    3897          471 :   if (dispatch_device_num)
    3898          287 :     dispatch_device_num = OMP_CLAUSE_DEVICE_ID (dispatch_device_num);
    3899          471 :   dispatch_interop = omp_find_clause (dispatch_clauses, OMP_CLAUSE_INTEROP);
    3900          471 :   int nappend = 0, ninterop = 0;
    3901         1351 :   for (tree t = dispatch_append_args; t; t = TREE_CHAIN (t))
    3902          409 :     nappend++;
    3903              : 
    3904              :   /* FIXME: error checking should be taken out of this function and
    3905              :      handled before any attempt at filtering or resolution happens.
    3906              :      Otherwise whether or not diagnostics appear is determined by
    3907              :      GCC internals, how good the front ends are at constant-folding,
    3908              :      the split between early/late resolution, etc instead of the code
    3909              :      as written by the user.  */
    3910          471 :   if (dispatch_interop)
    3911          861 :     for (tree t = dispatch_interop; t; t = TREE_CHAIN (t))
    3912          605 :       if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
    3913          409 :         ninterop++;
    3914          471 :   if (dispatch_interop && !dispatch_device_num)
    3915              :     {
    3916            7 :       gcc_checking_assert (ninterop > 1);
    3917            7 :       error_at (OMP_CLAUSE_LOCATION (dispatch_interop),
    3918              :                 "the %<device%> clause must be present if the %<interop%> "
    3919              :                 "clause has more than one list item");
    3920              :     }
    3921          471 :   if (nappend < ninterop)
    3922              :     {
    3923           48 :       error_at (OMP_CLAUSE_LOCATION (dispatch_interop),
    3924              :                 "number of list items in %<interop%> clause (%d) "
    3925              :                 "exceeds the number of %<append_args%> items (%d) for "
    3926              :                 "%<declare variant%> candidate %qD", ninterop, nappend, fndecl);
    3927           88 :       inform (dispatch_append_args
    3928            8 :               ? EXPR_LOCATION (TREE_PURPOSE (dispatch_append_args))
    3929           40 :               : DECL_SOURCE_LOCATION (fndecl),
    3930              :               "%<declare variant%> candidate %qD declared here", fndecl);
    3931           48 :       ninterop = nappend;
    3932              :     }
    3933          471 :   if (dispatch_append_args)
    3934              :     {
    3935          245 :       tree *buffer = XALLOCAVEC (tree, nargs + nappend);
    3936          245 :       tree arg = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
    3937              :       /* Copy the first arguments; insert then the interop objects,
    3938              :          and then copy the rest (nargs - nfirst_args) args.  */
    3939          245 :       int i;
    3940          679 :       for (i = 0; i < nfirst_args; i++)
    3941              :         {
    3942          434 :           arg = TREE_CHAIN (arg);
    3943          434 :           buffer[i] = CALL_EXPR_ARG (expr, i);
    3944              :         }
    3945              :       int j = ninterop;
    3946          583 :       for (tree t = dispatch_interop; t && j > 0; t = TREE_CHAIN (t))
    3947          338 :         if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
    3948          338 :           buffer[i + --j] = OMP_CLAUSE_DECL (t);
    3949          245 :       gcc_checking_assert (j == 0);
    3950              : 
    3951              :       /* Do we need to create additional interop objects?  */
    3952          245 :       if (ninterop < nappend)
    3953              :         {
    3954           56 :           if (dispatch_device_num == NULL_TREE)
    3955              :             /* Not remapping device number.  */
    3956           22 :             dispatch_device_num = build_int_cst (integer_type_node,
    3957              :                                                  GOMP_DEVICE_DEFAULT_OMP_61);
    3958           56 :           int nnew = nappend - ninterop;
    3959           56 :           tree nobjs = build_int_cst (integer_type_node, nnew);
    3960           56 :           tree a, t;
    3961              : 
    3962              :           /* Skip to the append_args clause for the first constructed
    3963              :              interop argument.  */
    3964           56 :           tree apparg = dispatch_append_args;
    3965           83 :           for (j = 0; j < ninterop; j++)
    3966           27 :             apparg = TREE_CHAIN (apparg);
    3967              : 
    3968              :           /* omp_interop_t *objs[n]; */
    3969           56 :           tree objtype = build_pointer_type (pointer_sized_int_node);
    3970           56 :           t = build_array_type_nelts (objtype, nnew);
    3971           56 :           tree objs = create_tmp_var (t, "interopobjs");
    3972              : 
    3973              :           /* int target_tgtsync[n]; */
    3974           56 :           t = build_array_type_nelts (integer_type_node, nnew);
    3975           56 :           tree target_tgtsync = create_tmp_var (t, "tgt_tgtsync");
    3976              : 
    3977              :           /* Scan first to determine if we need a prefer_type array.  */
    3978           56 :           tree prefer_type = NULL_TREE;
    3979           56 :           tree prefer_type_type = NULL_TREE;
    3980          141 :           for (j = ninterop, a = apparg; j < nappend; j++, a = TREE_CHAIN (a))
    3981           64 :             if (TREE_VALUE (a) != NULL_TREE)
    3982              :               {
    3983              :                 /* const char *prefer_type[n];  */
    3984           35 :                 t = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
    3985           35 :                 prefer_type_type = build_pointer_type (t);
    3986           35 :                 t = build_array_type_nelts (prefer_type_type, nnew);
    3987           35 :                 prefer_type = create_tmp_var (t, "pref_type");
    3988           35 :                 break;
    3989              :               }
    3990              : 
    3991              :           /* Initialize the arrays, generating temp vars and clobbers for
    3992              :              the interop objects.  (The constructed array holding the
    3993              :              pointers to these objects shouldn't need clobbering as there's
    3994              :              no reason for GOMP_interop to modify its contents.)  */
    3995          127 :           for (j = ninterop, a = apparg; j < nappend; j++, a = TREE_CHAIN (a))
    3996              :             {
    3997              :               /* The allocated temporaries for the interop objects
    3998              :                  have type omp_interop_t, which is an integer type that
    3999              :                  can encode a pointer.  */
    4000           71 :               tree objvar = create_tmp_var (pointer_sized_int_node, "interop");
    4001           71 :               buffer[i + j] = objvar;
    4002           71 :               TREE_ADDRESSABLE (objvar) = 1;
    4003              :               /* Generate a clobber for the temporary for when we're done
    4004              :                  with it.  */
    4005           71 :               tree c = build_clobber (pointer_sized_int_node,
    4006              :                                       CLOBBER_OBJECT_END);
    4007           71 :               c = build2 (MODIFY_EXPR, pointer_sized_int_node, objvar, c);
    4008           71 :               if (clobbers)
    4009           15 :                 clobbers = build2 (COMPOUND_EXPR, TREE_TYPE (clobbers),
    4010              :                                    c, clobbers);
    4011              :               else
    4012              :                 clobbers = c;
    4013              : 
    4014              :               /* objs[offset] = &objvar;  */
    4015           71 :               tree offset = build_int_cst (integer_type_node, j - ninterop);
    4016           71 :               tree init = build4 (ARRAY_REF, objtype, objs, offset,
    4017              :                                   NULL_TREE, NULL_TREE);
    4018           71 :               init = build2 (MODIFY_EXPR, objtype, init,
    4019              :                              build_fold_addr_expr (objvar));
    4020           71 :               if (init_code)
    4021           15 :                 init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
    4022              :                                     init_code, init);
    4023              :               else
    4024              :                 init_code = init;
    4025              : 
    4026              :               /* target_tgtsync[offset] = tgt;
    4027              :                  (Don't blame me, I didn't design the encoding of this
    4028              :                  info into the dispatch interop clause data structure,
    4029              :                  but the runtime wants a bit mask.)  */
    4030           71 :               tree tree_tgt = TREE_OPERAND (TREE_PURPOSE (a), 0);
    4031           71 :               int tgt = 0;
    4032           71 :               if (TREE_PURPOSE (tree_tgt) == boolean_true_node)
    4033           42 :                 tgt |= GOMP_INTEROP_TARGET;
    4034           71 :               if (TREE_VALUE (tree_tgt) == boolean_true_node)
    4035           36 :                 tgt |= GOMP_INTEROP_TARGETSYNC;
    4036           71 :               init = build4 (ARRAY_REF, integer_type_node,
    4037              :                              target_tgtsync, offset, NULL_TREE, NULL_TREE);
    4038           71 :               init = build2 (MODIFY_EXPR, integer_type_node, init,
    4039           71 :                              build_int_cst (integer_type_node, tgt));
    4040           71 :               init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
    4041              :                                   init_code, init);
    4042              : 
    4043           71 :               if (prefer_type)
    4044              :                 {
    4045           45 :                   tree pref = TREE_VALUE (a);
    4046           45 :                   if (pref == NULL_TREE)
    4047            6 :                     pref = null_pointer_node;
    4048              :                   else
    4049           39 :                     pref = build_fold_addr_expr (pref);
    4050           45 :                   init = build4 (ARRAY_REF, prefer_type_type, prefer_type,
    4051              :                                  offset, NULL_TREE, NULL_TREE);
    4052           45 :                   init = build2 (MODIFY_EXPR, prefer_type_type, init,
    4053              :                                  pref);
    4054           45 :                   init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
    4055              :                                       init_code, init);
    4056              :                 }
    4057              :             }
    4058              : 
    4059           56 :           objs = build_fold_addr_expr (objs);
    4060           56 :           target_tgtsync = build_fold_addr_expr (target_tgtsync);
    4061           56 :           prefer_type = prefer_type ? build_fold_addr_expr (prefer_type)
    4062              :                                     : null_pointer_node;
    4063           56 :           tree fn = builtin_decl_explicit (BUILT_IN_GOMP_INTEROP);
    4064           56 :           tree create
    4065           56 :             = build_call_expr_loc (loc, fn, 11, dispatch_device_num,
    4066              :                                    nobjs, objs, target_tgtsync, prefer_type,
    4067              :                                    integer_zero_node, null_pointer_node,
    4068              :                                    integer_zero_node, null_pointer_node,
    4069              :                                    integer_zero_node, null_pointer_node);
    4070           56 :           if (init_code)
    4071           56 :             init_code = build2 (COMPOUND_EXPR, TREE_TYPE (create),
    4072              :                                 init_code, create);
    4073              :           else
    4074              :             init_code = create;
    4075              : 
    4076           56 :           cleanup
    4077           56 :             = build_call_expr_loc (loc, fn, 11, dispatch_device_num,
    4078              :                                    integer_zero_node, null_pointer_node,
    4079              :                                    null_pointer_node, null_pointer_node,
    4080              :                                    integer_zero_node, null_pointer_node,
    4081              :                                    nobjs, objs,
    4082              :                                    integer_zero_node, null_pointer_node);
    4083           56 :           if (clobbers)
    4084           56 :             cleanup = build2 (COMPOUND_EXPR, TREE_TYPE (clobbers),
    4085              :                               cleanup, clobbers);
    4086              :         }
    4087              : 
    4088          654 :       for (j = 0; j < nappend; j++)
    4089              :         {
    4090              :           /* Fortran permits by-reference or by-value for the dummy arg
    4091              :              and by-value, by-reference, ptr by-reference as actual
    4092              :              argument. Handle this.  */
    4093          409 :           tree obj = buffer[i + j];  // interop object
    4094          409 :           tree a2 = TREE_VALUE (arg);  // parameter type
    4095          766 :           if (POINTER_TYPE_P (TREE_TYPE (obj))
    4096          436 :               && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (obj))))
    4097              :             {
    4098           32 :               tree t = TREE_TYPE (TREE_TYPE (obj));
    4099           32 :               gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (t)));
    4100           32 :               obj = fold_build1 (INDIRECT_REF, t, obj);
    4101              :             }
    4102          750 :           if (POINTER_TYPE_P (TREE_TYPE (obj))
    4103          420 :               && INTEGRAL_TYPE_P (a2))
    4104              :             {
    4105           43 :               tree t = TREE_TYPE (TREE_TYPE (obj));
    4106           43 :               gcc_checking_assert (INTEGRAL_TYPE_P (t));
    4107           43 :               obj = fold_build1 (INDIRECT_REF, t, obj);
    4108              :             }
    4109          497 :           else if (INTEGRAL_TYPE_P (TREE_TYPE (obj))
    4110          461 :                    && POINTER_TYPE_P (a2))
    4111              :             {
    4112           18 :               gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (a2)));
    4113           18 :               obj = build_fold_addr_expr (obj);
    4114              :             }
    4115          348 :           else if (!INTEGRAL_TYPE_P (a2)
    4116          348 :                    || !INTEGRAL_TYPE_P (TREE_TYPE (obj)))
    4117              :             {
    4118           36 :               tree t = TREE_TYPE (obj);
    4119           36 :               gcc_checking_assert (POINTER_TYPE_P (t)
    4120              :                                    && POINTER_TYPE_P (a2)
    4121              :                                    && INTEGRAL_TYPE_P (TREE_TYPE (t))
    4122              :                                    && INTEGRAL_TYPE_P (TREE_TYPE (a2)));
    4123              :             }
    4124          409 :           buffer[i + j] = obj;
    4125          409 :           arg = TREE_CHAIN (arg);
    4126              :         }
    4127          245 :       i += nappend;
    4128          534 :       for (j = nfirst_args; j < nargs; j++)
    4129          289 :         buffer[i++] = CALL_EXPR_ARG (expr, j);
    4130          245 :       nargs += nappend;
    4131          245 :       tree call = expr;
    4132          490 :       expr = build_call_array_loc (EXPR_LOCATION (expr), TREE_TYPE (call),
    4133          245 :                                    CALL_EXPR_FN (call), nargs, buffer);
    4134              : 
    4135              :       /* Copy all CALL_EXPR flags.  */
    4136          245 :       CALL_EXPR_STATIC_CHAIN (expr) = CALL_EXPR_STATIC_CHAIN (call);
    4137          245 :       CALL_EXPR_TAILCALL (expr) = CALL_EXPR_TAILCALL (call);
    4138          245 :       CALL_EXPR_RETURN_SLOT_OPT (expr)
    4139          245 :         = CALL_EXPR_RETURN_SLOT_OPT (call);
    4140          245 :       CALL_FROM_THUNK_P (expr) = CALL_FROM_THUNK_P (call);
    4141          245 :       SET_EXPR_LOCATION (expr, EXPR_LOCATION (call));
    4142          245 :       CALL_EXPR_VA_ARG_PACK (expr) = CALL_EXPR_VA_ARG_PACK (call);
    4143              :     }
    4144              : 
    4145              :   /* Nothing to do for adjust_args?  */
    4146          471 :   if (!dispatch_adjust_args_list || !TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
    4147          260 :     goto add_cleanup;
    4148              : 
    4149              :   /* Handle adjust_args.  */
    4150          966 :   for (int i = 0; i < nargs; i++)
    4151              :     {
    4152          755 :       tree *arg_p = &CALL_EXPR_ARG (expr, i);
    4153              : 
    4154              :       /* Nothing to do if arg is constant null pointer.  */
    4155          755 :       if (integer_zerop (*arg_p))
    4156            6 :         continue;
    4157              : 
    4158              :       bool need_device_ptr = false;
    4159              :       bool need_device_addr = false;
    4160         2247 :       for (int need_addr = 0; need_addr <= 1; need_addr++)
    4161         2301 :         for (tree arg = (need_addr
    4162         1498 :                          ? TREE_VALUE (dispatch_adjust_args_list)
    4163         1498 :                          : TREE_PURPOSE (dispatch_adjust_args_list));
    4164         2301 :              arg != NULL; arg = TREE_CHAIN (arg))
    4165              :           {
    4166         1152 :             if (TREE_VALUE (arg)
    4167         1152 :                 && TREE_CODE (TREE_VALUE (arg)) == INTEGER_CST
    4168         2304 :                 && wi::eq_p (i, wi::to_wide (TREE_VALUE (arg))))
    4169              :               {
    4170          349 :                 if (need_addr)
    4171              :                   need_device_addr = true;
    4172              :                 else
    4173          349 :                   need_device_ptr = true;
    4174              :                 break;
    4175              :               }
    4176              :           }
    4177              : 
    4178          749 :       if (need_device_ptr || need_device_addr)
    4179              :         {
    4180          740 :           bool is_device_ptr = false;
    4181          740 :           bool has_device_addr = false;
    4182              : 
    4183          740 :           for (tree c = dispatch_clauses; c; c = TREE_CHAIN (c))
    4184              :             {
    4185          454 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR
    4186          454 :                   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
    4187              :                 {
    4188          164 :                   tree decl1 = DECL_NAME (OMP_CLAUSE_DECL (c));
    4189          164 :                   tree decl2 = tree_strip_nop_conversions (*arg_p);
    4190          164 :                   if (TREE_CODE (decl2) == ADDR_EXPR)
    4191           19 :                     decl2 = TREE_OPERAND (decl2, 0);
    4192          164 :                   if (VAR_P (decl2) || TREE_CODE (decl2) == PARM_DECL)
    4193              :                     {
    4194          140 :                       decl2 = DECL_NAME (decl2);
    4195          140 :                       if (decl1 == decl2
    4196          203 :                           && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
    4197              :                         {
    4198           58 :                           if (need_device_addr)
    4199            0 :                             warning_at (OMP_CLAUSE_LOCATION (c),
    4200            0 :                                         OPT_Wopenmp,
    4201              :                                         "%<is_device_ptr%> for %qD does"
    4202              :                                         " not imply %<has_device_addr%> "
    4203              :                                         "required for %<need_device_addr%>",
    4204            0 :                                         OMP_CLAUSE_DECL (c));
    4205              :                           is_device_ptr = true;
    4206              :                           break;
    4207              :                         }
    4208           82 :                       else if (decl1 == decl2)
    4209              :                         {
    4210            5 :                           if (need_device_ptr)
    4211           10 :                             warning_at (OMP_CLAUSE_LOCATION (c),
    4212            5 :                                         OPT_Wopenmp,
    4213              :                                         "%<has_device_addr%> for %qD does"
    4214              :                                         " not imply %<is_device_ptr%> "
    4215              :                                         "required for %<need_device_ptr%>",
    4216            5 :                                         OMP_CLAUSE_DECL (c));
    4217              :                           has_device_addr = true;
    4218              :                           break;
    4219              :                         }
    4220              :                     }
    4221              :                 }
    4222              :             }
    4223              : 
    4224          349 :           if ((need_device_ptr && !is_device_ptr)
    4225           58 :               || (need_device_addr && !has_device_addr))
    4226              :             {
    4227          291 :               if (dispatch_device_num == NULL_TREE)
    4228              :                 {
    4229              :                   // device_num = omp_get_default_device ()
    4230          105 :                   tree fn
    4231          105 :                     = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
    4232          105 :                   tree call = build_call_expr (fn, 0);
    4233          105 :                   dispatch_device_num = create_tmp_var_raw (TREE_TYPE (call));
    4234          105 :                   tree init
    4235          105 :                     = build4 (TARGET_EXPR, TREE_TYPE (call),
    4236              :                               dispatch_device_num, call, NULL_TREE, NULL_TREE);
    4237          105 :                   if (init_code)
    4238            0 :                     init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
    4239              :                                         init_code, init);
    4240              :                   else
    4241              :                     init_code = init;
    4242              :                 }
    4243              : 
    4244              :               // We want to emit the following statement:
    4245              :               //   mapped_arg = omp_get_mapped_ptr (arg,
    4246              :               //                device_num)
    4247              :               // but arg has to be the actual pointer, not a
    4248              :               // reference or a conversion expression.
    4249          291 :               tree actual_ptr
    4250          291 :                 = ((TREE_CODE (*arg_p) == ADDR_EXPR)
    4251          291 :                    ? TREE_OPERAND (*arg_p, 0)
    4252           38 :                    : *arg_p);
    4253          291 :               if (TREE_CODE (actual_ptr) == NOP_EXPR
    4254          291 :                   && (TREE_CODE (TREE_TYPE (TREE_OPERAND (actual_ptr, 0)))
    4255              :                       == REFERENCE_TYPE))
    4256              :                 {
    4257            6 :                   actual_ptr = TREE_OPERAND (actual_ptr, 0);
    4258            6 :                   actual_ptr = build1 (INDIRECT_REF,
    4259            6 :                                        TREE_TYPE (actual_ptr),
    4260              :                                        actual_ptr);
    4261              :                 }
    4262          291 :               tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_MAPPED_PTR);
    4263          291 :               tree mapped_arg = build_call_expr_loc (loc, fn, 2, actual_ptr,
    4264              :                                                      dispatch_device_num);
    4265              : 
    4266          291 :               if (TREE_CODE (*arg_p) == ADDR_EXPR
    4267          291 :                   || (TREE_CODE (TREE_TYPE (actual_ptr)) == REFERENCE_TYPE))
    4268           47 :                 mapped_arg = build_fold_addr_expr (mapped_arg);
    4269          244 :               else if (TREE_CODE (*arg_p) == NOP_EXPR)
    4270           35 :                 mapped_arg = build1 (NOP_EXPR, TREE_TYPE (*arg_p),
    4271              :                                      mapped_arg);
    4272          291 :               *arg_p = mapped_arg;
    4273              :             }
    4274              :         }
    4275              :     }
    4276              : 
    4277          471 :  add_cleanup:
    4278          471 :   if (cleanup)
    4279              :     {
    4280           56 :       tree result = NULL_TREE;
    4281           56 :       if (want_value && pointerize)
    4282              :         {
    4283            0 :           tree tmp = create_tmp_var (build_pointer_type (TREE_TYPE (expr)),
    4284              :                                      "cleanuptmp");
    4285            0 :           result = build_simple_mem_ref (tmp);
    4286            0 :           expr = build2 (INIT_EXPR, TREE_TYPE (tmp), tmp,
    4287              :                          build_fold_addr_expr (expr));
    4288              :         }
    4289           56 :       else if (want_value)
    4290              :         {
    4291           18 :           tree tmp = create_tmp_var (TREE_TYPE (expr), "cleanuptmp");
    4292           18 :           result = tmp;
    4293           18 :           expr = build2 (INIT_EXPR, TREE_TYPE (tmp), tmp, expr);
    4294              :         }
    4295           56 :       if (init_code)
    4296           56 :         expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init_code, expr);
    4297           56 :       expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, cleanup);
    4298              : 
    4299           56 :       if (result)
    4300           18 :         expr = build2 (COMPOUND_EXPR, TREE_TYPE (result), expr, result);
    4301              :     }
    4302          415 :   else if (init_code)
    4303          105 :     expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init_code, expr);
    4304              : 
    4305              :   return expr;
    4306              : }
    4307              : 
    4308              : /* Helper function for gimplify_call_expr: handle "declare variant"
    4309              :    resolution and expansion of the CALL_EXPR EXPR.  WANT_VALUE is true
    4310              :    if the result value of the call is needed; POINTERIZE is true if it
    4311              :    also needs to be pointerized.  If OMP_DISPATCH_P is true, apply
    4312              :    associated transformations using DISPATCH_CLAUSES.
    4313              :    This function may return either the original call or some other
    4314              :    expression such as a conditional to select one of multiple calls.
    4315              : 
    4316              :    FIXME: this function is written to be independent of gimplifier internals
    4317              :    so that it could be moved to omp-general.cc and invoked from the
    4318              :    front ends instead, per PR115076.  */
    4319              : 
    4320              : static tree
    4321         1355 : expand_variant_call_expr (tree expr, bool want_value, bool pointerize,
    4322              :                           bool omp_dispatch_p, tree dispatch_clauses)
    4323              : {
    4324              :   /* If we've already processed this call, stop now.  This can happen
    4325              :      if the variant call resolves to the original function, or to
    4326              :      a dynamic conditional that includes the default call to the original
    4327              :      function.  */
    4328         1355 :   gcc_assert (omp_resolved_variant_calls != NULL);
    4329         1355 :   if (omp_resolved_variant_calls->contains (expr))
    4330          131 :     return expr;
    4331              : 
    4332         1224 :   tree fndecl = get_callee_fndecl (expr);
    4333         1224 :   tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (expr));
    4334         1224 :   location_t loc = EXPR_LOCATION (expr);
    4335         1224 :   tree construct_context = omp_get_construct_context ();
    4336         1224 :   vec<struct omp_variant> all_candidates
    4337         1224 :     = omp_declare_variant_candidates (fndecl, construct_context);
    4338         1224 :   gcc_assert (!all_candidates.is_empty ());
    4339         1224 :   vec<struct omp_variant> candidates
    4340         1224 :     = omp_get_dynamic_candidates (all_candidates, construct_context);
    4341              : 
    4342              :   /* If the variant call could be resolved now, build a nest of COND_EXPRs
    4343              :      if there are dynamic candidates, and/or a new CALL_EXPR for each
    4344              :      candidate call.  */
    4345         1224 :   if (!candidates.is_empty ())
    4346              :     {
    4347         1212 :       int n = candidates.length ();
    4348         1212 :       tree tail = NULL_TREE;
    4349              : 
    4350         2462 :       for (int i = n - 1; i >= 0; i--)
    4351              :         {
    4352         1250 :           if (tail)
    4353           38 :             gcc_assert (candidates[i].dynamic_selector);
    4354              :           else
    4355         1212 :             gcc_assert (!candidates[i].dynamic_selector);
    4356         1250 :           if (candidates[i].alternative == fndecl)
    4357              :             {
    4358              :               /* We should only get the original function back as the
    4359              :                  default.  */
    4360          284 :               gcc_assert (!tail);
    4361          284 :               omp_resolved_variant_calls->add (expr);
    4362          284 :               tail = expr;
    4363              :             }
    4364              :           else
    4365              :             {
    4366              :               /* For the final static selector, we can re-use the old
    4367              :                  CALL_EXPR and just replace the function, unless it may
    4368              :                  need dispatch argument modification.  Otherwise,
    4369              :                  make a copy of it.  */
    4370          966 :               tree thiscall = (tail || omp_dispatch_p
    4371          966 :                                ? unshare_expr (expr) : expr);
    4372          966 :               CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype,
    4373          966 :                                                 candidates[i].alternative);
    4374          966 :               if (omp_dispatch_p)
    4375          471 :                 thiscall = modify_call_for_omp_dispatch (thiscall,
    4376              :                                                          dispatch_clauses,
    4377              :                                                          want_value,
    4378              :                                                          pointerize);
    4379          966 :               if (!tail)
    4380              :                 tail = thiscall;
    4381              :               else
    4382           76 :                 tail = build3 (COND_EXPR, TREE_TYPE (expr),
    4383           38 :                                omp_dynamic_cond (candidates[i].selector,
    4384              :                                                  find_supercontext ()),
    4385              :                                thiscall, tail);
    4386              :             }
    4387              :         }
    4388              :       return tail;
    4389              :     }
    4390              : 
    4391              :   /* If we couldn't resolve the variant call now, expand it into a loop using
    4392              :      a switch and OMP_NEXT_VARIANT for dispatch.  The ompdevlow pass will
    4393              :      handle OMP_NEXT_VARIANT expansion.  */
    4394              :   else
    4395              :     {
    4396              :       /* If we need a usable return value, we need a temporary
    4397              :          and an assignment in each alternative.  This logic was borrowed
    4398              :          from gimplify_cond_expr.  */
    4399           12 :       tree type = TREE_TYPE (expr);
    4400           12 :       tree tmp = NULL_TREE, result = NULL_TREE;
    4401              : 
    4402           12 :       if (want_value)
    4403              :         {
    4404           12 :           if (pointerize)
    4405              :             {
    4406            0 :               type = build_pointer_type (type);
    4407            0 :               tmp = create_tmp_var (type, "iftmp");
    4408            0 :               result = build_simple_mem_ref_loc (loc, tmp);
    4409              :             }
    4410              :           else
    4411              :             {
    4412           12 :               tmp = create_tmp_var (type, "iftmp");
    4413           12 :               result = tmp;
    4414              :             }
    4415              :         }
    4416              : 
    4417              :       /* Preprocess the all_candidates array so that the alternative field of
    4418              :          each element holds the actual function call expression and possible
    4419              :          assignment, instead of just the decl for the variant function.  */
    4420           60 :       for (unsigned int i = 0; i < all_candidates.length (); i++)
    4421              :         {
    4422           48 :           tree decl = all_candidates[i].alternative;
    4423           48 :           tree thiscall;
    4424              : 
    4425              :           /* We need to turn the decl from the candidate into a function
    4426              :              call and possible assignment, and stuff that in
    4427              :              the directive seq of the gomp_variant.  */
    4428           48 :           if (decl == fndecl)
    4429              :             {
    4430           12 :               thiscall = expr;
    4431           12 :               omp_resolved_variant_calls->add (expr);
    4432              :             }
    4433              :           else
    4434              :             {
    4435           36 :               thiscall = unshare_expr (expr);
    4436           36 :               CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype, decl);
    4437           36 :               if (omp_dispatch_p)
    4438            0 :                 thiscall = modify_call_for_omp_dispatch (thiscall,
    4439              :                                                          dispatch_clauses,
    4440              :                                                          want_value,
    4441              :                                                          pointerize);
    4442              :             }
    4443           48 :           if (pointerize)
    4444            0 :             thiscall = build_fold_addr_expr_loc (loc, thiscall);
    4445           48 :           if (want_value)
    4446           48 :             thiscall = build2 (INIT_EXPR, type, tmp, thiscall);
    4447           48 :           all_candidates[i].alternative = thiscall;
    4448              :         }
    4449              : 
    4450           12 :       cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
    4451           12 :       tree expansion = expand_late_variant_directive (all_candidates,
    4452              :                                                       construct_context);
    4453           12 :       if (result)
    4454           12 :         expansion = build2 (COMPOUND_EXPR, TREE_TYPE (result),
    4455              :                             expansion, result);
    4456              :       return expansion;
    4457              :     }
    4458              : }
    4459              : 
    4460              : /* Wrapper around expand_variant_call_expr to interface with gimplifier
    4461              :    state.  EXPR and OMP_DISPATCH_P are as for expand_variant_call_expr,
    4462              :    FALLBACK is used to compute the WANT_VALUE and POINTERIZE arguments.  */
    4463              : static tree
    4464         1355 : gimplify_variant_call_expr (tree expr, fallback_t fallback,
    4465              :                             bool omp_dispatch_p)
    4466              : {
    4467         1355 :   tree type = TREE_TYPE (expr);
    4468         1355 :   bool want_value = (fallback != fb_none && !VOID_TYPE_P (type));
    4469          673 :   bool pointerize = false;
    4470              :   /* If the result value must be an lvalue or the result type must
    4471              :      live in memory, then we have to pointerize it if we need a temporary.  */
    4472          673 :   if (want_value
    4473          673 :       && ((!(fallback & fb_rvalue) && (fallback & fb_lvalue))
    4474          673 :           || TREE_ADDRESSABLE (type)))
    4475            0 :     pointerize = true;
    4476              : 
    4477         1867 :   return expand_variant_call_expr (expr, want_value, pointerize,
    4478              :                                    omp_dispatch_p,
    4479              :                                    (omp_dispatch_p
    4480          512 :                                     ? gimplify_omp_ctxp->clauses
    4481         1355 :                                     : NULL_TREE));
    4482              : }
    4483              : 
    4484              : 
    4485              : /* Helper function for gimplify_call_expr, called via walk_tree.
    4486              :    Find used user labels.  */
    4487              : 
    4488              : static tree
    4489          766 : find_used_user_labels (tree *tp, int *, void *)
    4490              : {
    4491          766 :   if (TREE_CODE (*tp) == LABEL_EXPR
    4492           15 :       && !DECL_ARTIFICIAL (LABEL_EXPR_LABEL (*tp))
    4493           15 :       && DECL_NAME (LABEL_EXPR_LABEL (*tp))
    4494          781 :       && TREE_USED (LABEL_EXPR_LABEL (*tp)))
    4495           15 :     return *tp;
    4496              :   return NULL_TREE;
    4497              : }
    4498              : 
    4499              : 
    4500              : /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
    4501              :    WANT_VALUE is true if the result of the call is desired.  */
    4502              : 
    4503              : static enum gimplify_status
    4504     17446566 : gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
    4505              : {
    4506     17446566 :   bool want_value = (fallback != fb_none);
    4507     17446566 :   tree fndecl, parms, p, fnptrtype;
    4508     17446566 :   enum gimplify_status ret;
    4509     17446566 :   int i, nargs;
    4510     17446566 :   gcall *call;
    4511     17446566 :   bool builtin_va_start_p = false, omp_dispatch_p = false;
    4512     17446566 :   location_t loc = EXPR_LOCATION (*expr_p);
    4513              : 
    4514     17446566 :   gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
    4515              : 
    4516              :   /* For reliable diagnostics during inlining, it is necessary that
    4517              :      every call_expr be annotated with file and line.  */
    4518     17446566 :   if (! EXPR_HAS_LOCATION (*expr_p))
    4519       526007 :     SET_EXPR_LOCATION (*expr_p, input_location);
    4520              : 
    4521              :   /* Gimplify internal functions created in the FEs.  */
    4522     17446566 :   if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
    4523              :     {
    4524       610554 :       enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
    4525       610554 :       if (ifn == IFN_GOMP_DISPATCH)
    4526              :         {
    4527          884 :           gcc_assert (flag_openmp
    4528              :                       && gimplify_omp_ctxp
    4529              :                       && gimplify_omp_ctxp->code == OMP_DISPATCH);
    4530          884 :           *expr_p = CALL_EXPR_ARG (*expr_p, 0);
    4531          884 :           gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
    4532          884 :           if (! EXPR_HAS_LOCATION (*expr_p))
    4533           94 :             SET_EXPR_LOCATION (*expr_p, input_location);
    4534              :           omp_dispatch_p = true;
    4535              :         }
    4536              :       else
    4537              :         {
    4538       609670 :           if (want_value)
    4539              :             return GS_ALL_DONE;
    4540              : 
    4541        21251 :           nargs = call_expr_nargs (*expr_p);
    4542        21251 :           auto_vec<tree> vargs (nargs);
    4543              : 
    4544        21251 :           if (ifn == IFN_ASSUME)
    4545              :             {
    4546          610 :               if (simple_condition_p (CALL_EXPR_ARG (*expr_p, 0)))
    4547              :                 {
    4548              :                   /* If the [[assume (cond)]]; condition is simple
    4549              :                      enough and can be evaluated unconditionally
    4550              :                      without side-effects, expand it as
    4551              :                      if (!cond) __builtin_unreachable ();  */
    4552          312 :                   tree fndecl = builtin_decl_explicit (BUILT_IN_UNREACHABLE);
    4553          312 :                   *expr_p
    4554          624 :                     = build3 (COND_EXPR, void_type_node,
    4555          312 :                               CALL_EXPR_ARG (*expr_p, 0), void_node,
    4556          312 :                               build_call_expr_loc (EXPR_LOCATION (*expr_p),
    4557              :                                                    fndecl, 0));
    4558          312 :                   return GS_OK;
    4559              :                 }
    4560              :               /* If not optimizing, ignore the assumptions unless there
    4561              :                  are used user labels in it.  */
    4562          298 :               if ((!optimize
    4563           98 :                    && !walk_tree_without_duplicates (&CALL_EXPR_ARG (*expr_p,
    4564              :                                                                      0),
    4565              :                                                      find_used_user_labels,
    4566              :                                                      NULL))
    4567          313 :                   || seen_error ())
    4568              :                 {
    4569           94 :                   *expr_p = NULL_TREE;
    4570           94 :                   return GS_ALL_DONE;
    4571              :                 }
    4572              :               /* Temporarily, until gimple lowering, transform
    4573              :                  .ASSUME (cond);
    4574              :                  into:
    4575              :                  [[assume (guard)]]
    4576              :                  {
    4577              :                    guard = cond;
    4578              :                  }
    4579              :                  such that gimple lowering can outline the condition into
    4580              :                  a separate function easily.  */
    4581          204 :               tree guard = create_tmp_var (boolean_type_node);
    4582          204 :               *expr_p = build2 (MODIFY_EXPR, void_type_node, guard,
    4583          204 :                                 gimple_boolify (CALL_EXPR_ARG (*expr_p, 0)));
    4584          204 :               *expr_p = build3 (BIND_EXPR, void_type_node, NULL, *expr_p, NULL);
    4585          204 :               push_gimplify_context ();
    4586          204 :               gimple_seq body = NULL;
    4587          204 :               gimple *g = gimplify_and_return_first (*expr_p, &body);
    4588          204 :               pop_gimplify_context (g);
    4589          204 :               g = gimple_build_assume (guard, body);
    4590          204 :               gimple_set_location (g, loc);
    4591          204 :               gimplify_seq_add_stmt (pre_p, g);
    4592          204 :               *expr_p = NULL_TREE;
    4593          204 :               return GS_ALL_DONE;
    4594              :             }
    4595        20641 :           else if (ifn == IFN_UBSAN_BOUNDS
    4596        20641 :                    && nargs == 3
    4597        20641 :                    && integer_onep (CALL_EXPR_ARG (*expr_p, 0)))
    4598              :             {
    4599              :               /* If first argument is one, add TYPE_MAX_VALUE (TYPE_DOMAIN (t))
    4600              :                  to 3rd argument and change first argument to 0.  This is
    4601              :                  done by ubsan_instrument_bounds so that we can use the
    4602              :                  max value from gimplify_type_sizes here instead of original
    4603              :                  expression for VLAs.  */
    4604          406 :               tree type = TREE_TYPE (CALL_EXPR_ARG (*expr_p, 0));
    4605          406 :               CALL_EXPR_ARG (*expr_p, 0) = build_int_cst (type, 0);
    4606          406 :               gcc_assert (TREE_CODE (type) == POINTER_TYPE);
    4607          406 :               type = TREE_TYPE (type);
    4608          406 :               gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
    4609          406 :               tree maxv = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    4610          406 :               gcc_assert (maxv);
    4611          406 :               tree arg3 = CALL_EXPR_ARG (*expr_p, 2);
    4612          406 :               CALL_EXPR_ARG (*expr_p, 2)
    4613          812 :                 = fold_build2 (PLUS_EXPR, TREE_TYPE (arg3), maxv, arg3);
    4614              :             }
    4615              : 
    4616        46432 :           for (i = 0; i < nargs; i++)
    4617              :             {
    4618        25791 :               gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
    4619        25791 :                             EXPR_LOCATION (*expr_p));
    4620        25791 :               vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
    4621              :             }
    4622              : 
    4623        20641 :           gcall *call = gimple_build_call_internal_vec (ifn, vargs);
    4624        20641 :           gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
    4625        20641 :           gimplify_seq_add_stmt (pre_p, call);
    4626        20641 :           return GS_ALL_DONE;
    4627        21251 :         }
    4628              :     }
    4629              : 
    4630              :   /* This may be a call to a builtin function.
    4631              : 
    4632              :      Builtin function calls may be transformed into different
    4633              :      (and more efficient) builtin function calls under certain
    4634              :      circumstances.  Unfortunately, gimplification can muck things
    4635              :      up enough that the builtin expanders are not aware that certain
    4636              :      transformations are still valid.
    4637              : 
    4638              :      So we attempt transformation/gimplification of the call before
    4639              :      we gimplify the CALL_EXPR.  At this time we do not manage to
    4640              :      transform all calls in the same manner as the expanders do, but
    4641              :      we do transform most of them.  */
    4642     16836896 :   fndecl = get_callee_fndecl (*expr_p);
    4643     16836896 :   if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
    4644      4022144 :     switch (DECL_FUNCTION_CODE (fndecl))
    4645              :       {
    4646        95879 :       CASE_BUILT_IN_ALLOCA:
    4647              :         /* If the call has been built for a variable-sized object, then we
    4648              :            want to restore the stack level when the enclosing BIND_EXPR is
    4649              :            exited to reclaim the allocated space; otherwise, we precisely
    4650              :            need to do the opposite and preserve the latest stack level.  */
    4651        95879 :         if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
    4652         9051 :           gimplify_ctxp->save_stack = true;
    4653              :         else
    4654        86828 :           gimplify_ctxp->keep_stack = true;
    4655              :         break;
    4656              : 
    4657        20938 :       case BUILT_IN_VA_START:
    4658        20938 :         {
    4659        20938 :           builtin_va_start_p = true;
    4660        20938 :           if (call_expr_nargs (*expr_p) < 2)
    4661              :             {
    4662            1 :               error ("too few arguments to function %<va_start%>");
    4663            1 :               *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
    4664            1 :               return GS_OK;
    4665              :             }
    4666              : 
    4667        20937 :           if (fold_builtin_next_arg (*expr_p, true))
    4668              :             {
    4669            8 :               *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
    4670            8 :               return GS_OK;
    4671              :             }
    4672              :           break;
    4673              :         }
    4674              : 
    4675           32 :       case BUILT_IN_EH_RETURN:
    4676           32 :         cfun->calls_eh_return = true;
    4677           32 :         break;
    4678              : 
    4679         1048 :       case BUILT_IN_CLEAR_PADDING:
    4680         1048 :         if (call_expr_nargs (*expr_p) == 1)
    4681              :           {
    4682              :             /* Remember the original type of the argument in an internal
    4683              :                dummy second argument, as in GIMPLE pointer conversions are
    4684              :                useless.  Also mark this call as not for automatic
    4685              :                initialization in the internal dummy third argument.  */
    4686          524 :             p = CALL_EXPR_ARG (*expr_p, 0);
    4687          524 :             *expr_p
    4688          524 :               = build_call_expr_loc (EXPR_LOCATION (*expr_p), fndecl, 2, p,
    4689          524 :                                      build_zero_cst (TREE_TYPE (p)));
    4690          524 :             return GS_OK;
    4691              :           }
    4692              :         break;
    4693              : 
    4694              :       default:
    4695              :         ;
    4696              :       }
    4697     16836363 :   if (fndecl && fndecl_built_in_p (fndecl))
    4698              :     {
    4699      4263271 :       tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
    4700      4263271 :       if (new_tree && new_tree != *expr_p)
    4701              :         {
    4702              :           /* There was a transformation of this call which computes the
    4703              :              same value, but in a more efficient way.  Return and try
    4704              :              again.  */
    4705          773 :           *expr_p = new_tree;
    4706          773 :           return GS_OK;
    4707              :         }
    4708              :     }
    4709              : 
    4710              :   /* Remember the original function pointer type.  */
    4711     16835590 :   fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
    4712              : 
    4713              :   /* Handle "declare variant" resolution and arglist processing.  */
    4714     16835590 :   if (flag_openmp
    4715       411321 :       && fndecl
    4716       408610 :       && cfun
    4717       408610 :       && (cfun->curr_properties & PROP_gimple_any) == 0
    4718       262590 :       && !omp_has_novariants ()
    4719     17098068 :       && lookup_attribute ("omp declare variant base",
    4720       262478 :                            DECL_ATTRIBUTES (fndecl)))
    4721              :     {
    4722         1355 :       tree orig = *expr_p;
    4723         1355 :       *expr_p = gimplify_variant_call_expr (*expr_p, fallback,
    4724              :                                             omp_dispatch_p);
    4725              : 
    4726              :       /* This may resolve to the same call, or the call expr with just
    4727              :          the function replaced, in which case we should just continue to
    4728              :          gimplify it normally.  Otherwise, if we get something else back,
    4729              :          stop here and re-gimplify the whole replacement expr.  */
    4730         1355 :       if (*expr_p != orig)
    4731              :         return GS_OK;
    4732              :     }
    4733              : 
    4734              :   /* There is a sequence point before the call, so any side effects in
    4735              :      the calling expression must occur before the actual call.  Force
    4736              :      gimplify_expr to use an internal post queue.  */
    4737     16835101 :   ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
    4738              :                        is_gimple_call_addr, fb_rvalue);
    4739              : 
    4740     16835101 :   if (ret == GS_ERROR)
    4741              :     return GS_ERROR;
    4742              : 
    4743     16835099 :   nargs = call_expr_nargs (*expr_p);
    4744              : 
    4745              :   /* Get argument types for verification.  */
    4746     16835099 :   fndecl = get_callee_fndecl (*expr_p);
    4747     16835099 :   parms = NULL_TREE;
    4748     16835099 :   if (fndecl)
    4749     16606951 :     parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
    4750              :   else
    4751       228148 :     parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
    4752              : 
    4753     33442050 :   if (fndecl && DECL_ARGUMENTS (fndecl))
    4754              :     p = DECL_ARGUMENTS (fndecl);
    4755      7455987 :   else if (parms)
    4756              :     p = parms;
    4757              :   else
    4758              :     p = NULL_TREE;
    4759     44858319 :   for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
    4760              :     ;
    4761              : 
    4762              :   /* If the last argument is __builtin_va_arg_pack () and it is not
    4763              :      passed as a named argument, decrease the number of CALL_EXPR
    4764              :      arguments and set instead the CALL_EXPR_VA_ARG_PACK flag.  */
    4765     16835099 :   if (!p
    4766     16835099 :       && i < nargs
    4767     16835099 :       && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
    4768              :     {
    4769         5977 :       tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
    4770         5977 :       tree last_arg_fndecl = get_callee_fndecl (last_arg);
    4771              : 
    4772         5977 :       if (last_arg_fndecl
    4773         5977 :           && fndecl_built_in_p (last_arg_fndecl, BUILT_IN_VA_ARG_PACK))
    4774              :         {
    4775          132 :           tree call = *expr_p;
    4776              : 
    4777          132 :           --nargs;
    4778          264 :           *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
    4779          132 :                                           CALL_EXPR_FN (call),
    4780          132 :                                           nargs, CALL_EXPR_ARGP (call));
    4781              : 
    4782              :           /* Copy all CALL_EXPR flags, location and block, except
    4783              :              CALL_EXPR_VA_ARG_PACK flag.  */
    4784          132 :           CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
    4785          132 :           CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
    4786          132 :           CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
    4787          132 :             = CALL_EXPR_RETURN_SLOT_OPT (call);
    4788          132 :           CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
    4789          132 :           SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
    4790              : 
    4791              :           /* Set CALL_EXPR_VA_ARG_PACK.  */
    4792          132 :           CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
    4793              :         }
    4794              :     }
    4795              : 
    4796              :   /* If the call returns twice then after building the CFG the call
    4797              :      argument computations will no longer dominate the call because
    4798              :      we add an abnormal incoming edge to the call.  So do not use SSA
    4799              :      vars there.  */
    4800     16835099 :   bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
    4801              : 
    4802              : 
    4803              :   /* Gimplify the function arguments.  */
    4804     16835099 :   if (nargs > 0)
    4805              :     {
    4806     14717646 :       for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
    4807     44236249 :            PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
    4808     29518603 :            PUSH_ARGS_REVERSED ? i-- : i++)
    4809              :         {
    4810     29518603 :           enum gimplify_status t;
    4811              : 
    4812              :           /* Avoid gimplifying the second argument to va_start, which needs to
    4813              :              be the plain PARM_DECL.  */
    4814     29518603 :           if ((i != 1) || !builtin_va_start_p)
    4815              :             {
    4816     29497674 :               tree *arg_p = &CALL_EXPR_ARG (*expr_p, i);
    4817              : 
    4818     29497674 :               if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
    4819         6687 :                 gimplify_omp_ctxp->in_call_args = true;
    4820     29497674 :               t = gimplify_arg (arg_p, pre_p, EXPR_LOCATION (*expr_p),
    4821              :                                 !returns_twice);
    4822     29497674 :               if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
    4823         6687 :                 gimplify_omp_ctxp->in_call_args = false;
    4824              : 
    4825     29497674 :               if (t == GS_ERROR)
    4826     29518603 :                 ret = GS_ERROR;
    4827              :             }
    4828              :         }
    4829              :     }
    4830              : 
    4831              :   /* Gimplify the static chain.  */
    4832     16835099 :   if (CALL_EXPR_STATIC_CHAIN (*expr_p))
    4833              :     {
    4834        28853 :       if (fndecl && !DECL_STATIC_CHAIN (fndecl))
    4835            3 :         CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
    4836              :       else
    4837              :         {
    4838        28174 :           enum gimplify_status t;
    4839        28174 :           t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
    4840        28174 :                             EXPR_LOCATION (*expr_p), ! returns_twice);
    4841        28174 :           if (t == GS_ERROR)
    4842     16835099 :             ret = GS_ERROR;
    4843              :         }
    4844              :     }
    4845              : 
    4846              :   /* Verify the function result.  */
    4847     16835099 :   if (want_value && fndecl
    4848     16835099 :       && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
    4849              :     {
    4850            0 :       error_at (loc, "using result of function returning %<void%>");
    4851            0 :       ret = GS_ERROR;
    4852              :     }
    4853              : 
    4854              :   /* Try this again in case gimplification exposed something.  */
    4855     16835099 :   if (ret != GS_ERROR)
    4856              :     {
    4857     16834949 :       tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
    4858              : 
    4859     16834949 :       if (new_tree && new_tree != *expr_p)
    4860              :         {
    4861              :           /* There was a transformation of this call which computes the
    4862              :              same value, but in a more efficient way.  Return and try
    4863              :              again.  */
    4864        11459 :           *expr_p = new_tree;
    4865        11459 :           return GS_OK;
    4866              :         }
    4867              :     }
    4868              :   else
    4869              :     {
    4870          150 :       *expr_p = error_mark_node;
    4871          150 :       return GS_ERROR;
    4872              :     }
    4873              : 
    4874              :   /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
    4875              :      decl.  This allows us to eliminate redundant or useless
    4876              :      calls to "const" functions.  */
    4877     16823490 :   if (TREE_CODE (*expr_p) == CALL_EXPR)
    4878              :     {
    4879     16823490 :       int flags = call_expr_flags (*expr_p);
    4880     16823490 :       if (flags & (ECF_CONST | ECF_PURE)
    4881              :           /* An infinite loop is considered a side effect.  */
    4882      2458794 :           && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
    4883      2059326 :         TREE_SIDE_EFFECTS (*expr_p) = 0;
    4884              :     }
    4885              : 
    4886              :   /* If the value is not needed by the caller, emit a new GIMPLE_CALL
    4887              :      and clear *EXPR_P.  Otherwise, leave *EXPR_P in its gimplified
    4888              :      form and delegate the creation of a GIMPLE_CALL to
    4889              :      gimplify_modify_expr.  This is always possible because when
    4890              :      WANT_VALUE is true, the caller wants the result of this call into
    4891              :      a temporary, which means that we will emit an INIT_EXPR in
    4892              :      internal_get_tmp_var which will then be handled by
    4893              :      gimplify_modify_expr.  */
    4894     16823490 :   if (!want_value)
    4895              :     {
    4896              :       /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
    4897              :          have to do is replicate it as a GIMPLE_CALL tuple.  */
    4898      6418270 :       gimple_stmt_iterator gsi;
    4899      6418270 :       call = gimple_build_call_from_tree (*expr_p, fnptrtype);
    4900      6418270 :       notice_special_calls (call);
    4901      6418270 :       gimplify_seq_add_stmt (pre_p, call);
    4902      6418270 :       gsi = gsi_last (*pre_p);
    4903      6418270 :       maybe_fold_stmt (&gsi);
    4904      6418270 :       *expr_p = NULL_TREE;
    4905              :     }
    4906              :   else
    4907              :     /* Remember the original function type.  */
    4908     10405220 :     CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
    4909     10405220 :                                      CALL_EXPR_FN (*expr_p));
    4910              : 
    4911              :   return ret;
    4912              : }
    4913              : 
    4914              : /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
    4915              :    rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
    4916              : 
    4917              :    TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
    4918              :    condition is true or false, respectively.  If null, we should generate
    4919              :    our own to skip over the evaluation of this specific expression.
    4920              : 
    4921              :    LOCUS is the source location of the COND_EXPR.
    4922              : 
    4923              :    The condition_uid is a discriminator tag for condition coverage used to map
    4924              :    conditions to its corresponding full Boolean function.
    4925              : 
    4926              :    This function is the tree equivalent of do_jump.
    4927              : 
    4928              :    shortcut_cond_r should only be called by shortcut_cond_expr.  */
    4929              : 
    4930              : static tree
    4931      1337186 : shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
    4932              :                  location_t locus, unsigned condition_uid)
    4933              : {
    4934      1337186 :   tree local_label = NULL_TREE;
    4935      1337186 :   tree t, expr = NULL;
    4936              : 
    4937              :   /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
    4938              :      retain the shortcut semantics.  Just insert the gotos here;
    4939              :      shortcut_cond_expr will append the real blocks later.  */
    4940      1337186 :   if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
    4941              :     {
    4942       124557 :       location_t new_locus;
    4943              : 
    4944              :       /* Turn if (a && b) into
    4945              : 
    4946              :          if (a); else goto no;
    4947              :          if (b) goto yes; else goto no;
    4948              :          (no:) */
    4949              : 
    4950       124557 :       if (false_label_p == NULL)
    4951         4023 :         false_label_p = &local_label;
    4952              : 
    4953              :       /* Keep the original source location on the first 'if'.  */
    4954       124557 :       t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus,
    4955              :                            condition_uid);
    4956       124557 :       append_to_statement_list (t, &expr);
    4957              : 
    4958              :       /* Set the source location of the && on the second 'if'.  */
    4959       124557 :       new_locus = rexpr_location (pred, locus);
    4960       124557 :       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
    4961              :                            new_locus, condition_uid);
    4962       124557 :       append_to_statement_list (t, &expr);
    4963              :     }
    4964      1212629 :   else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
    4965              :     {
    4966       387608 :       location_t new_locus;
    4967              : 
    4968              :       /* Turn if (a || b) into
    4969              : 
    4970              :          if (a) goto yes;
    4971              :          if (b) goto yes; else goto no;
    4972              :          (yes:) */
    4973              : 
    4974       387608 :       if (true_label_p == NULL)
    4975       232451 :         true_label_p = &local_label;
    4976              : 
    4977              :       /* Keep the original source location on the first 'if'.  */
    4978       387608 :       t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus,
    4979              :                            condition_uid);
    4980       387608 :       append_to_statement_list (t, &expr);
    4981              : 
    4982              :       /* Set the source location of the || on the second 'if'.  */
    4983       387608 :       new_locus = rexpr_location (pred, locus);
    4984       387608 :       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
    4985              :                            new_locus, condition_uid);
    4986       387608 :       append_to_statement_list (t, &expr);
    4987              :     }
    4988       825021 :   else if (TREE_CODE (pred) == COND_EXPR
    4989          852 :            && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
    4990       825873 :            && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
    4991              :     {
    4992          846 :       location_t new_locus;
    4993              : 
    4994              :       /* As long as we're messing with gotos, turn if (a ? b : c) into
    4995              :          if (a)
    4996              :            if (b) goto yes; else goto no;
    4997              :          else
    4998              :            if (c) goto yes; else goto no;
    4999              : 
    5000              :          Don't do this if one of the arms has void type, which can happen
    5001              :          in C++ when the arm is throw.  */
    5002              : 
    5003              :       /* Keep the original source location on the first 'if'.  Set the source
    5004              :          location of the ? on the second 'if'.  */
    5005          846 :       new_locus = rexpr_location (pred, locus);
    5006         1692 :       expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
    5007          846 :                      shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
    5008              :                                       false_label_p, locus, condition_uid),
    5009          846 :                      shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
    5010              :                                       false_label_p, new_locus,
    5011              :                                       condition_uid));
    5012          846 :       tree_associate_condition_with_expr (expr, condition_uid);
    5013              :     }
    5014              :   else
    5015              :     {
    5016       824175 :       expr = build3 (COND_EXPR, void_type_node, pred,
    5017              :                      build_and_jump (true_label_p),
    5018              :                      build_and_jump (false_label_p));
    5019       824175 :       SET_EXPR_LOCATION (expr, locus);
    5020       824175 :       tree_associate_condition_with_expr (expr, condition_uid);
    5021              :     }
    5022              : 
    5023      1337186 :   if (local_label)
    5024              :     {
    5025       236474 :       t = build1 (LABEL_EXPR, void_type_node, local_label);
    5026       236474 :       append_to_statement_list (t, &expr);
    5027              :     }
    5028              : 
    5029      1337186 :   return expr;
    5030              : }
    5031              : 
    5032              : /* If EXPR is a GOTO_EXPR, return it.  If it is a STATEMENT_LIST, skip
    5033              :    any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
    5034              :    statement, if it is the last one.  Otherwise, return NULL.  */
    5035              : 
    5036              : static tree
    5037     11773084 : find_goto (tree expr)
    5038              : {
    5039     12106417 :   if (!expr)
    5040              :     return NULL_TREE;
    5041              : 
    5042     10132730 :   if (TREE_CODE (expr) == GOTO_EXPR)
    5043              :     return expr;
    5044              : 
    5045      8072900 :   if (TREE_CODE (expr) != STATEMENT_LIST)
    5046              :     return NULL_TREE;
    5047              : 
    5048      1063991 :   tree_stmt_iterator i = tsi_start (expr);
    5049              : 
    5050      1420163 :   while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
    5051       356172 :     tsi_next (&i);
    5052              : 
    5053      1063991 :   if (!tsi_one_before_end_p (i))
    5054              :     return NULL_TREE;
    5055              : 
    5056       333333 :   return find_goto (tsi_stmt (i));
    5057              : }
    5058              : 
    5059              : /* Same as find_goto, except that it returns NULL if the destination
    5060              :    is not a LABEL_DECL.  */
    5061              : 
    5062              : static inline tree
    5063     11773084 : find_goto_label (tree expr)
    5064              : {
    5065     11773084 :   tree dest = find_goto (expr);
    5066     13832914 :   if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
    5067      2059719 :     return dest;
    5068              :   return NULL_TREE;
    5069              : }
    5070              : 
    5071              : 
    5072              : /* Given a multi-term condition (ANDIF, ORIF), walk the predicate PRED and tag
    5073              :    every basic condition with CONDITION_UID.  Two basic conditions share the
    5074              :    CONDITION_UID discriminator when they belong to the same predicate, which is
    5075              :    used by the condition coverage.  Doing this as an explicit step makes for a
    5076              :    simpler implementation than weaving it into the splitting code as the
    5077              :    splitting code eventually calls the entry point gimplfiy_expr which makes
    5078              :    bookkeeping complicated.  */
    5079              : static void
    5080       579090 : tag_shortcut_cond (tree pred, unsigned condition_uid)
    5081              : {
    5082       618695 :   if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR
    5083       618695 :       || TREE_CODE (pred) == TRUTH_ORIF_EXPR)
    5084              :     {
    5085       567707 :       tree fst = TREE_OPERAND (pred, 0);
    5086       567707 :       tree lst = TREE_OPERAND (pred, 1);
    5087              : 
    5088       567707 :       if (TREE_CODE (fst) == TRUTH_ANDIF_EXPR
    5089       567707 :           || TREE_CODE (fst) == TRUTH_ORIF_EXPR)
    5090       169428 :         tag_shortcut_cond (fst, condition_uid);
    5091       398279 :       else if (TREE_CODE (fst) == COND_EXPR)
    5092          244 :         tree_associate_condition_with_expr (fst, condition_uid);
    5093              : 
    5094       567707 :       if (TREE_CODE (lst) == TRUTH_ANDIF_EXPR
    5095       567707 :           || TREE_CODE (lst) == TRUTH_ORIF_EXPR)
    5096              :         tag_shortcut_cond (lst, condition_uid);
    5097       528102 :       else if (TREE_CODE (lst) == COND_EXPR)
    5098          445 :         tree_associate_condition_with_expr (lst, condition_uid);
    5099              :     }
    5100       579090 : }
    5101              : 
    5102              : /* Given a conditional expression EXPR with short-circuit boolean
    5103              :    predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
    5104              :    predicate apart into the equivalent sequence of conditionals.  CONDITION_UID
    5105              :    is a the tag/discriminator for this EXPR - all basic conditions in the
    5106              :    expression will be given the same CONDITION_UID.  */
    5107              : static tree
    5108       409662 : shortcut_cond_expr (tree expr, unsigned condition_uid)
    5109              : {
    5110       409662 :   tree pred = TREE_OPERAND (expr, 0);
    5111       409662 :   tree then_ = TREE_OPERAND (expr, 1);
    5112       409662 :   tree else_ = TREE_OPERAND (expr, 2);
    5113       409662 :   tree true_label, false_label, end_label, t;
    5114       409662 :   tree *true_label_p;
    5115       409662 :   tree *false_label_p;
    5116       409662 :   bool emit_end, emit_false, jump_over_else;
    5117       409662 :   bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
    5118       409662 :   bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
    5119              : 
    5120       409662 :   tag_shortcut_cond (pred, condition_uid);
    5121              : 
    5122              :   /* First do simple transformations.  */
    5123       409662 :   if (!else_se)
    5124              :     {
    5125              :       /* If there is no 'else', turn
    5126              :            if (a && b) then c
    5127              :          into
    5128              :            if (a) if (b) then c.  */
    5129       297093 :       while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
    5130              :         {
    5131              :           /* Keep the original source location on the first 'if'.  */
    5132        52048 :           location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
    5133        52048 :           TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
    5134              :           /* Set the source location of the && on the second 'if'.  */
    5135        52048 :           if (rexpr_has_location (pred))
    5136        51024 :             SET_EXPR_LOCATION (expr, rexpr_location (pred));
    5137        52048 :           then_ = shortcut_cond_expr (expr, condition_uid);
    5138        52048 :           then_se = then_ && TREE_SIDE_EFFECTS (then_);
    5139        52048 :           pred = TREE_OPERAND (pred, 0);
    5140        52048 :           expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
    5141        52048 :           SET_EXPR_LOCATION (expr, locus);
    5142              :         }
    5143              :     }
    5144              : 
    5145       409662 :   if (!then_se)
    5146              :     {
    5147              :       /* If there is no 'then', turn
    5148              :            if (a || b); else d
    5149              :          into
    5150              :            if (a); else if (b); else d.  */
    5151        25589 :       while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
    5152              :         {
    5153              :           /* Keep the original source location on the first 'if'.  */
    5154          797 :           location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
    5155          797 :           TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
    5156              :           /* Set the source location of the || on the second 'if'.  */
    5157          797 :           if (rexpr_has_location (pred))
    5158          796 :             SET_EXPR_LOCATION (expr, rexpr_location (pred));
    5159          797 :           else_ = shortcut_cond_expr (expr, condition_uid);
    5160          797 :           else_se = else_ && TREE_SIDE_EFFECTS (else_);
    5161          797 :           pred = TREE_OPERAND (pred, 0);
    5162          797 :           expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
    5163          797 :           SET_EXPR_LOCATION (expr, locus);
    5164              :         }
    5165              :     }
    5166              : 
    5167              :   /* The expr tree should also have the expression id set.  */
    5168       409662 :   tree_associate_condition_with_expr (expr, condition_uid);
    5169              : 
    5170              :   /* If we're done, great.  */
    5171       409662 :   if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
    5172       409662 :       && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
    5173        98498 :     return expr;
    5174              : 
    5175              :   /* Otherwise we need to mess with gotos.  Change
    5176              :        if (a) c; else d;
    5177              :      to
    5178              :        if (a); else goto no;
    5179              :        c; goto end;
    5180              :        no: d; end:
    5181              :      and recursively gimplify the condition.  */
    5182              : 
    5183       311164 :   true_label = false_label = end_label = NULL_TREE;
    5184              : 
    5185              :   /* If our arms just jump somewhere, hijack those labels so we don't
    5186              :      generate jumps to jumps.  */
    5187              : 
    5188       311164 :   if (tree then_goto = find_goto_label (then_))
    5189              :     {
    5190         4665 :       true_label = GOTO_DESTINATION (then_goto);
    5191         4665 :       then_ = NULL;
    5192         4665 :       then_se = false;
    5193              :     }
    5194              : 
    5195       311164 :   if (tree else_goto = find_goto_label (else_))
    5196              :     {
    5197         4248 :       false_label = GOTO_DESTINATION (else_goto);
    5198         4248 :       else_ = NULL;
    5199         4248 :       else_se = false;
    5200              :     }
    5201              : 
    5202              :   /* If we aren't hijacking a label for the 'then' branch, it falls through.  */
    5203       311164 :   if (true_label)
    5204              :     true_label_p = &true_label;
    5205              :   else
    5206       306499 :     true_label_p = NULL;
    5207              : 
    5208              :   /* The 'else' branch also needs a label if it contains interesting code.  */
    5209       311164 :   if (false_label || else_se)
    5210              :     false_label_p = &false_label;
    5211              :   else
    5212              :     false_label_p = NULL;
    5213              : 
    5214              :   /* If there was nothing else in our arms, just forward the label(s).  */
    5215       151766 :   if (!then_se && !else_se)
    5216         8648 :     return shortcut_cond_r (pred, true_label_p, false_label_p,
    5217        12972 :                             EXPR_LOC_OR_LOC (expr, input_location), condition_uid);
    5218              : 
    5219              :   /* If our last subexpression already has a terminal label, reuse it.  */
    5220       306840 :   if (else_se)
    5221       159398 :     t = expr_last (else_);
    5222       147442 :   else if (then_se)
    5223       147442 :     t = expr_last (then_);
    5224              :   else
    5225              :     t = NULL;
    5226       306840 :   if (t && TREE_CODE (t) == LABEL_EXPR)
    5227          957 :     end_label = LABEL_EXPR_LABEL (t);
    5228              : 
    5229              :   /* If we don't care about jumping to the 'else' branch, jump to the end
    5230              :      if the condition is false.  */
    5231       306840 :   if (!false_label_p)
    5232       147200 :     false_label_p = &end_label;
    5233              : 
    5234              :   /* We only want to emit these labels if we aren't hijacking them.  */
    5235       306840 :   emit_end = (end_label == NULL_TREE);
    5236       306840 :   emit_false = (false_label == NULL_TREE);
    5237              : 
    5238              :   /* We only emit the jump over the else clause if we have to--if the
    5239              :      then clause may fall through.  Otherwise we can wind up with a
    5240              :      useless jump and a useless label at the end of gimplified code,
    5241              :      which will cause us to think that this conditional as a whole
    5242              :      falls through even if it doesn't.  If we then inline a function
    5243              :      which ends with such a condition, that can cause us to issue an
    5244              :      inappropriate warning about control reaching the end of a
    5245              :      non-void function.  */
    5246       306840 :   jump_over_else = block_may_fallthru (then_);
    5247              : 
    5248       613043 :   pred = shortcut_cond_r (pred, true_label_p, false_label_p,
    5249       613043 :                           EXPR_LOC_OR_LOC (expr, input_location),
    5250              :                           condition_uid);
    5251              : 
    5252       306840 :   expr = NULL;
    5253       306840 :   append_to_statement_list (pred, &expr);
    5254              : 
    5255       306840 :   append_to_statement_list (then_, &expr);
    5256       306840 :   if (else_se)
    5257              :     {
    5258       159398 :       if (jump_over_else)
    5259              :         {
    5260       156847 :           tree last = expr_last (expr);
    5261       156847 :           t = build_and_jump (&end_label);
    5262       156847 :           if (rexpr_has_location (last))
    5263        37963 :             SET_EXPR_LOCATION (t, rexpr_location (last));
    5264       156847 :           append_to_statement_list (t, &expr);
    5265              :         }
    5266       159398 :       if (emit_false)
    5267              :         {
    5268       159398 :           t = build1 (LABEL_EXPR, void_type_node, false_label);
    5269       159398 :           append_to_statement_list (t, &expr);
    5270              :         }
    5271       159398 :       append_to_statement_list (else_, &expr);
    5272              :     }
    5273       306840 :   if (emit_end && end_label)
    5274              :     {
    5275       303096 :       t = build1 (LABEL_EXPR, void_type_node, end_label);
    5276       303096 :       append_to_statement_list (t, &expr);
    5277              :     }
    5278              : 
    5279       306840 :   return expr;
    5280              : }
    5281              : 
    5282              : /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE.  */
    5283              : 
    5284              : tree
    5285     17129741 : gimple_boolify (tree expr)
    5286              : {
    5287     17129741 :   tree type = TREE_TYPE (expr);
    5288     17129741 :   location_t loc = EXPR_LOCATION (expr);
    5289              : 
    5290     17129741 :   if (TREE_CODE (expr) == NE_EXPR
    5291      5840611 :       && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
    5292     18091617 :       && integer_zerop (TREE_OPERAND (expr, 1)))
    5293              :     {
    5294       497375 :       tree call = TREE_OPERAND (expr, 0);
    5295       497375 :       tree fn = get_callee_fndecl (call);
    5296              : 
    5297              :       /* For __builtin_expect ((long) (x), y) recurse into x as well
    5298              :          if x is truth_value_p.  */
    5299       497375 :       if (fn
    5300       496359 :           && fndecl_built_in_p (fn, BUILT_IN_EXPECT)
    5301       635272 :           && call_expr_nargs (call) == 2)
    5302              :         {
    5303       137897 :           tree arg = CALL_EXPR_ARG (call, 0);
    5304       137897 :           if (arg)
    5305              :             {
    5306       137897 :               if (TREE_CODE (arg) == NOP_EXPR
    5307       137897 :                   && TREE_TYPE (arg) == TREE_TYPE (call))
    5308        65158 :                 arg = TREE_OPERAND (arg, 0);
    5309       137897 :               if (truth_value_p (TREE_CODE (arg)))
    5310              :                 {
    5311       114411 :                   arg = gimple_boolify (arg);
    5312       114411 :                   CALL_EXPR_ARG (call, 0)
    5313       228822 :                     = fold_convert_loc (loc, TREE_TYPE (call), arg);
    5314              :                 }
    5315              :             }
    5316              :         }
    5317              :     }
    5318              : 
    5319     17129741 :   switch (TREE_CODE (expr))
    5320              :     {
    5321      1401957 :     case TRUTH_AND_EXPR:
    5322      1401957 :     case TRUTH_OR_EXPR:
    5323      1401957 :     case TRUTH_XOR_EXPR:
    5324      1401957 :     case TRUTH_ANDIF_EXPR:
    5325      1401957 :     case TRUTH_ORIF_EXPR:
    5326              :       /* Also boolify the arguments of truth exprs.  */
    5327      1401957 :       TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
    5328              :       /* FALLTHRU */
    5329              : 
    5330      1836196 :     case TRUTH_NOT_EXPR:
    5331      1836196 :       TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
    5332              : 
    5333              :       /* These expressions always produce boolean results.  */
    5334      1836196 :       if (TREE_CODE (type) != BOOLEAN_TYPE)
    5335       311975 :         TREE_TYPE (expr) = boolean_type_node;
    5336              :       return expr;
    5337              : 
    5338         5550 :     case ANNOTATE_EXPR:
    5339         5550 :       switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
    5340              :         {
    5341         5550 :         case annot_expr_ivdep_kind:
    5342         5550 :         case annot_expr_unroll_kind:
    5343         5550 :         case annot_expr_no_vector_kind:
    5344         5550 :         case annot_expr_vector_kind:
    5345         5550 :         case annot_expr_parallel_kind:
    5346         5550 :         case annot_expr_maybe_infinite_kind:
    5347         5550 :           TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
    5348         5550 :           if (TREE_CODE (type) != BOOLEAN_TYPE)
    5349         3474 :             TREE_TYPE (expr) = boolean_type_node;
    5350              :           return expr;
    5351            0 :         default:
    5352            0 :           gcc_unreachable ();
    5353              :         }
    5354              : 
    5355     15287995 :     default:
    5356     15287995 :       if (COMPARISON_CLASS_P (expr))
    5357              :         {
    5358              :           /* These expressions always produce boolean results.  */
    5359     13391110 :           if (TREE_CODE (type) != BOOLEAN_TYPE)
    5360      1621417 :             TREE_TYPE (expr) = boolean_type_node;
    5361              :           return expr;
    5362              :         }
    5363              :       /* Other expressions that get here must have boolean values, but
    5364              :          might need to be converted to the appropriate mode.  */
    5365      1896885 :       if (TREE_CODE (type) == BOOLEAN_TYPE)
    5366              :         return expr;
    5367       207114 :       return fold_convert_loc (loc, boolean_type_node, expr);
    5368              :     }
    5369              : }
    5370              : 
    5371              : /* Given a conditional expression *EXPR_P without side effects, gimplify
    5372              :    its operands.  New statements are inserted to PRE_P.  */
    5373              : 
    5374              : static enum gimplify_status
    5375        46362 : gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
    5376              : {
    5377        46362 :   tree expr = *expr_p, cond;
    5378        46362 :   enum gimplify_status ret, tret;
    5379        46362 :   enum tree_code code;
    5380              : 
    5381        46362 :   cond = gimple_boolify (COND_EXPR_COND (expr));
    5382              : 
    5383              :   /* We need to handle && and || specially, as their gimplification
    5384              :      creates pure cond_expr, thus leading to an infinite cycle otherwise.  */
    5385        46362 :   code = TREE_CODE (cond);
    5386        46362 :   if (code == TRUTH_ANDIF_EXPR)
    5387           39 :     TREE_SET_CODE (cond, TRUTH_AND_EXPR);
    5388        46323 :   else if (code == TRUTH_ORIF_EXPR)
    5389         4673 :     TREE_SET_CODE (cond, TRUTH_OR_EXPR);
    5390        46362 :   ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_val, fb_rvalue);
    5391        46362 :   COND_EXPR_COND (*expr_p) = cond;
    5392              : 
    5393        46362 :   tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
    5394              :                         is_gimple_val, fb_rvalue);
    5395        46362 :   ret = MIN (ret, tret);
    5396        46362 :   tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
    5397              :                         is_gimple_val, fb_rvalue);
    5398              : 
    5399        46362 :   return MIN (ret, tret);
    5400              : }
    5401              : 
    5402              : /* Return true if evaluating EXPR could trap.
    5403              :    EXPR is GENERIC, while tree_could_trap_p can be called
    5404              :    only on GIMPLE.  */
    5405              : 
    5406              : bool
    5407     16485393 : generic_expr_could_trap_p (tree expr)
    5408              : {
    5409     16485393 :   unsigned i, n;
    5410              : 
    5411     16485393 :   if (!expr || is_gimple_val (expr))
    5412              :     return false;
    5413              : 
    5414      9202284 :   if (!EXPR_P (expr) || tree_could_trap_p (expr))
    5415              :     return true;
    5416              : 
    5417      6753263 :   n = TREE_OPERAND_LENGTH (expr);
    5418     16114590 :   for (i = 0; i < n; i++)
    5419     11683004 :     if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
    5420              :       return true;
    5421              : 
    5422              :   return false;
    5423              : }
    5424              : 
    5425              : /* Associate the condition STMT with the discriminator UID.  STMTs that are
    5426              :    broken down with ANDIF/ORIF from the same Boolean expression should be given
    5427              :    the same UID; 'if (a && b && c) { if (d || e) ... } ...' should yield the
    5428              :    { a: 1, b: 1, c: 1, d: 2, e: 2 } when gimplification is done.  This is used
    5429              :    for condition coverage.  */
    5430              : static void
    5431      5575378 : gimple_associate_condition_with_expr (struct function *fn, gcond *stmt,
    5432              :                                       unsigned uid)
    5433              : {
    5434      5575378 :   if (!condition_coverage_flag)
    5435              :     return;
    5436              : 
    5437          657 :   if (!fn->cond_uids)
    5438          144 :     fn->cond_uids = new hash_map <gcond*, unsigned> ();
    5439              : 
    5440          657 :   fn->cond_uids->put (stmt, uid);
    5441              : }
    5442              : 
    5443              : /*  Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
    5444              :     into
    5445              : 
    5446              :     if (p)                      if (p)
    5447              :       t1 = a;                     a;
    5448              :     else                or      else
    5449              :       t1 = b;                     b;
    5450              :     t1;
    5451              : 
    5452              :     The second form is used when *EXPR_P is of type void.
    5453              : 
    5454              :     PRE_P points to the list where side effects that must happen before
    5455              :       *EXPR_P should be stored.  */
    5456              : 
    5457              : static enum gimplify_status
    5458      6354363 : gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
    5459              : {
    5460      6354363 :   tree expr = *expr_p;
    5461      6354363 :   tree type = TREE_TYPE (expr);
    5462      6354363 :   location_t loc = EXPR_LOCATION (expr);
    5463      6354363 :   tree tmp, arm1, arm2;
    5464      6354363 :   enum gimplify_status ret;
    5465      6354363 :   tree label_true, label_false, label_cont;
    5466      6354363 :   bool have_then_clause_p, have_else_clause_p;
    5467      6354363 :   gcond *cond_stmt;
    5468      6354363 :   enum tree_code pred_code;
    5469      6354363 :   gimple_seq seq = NULL;
    5470              : 
    5471              :   /* If this COND_EXPR has a value, copy the values into a temporary within
    5472              :      the arms.  */
    5473      6354363 :   if (!VOID_TYPE_P (type))
    5474              :     {
    5475       421388 :       tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
    5476       421388 :       tree result;
    5477              : 
    5478              :       /* If either an rvalue is ok or we do not require an lvalue, create the
    5479              :          temporary.  But we cannot do that if the type is addressable.  */
    5480       421388 :       if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
    5481       421381 :           && !TREE_ADDRESSABLE (type))
    5482              :         {
    5483       421381 :           if (gimplify_ctxp->allow_rhs_cond_expr
    5484              :               /* If either branch has side effects or could trap, it can't be
    5485              :                  evaluated unconditionally.  */
    5486        46362 :               && !TREE_SIDE_EFFECTS (then_)
    5487        46362 :               && !generic_expr_could_trap_p (then_)
    5488        46362 :               && !TREE_SIDE_EFFECTS (else_)
    5489       467743 :               && !generic_expr_could_trap_p (else_))
    5490        46362 :             return gimplify_pure_cond_expr (expr_p, pre_p);
    5491              : 
    5492       375019 :           tmp = create_tmp_var (type, "iftmp");
    5493       375019 :           result = tmp;
    5494              :         }
    5495              : 
    5496              :       /* Otherwise, only create and copy references to the values.  */
    5497              :       else
    5498              :         {
    5499            7 :           type = build_pointer_type (type);
    5500              : 
    5501            7 :           if (!VOID_TYPE_P (TREE_TYPE (then_)))
    5502            7 :             then_ = build_fold_addr_expr_loc (loc, then_);
    5503              : 
    5504            7 :           if (!VOID_TYPE_P (TREE_TYPE (else_)))
    5505            7 :             else_ = build_fold_addr_expr_loc (loc, else_);
    5506              : 
    5507            7 :           expr
    5508            7 :             = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
    5509              : 
    5510            7 :           tmp = create_tmp_var (type, "iftmp");
    5511            7 :           result = build_simple_mem_ref_loc (loc, tmp);
    5512              :         }
    5513              : 
    5514              :       /* Build the new then clause, `tmp = then_;'.  But don't build the
    5515              :          assignment if the value is void; in C++ it can be if it's a throw.  */
    5516       375026 :       if (!VOID_TYPE_P (TREE_TYPE (then_)))
    5517       293448 :         TREE_OPERAND (expr, 1) = build2 (INIT_EXPR, type, tmp, then_);
    5518              : 
    5519              :       /* Similarly, build the new else clause, `tmp = else_;'.  */
    5520       375026 :       if (!VOID_TYPE_P (TREE_TYPE (else_)))
    5521       364352 :         TREE_OPERAND (expr, 2) = build2 (INIT_EXPR, type, tmp, else_);
    5522              : 
    5523       375026 :       TREE_TYPE (expr) = void_type_node;
    5524       375026 :       recalculate_side_effects (expr);
    5525              : 
    5526              :       /* Move the COND_EXPR to the prequeue.  */
    5527       375026 :       gimplify_stmt (&expr, pre_p);
    5528              : 
    5529       375026 :       *expr_p = result;
    5530       375026 :       return GS_ALL_DONE;
    5531              :     }
    5532              : 
    5533              :   /* Remove any COMPOUND_EXPR so the following cases will be caught.  */
    5534      5948494 :   STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
    5535      5932975 :   if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
    5536        81173 :     gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
    5537              : 
    5538              :   /* Make sure the condition has BOOLEAN_TYPE.  */
    5539      5932975 :   TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
    5540              : 
    5541              :   /* Break apart && and || conditions.  */
    5542      5932975 :   if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
    5543      5932975 :       || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
    5544              :     {
    5545       356817 :       expr = shortcut_cond_expr (expr, next_cond_uid ());
    5546              : 
    5547       356817 :       if (expr != *expr_p)
    5548              :         {
    5549       356817 :           *expr_p = expr;
    5550              : 
    5551              :           /* We can't rely on gimplify_expr to re-gimplify the expanded
    5552              :              form properly, as cleanups might cause the target labels to be
    5553              :              wrapped in a TRY_FINALLY_EXPR.  To prevent that, we need to
    5554              :              set up a conditional context.  */
    5555       356817 :           gimple_push_condition ();
    5556       356817 :           gimplify_stmt (expr_p, &seq);
    5557       356817 :           gimple_pop_condition (pre_p);
    5558       356817 :           gimple_seq_add_seq (pre_p, seq);
    5559              : 
    5560       356817 :           return GS_ALL_DONE;
    5561              :         }
    5562              :     }
    5563              : 
    5564              :   /* Now do the normal gimplification.  */
    5565              : 
    5566              :   /* Gimplify condition.  */
    5567      5576158 :   ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
    5568              :                        is_gimple_condexpr_for_cond, fb_rvalue);
    5569      5576158 :   if (ret == GS_ERROR)
    5570              :     return GS_ERROR;
    5571      5575378 :   gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
    5572              : 
    5573      5575378 :   gimple_push_condition ();
    5574              : 
    5575      5575378 :   have_then_clause_p = have_else_clause_p = false;
    5576      5575378 :   label_true = find_goto_label (TREE_OPERAND (expr, 1));
    5577      5575378 :   if (label_true
    5578      1313360 :       && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
    5579              :       /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
    5580              :          have different locations, otherwise we end up with incorrect
    5581              :          location information on the branches.  */
    5582      6888738 :       && (optimize
    5583       242694 :           || !EXPR_HAS_LOCATION (expr)
    5584       215737 :           || !rexpr_has_location (label_true)
    5585        77536 :           || EXPR_LOCATION (expr) == rexpr_location (label_true)))
    5586              :     {
    5587      1311075 :       have_then_clause_p = true;
    5588      1311075 :       label_true = GOTO_DESTINATION (label_true);
    5589              :     }
    5590              :   else
    5591      4264303 :     label_true = create_artificial_label (UNKNOWN_LOCATION);
    5592      5575378 :   label_false = find_goto_label (TREE_OPERAND (expr, 2));
    5593      5575378 :   if (label_false
    5594       737446 :       && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
    5595              :       /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
    5596              :          have different locations, otherwise we end up with incorrect
    5597              :          location information on the branches.  */
    5598      6312824 :       && (optimize
    5599       106404 :           || !EXPR_HAS_LOCATION (expr)
    5600       105516 :           || !rexpr_has_location (label_false)
    5601          154 :           || EXPR_LOCATION (expr) == rexpr_location (label_false)))
    5602              :     {
    5603       737304 :       have_else_clause_p = true;
    5604       737304 :       label_false = GOTO_DESTINATION (label_false);
    5605              :     }
    5606              :   else
    5607      4838074 :     label_false = create_artificial_label (UNKNOWN_LOCATION);
    5608              : 
    5609      5575378 :   unsigned cond_uid = 0;
    5610      5575378 :   if (cond_uids)
    5611          467 :     if (unsigned *v = cond_uids->get (expr))
    5612          447 :       cond_uid = *v;
    5613          447 :   if (cond_uid == 0)
    5614      5574931 :     cond_uid = next_cond_uid ();
    5615              : 
    5616      5575378 :   gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
    5617              :                                  &arm2);
    5618      5575378 :   cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
    5619              :                                  label_false);
    5620      5575378 :   gimple_set_location (cond_stmt, EXPR_LOCATION (expr));
    5621      5575378 :   gimple_associate_condition_with_expr (cfun, cond_stmt, cond_uid);
    5622      5575378 :   copy_warning (cond_stmt, COND_EXPR_COND (expr));
    5623      5575378 :   gimplify_seq_add_stmt (&seq, cond_stmt);
    5624      5575378 :   gimple_stmt_iterator gsi = gsi_last (seq);
    5625      5575378 :   maybe_fold_stmt (&gsi);
    5626              : 
    5627      5575378 :   label_cont = NULL_TREE;
    5628      5575378 :   if (!have_then_clause_p)
    5629              :     {
    5630              :       /* For if (...) {} else { code; } put label_true after
    5631              :          the else block.  */
    5632      4264303 :       if (TREE_OPERAND (expr, 1) == NULL_TREE
    5633       200738 :           && !have_else_clause_p
    5634      4265073 :           && TREE_OPERAND (expr, 2) != NULL_TREE)
    5635              :         {
    5636              :           /* For if (0) {} else { code; } tell -Wimplicit-fallthrough
    5637              :              handling that label_cont == label_true can be only reached
    5638              :              through fallthrough from { code; }.  */
    5639          770 :           if (integer_zerop (COND_EXPR_COND (expr)))
    5640            0 :             UNUSED_LABEL_P (label_true) = 1;
    5641              :           label_cont = label_true;
    5642              :         }
    5643              :       else
    5644              :         {
    5645      4263533 :           bool then_side_effects
    5646      4263533 :             = (TREE_OPERAND (expr, 1)
    5647      4263533 :                && TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)));
    5648      4263533 :           gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
    5649      4263533 :           have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
    5650              :           /* For if (...) { code; } else {} or
    5651              :              if (...) { code; } else goto label; or
    5652              :              if (...) { code; return; } else { ... }
    5653              :              label_cont isn't needed.  */
    5654      4263533 :           if (!have_else_clause_p
    5655      4062419 :               && TREE_OPERAND (expr, 2) != NULL_TREE
    5656      7092229 :               && gimple_seq_may_fallthru (seq))
    5657              :             {
    5658      1967367 :               gimple *g;
    5659      1967367 :               label_cont = create_artificial_label (UNKNOWN_LOCATION);
    5660              : 
    5661              :               /* For if (0) { non-side-effect-code } else { code }
    5662              :                  tell -Wimplicit-fallthrough handling that label_cont can
    5663              :                  be only reached through fallthrough from { code }.  */
    5664      1967367 :               if (integer_zerop (COND_EXPR_COND (expr)))
    5665              :                 {
    5666        75801 :                   UNUSED_LABEL_P (label_true) = 1;
    5667        75801 :                   if (!then_side_effects)
    5668        12379 :                     UNUSED_LABEL_P (label_cont) = 1;
    5669              :                 }
    5670              : 
    5671      1967367 :               g = gimple_build_goto (label_cont);
    5672              : 
    5673              :               /* GIMPLE_COND's are very low level; they have embedded
    5674              :                  gotos.  This particular embedded goto should not be marked
    5675              :                  with the location of the original COND_EXPR, as it would
    5676              :                  correspond to the COND_EXPR's condition, not the ELSE or the
    5677              :                  THEN arms.  To avoid marking it with the wrong location, flag
    5678              :                  it as "no location".  */
    5679      1967367 :               gimple_set_do_not_emit_location (g);
    5680              : 
    5681      1967367 :               gimplify_seq_add_stmt (&seq, g);
    5682              :             }
    5683              :         }
    5684              :     }
    5685      5575378 :   if (!have_else_clause_p)
    5686              :     {
    5687              :       /* For if (1) { code } or if (1) { code } else { non-side-effect-code }
    5688              :          tell -Wimplicit-fallthrough handling that label_false can be only
    5689              :          reached through fallthrough from { code }.  */
    5690      4838074 :       if (integer_nonzerop (COND_EXPR_COND (expr))
    5691      4838074 :           && (TREE_OPERAND (expr, 2) == NULL_TREE
    5692        57488 :               || !TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2))))
    5693         7946 :         UNUSED_LABEL_P (label_false) = 1;
    5694      4838074 :       gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
    5695      4838074 :       have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
    5696              :     }
    5697      5575378 :   if (label_cont)
    5698      1968137 :     gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
    5699              : 
    5700      5575378 :   gimple_pop_condition (pre_p);
    5701      5575378 :   gimple_seq_add_seq (pre_p, seq);
    5702              : 
    5703      5575378 :   if (ret == GS_ERROR)
    5704              :     ; /* Do nothing.  */
    5705      5575378 :   else if (have_then_clause_p || have_else_clause_p)
    5706              :     ret = GS_ALL_DONE;
    5707              :   else
    5708              :     {
    5709              :       /* Both arms are empty; replace the COND_EXPR with its predicate.  */
    5710         2969 :       expr = TREE_OPERAND (expr, 0);
    5711         2969 :       gimplify_stmt (&expr, pre_p);
    5712              :     }
    5713              : 
    5714      5575378 :   *expr_p = NULL;
    5715      5575378 :   return ret;
    5716              : }
    5717              : 
    5718              : /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
    5719              :    to be marked addressable.
    5720              : 
    5721              :    We cannot rely on such an expression being directly markable if a temporary
    5722              :    has been created by the gimplification.  In this case, we create another
    5723              :    temporary and initialize it with a copy, which will become a store after we
    5724              :    mark it addressable.  This can happen if the front-end passed us something
    5725              :    that it could not mark addressable yet, like a Fortran pass-by-reference
    5726              :    parameter (int) floatvar.  */
    5727              : 
    5728              : static void
    5729     57321148 : prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
    5730              : {
    5731     62894054 :   while (handled_component_p (*expr_p))
    5732      5572906 :     expr_p = &TREE_OPERAND (*expr_p, 0);
    5733              : 
    5734              :   /* Do not allow an SSA name as the temporary.  */
    5735     57321148 :   if (is_gimple_reg (*expr_p))
    5736        12146 :     *expr_p = internal_get_tmp_var (*expr_p, seq_p, NULL, false, false, true);
    5737     57321148 : }
    5738              : 
    5739              : /* A subroutine of gimplify_modify_expr.  Replace a MODIFY_EXPR with
    5740              :    a call to __builtin_memcpy.  */
    5741              : 
    5742              : static enum gimplify_status
    5743         1110 : gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
    5744              :                                 gimple_seq *seq_p)
    5745              : {
    5746         1110 :   tree t, to, to_ptr, from, from_ptr;
    5747         1110 :   gcall *gs;
    5748         1110 :   location_t loc = EXPR_LOCATION (*expr_p);
    5749              : 
    5750         1110 :   to = TREE_OPERAND (*expr_p, 0);
    5751         1110 :   from = TREE_OPERAND (*expr_p, 1);
    5752         1110 :   gcc_assert (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (to)))
    5753              :               && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (from))));
    5754              : 
    5755              :   /* Mark the RHS addressable.  Beware that it may not be possible to do so
    5756              :      directly if a temporary has been created by the gimplification.  */
    5757         1110 :   prepare_gimple_addressable (&from, seq_p);
    5758              : 
    5759         1110 :   mark_addressable (from);
    5760         1110 :   from_ptr = build_fold_addr_expr_loc (loc, from);
    5761         1110 :   gimplify_arg (&from_ptr, seq_p, loc);
    5762              : 
    5763         1110 :   mark_addressable (to);
    5764         1110 :   to_ptr = build_fold_addr_expr_loc (loc, to);
    5765         1110 :   gimplify_arg (&to_ptr, seq_p, loc);
    5766              : 
    5767         1110 :   t = builtin_decl_implicit (BUILT_IN_MEMCPY);
    5768              : 
    5769         1110 :   gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
    5770         1110 :   gimple_call_set_alloca_for_var (gs, true);
    5771              : 
    5772         1110 :   if (want_value)
    5773              :     {
    5774              :       /* tmp = memcpy() */
    5775            8 :       t = create_tmp_var (TREE_TYPE (to_ptr));
    5776            8 :       gimple_call_set_lhs (gs, t);
    5777            8 :       gimplify_seq_add_stmt (seq_p, gs);
    5778              : 
    5779            8 :       *expr_p = build_simple_mem_ref (t);
    5780            8 :       return GS_ALL_DONE;
    5781              :     }
    5782              : 
    5783         1102 :   gimplify_seq_add_stmt (seq_p, gs);
    5784         1102 :   *expr_p = NULL;
    5785         1102 :   return GS_ALL_DONE;
    5786              : }
    5787              : 
    5788              : /* A subroutine of gimplify_modify_expr.  Replace a MODIFY_EXPR with
    5789              :    a call to __builtin_memset.  In this case we know that the RHS is
    5790              :    a CONSTRUCTOR with an empty element list.  */
    5791              : 
    5792              : static enum gimplify_status
    5793           84 : gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
    5794              :                                 gimple_seq *seq_p)
    5795              : {
    5796           84 :   tree t, from, to, to_ptr;
    5797           84 :   gcall *gs;
    5798           84 :   location_t loc = EXPR_LOCATION (*expr_p);
    5799              : 
    5800              :   /* Assert our assumptions, to abort instead of producing wrong code
    5801              :      silently if they are not met.  Beware that the RHS CONSTRUCTOR might
    5802              :      not be immediately exposed.  */
    5803           84 :   from = TREE_OPERAND (*expr_p, 1);
    5804           84 :   if (TREE_CODE (from) == WITH_SIZE_EXPR)
    5805           84 :     from = TREE_OPERAND (from, 0);
    5806              : 
    5807           84 :   gcc_assert (TREE_CODE (from) == CONSTRUCTOR
    5808              :               && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
    5809              : 
    5810              :   /* Now proceed.  */
    5811           84 :   to = TREE_OPERAND (*expr_p, 0);
    5812           84 :   gcc_assert (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (to))));
    5813              : 
    5814           84 :   to_ptr = build_fold_addr_expr_loc (loc, to);
    5815           84 :   gimplify_arg (&to_ptr, seq_p, loc);
    5816           84 :   t = builtin_decl_implicit (BUILT_IN_MEMSET);
    5817              : 
    5818           84 :   gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
    5819              : 
    5820           84 :   if (want_value)
    5821              :     {
    5822              :       /* tmp = memset() */
    5823            0 :       t = create_tmp_var (TREE_TYPE (to_ptr));
    5824            0 :       gimple_call_set_lhs (gs, t);
    5825            0 :       gimplify_seq_add_stmt (seq_p, gs);
    5826              : 
    5827            0 :       *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
    5828            0 :       return GS_ALL_DONE;
    5829              :     }
    5830              : 
    5831           84 :   gimplify_seq_add_stmt (seq_p, gs);
    5832           84 :   *expr_p = NULL;
    5833           84 :   return GS_ALL_DONE;
    5834              : }
    5835              : 
    5836              : /* A subroutine of gimplify_init_ctor_preeval.  Called via walk_tree,
    5837              :    determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
    5838              :    assignment.  Return non-null if we detect a potential overlap.  */
    5839              : 
    5840              : struct gimplify_init_ctor_preeval_data
    5841              : {
    5842              :   /* The base decl of the lhs object.  May be NULL, in which case we
    5843              :      have to assume the lhs is indirect.  */
    5844              :   tree lhs_base_decl;
    5845              : 
    5846              :   /* The alias set of the lhs object.  */
    5847              :   alias_set_type lhs_alias_set;
    5848              : };
    5849              : 
    5850              : static tree
    5851       116764 : gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
    5852              : {
    5853       116764 :   struct gimplify_init_ctor_preeval_data *data
    5854              :     = (struct gimplify_init_ctor_preeval_data *) xdata;
    5855       116764 :   tree t = *tp;
    5856              : 
    5857              :   /* If we find the base object, obviously we have overlap.  */
    5858       116764 :   if (data->lhs_base_decl == t)
    5859              :     return t;
    5860              : 
    5861              :   /* If the constructor component is indirect, determine if we have a
    5862              :      potential overlap with the lhs.  The only bits of information we
    5863              :      have to go on at this point are addressability and alias sets.  */
    5864       116763 :   if ((INDIRECT_REF_P (t)
    5865       116763 :        || TREE_CODE (t) == MEM_REF)
    5866          651 :       && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
    5867       117279 :       && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
    5868              :     return t;
    5869              : 
    5870              :   /* If the constructor component is a call, determine if it can hide a
    5871              :      potential overlap with the lhs through an INDIRECT_REF like above.
    5872              :      ??? Ugh - this is completely broken.  In fact this whole analysis
    5873              :      doesn't look conservative.  */
    5874       116247 :   if (TREE_CODE (t) == CALL_EXPR)
    5875              :     {
    5876            0 :       tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
    5877              : 
    5878            0 :       for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
    5879            0 :         if (POINTER_TYPE_P (TREE_VALUE (type))
    5880            0 :             && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
    5881            0 :             && alias_sets_conflict_p (data->lhs_alias_set,
    5882              :                                       get_alias_set
    5883            0 :                                         (TREE_TYPE (TREE_VALUE (type)))))
    5884              :           return t;
    5885              :     }
    5886              : 
    5887       116247 :   if (IS_TYPE_OR_DECL_P (t))
    5888        10968 :     *walk_subtrees = 0;
    5889              :   return NULL;
    5890              : }
    5891              : 
    5892              : /* A subroutine of gimplify_init_constructor.  Pre-evaluate EXPR,
    5893              :    force values that overlap with the lhs (as described by *DATA)
    5894              :    into temporaries.  */
    5895              : 
    5896              : static void
    5897       427375 : gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
    5898              :                             struct gimplify_init_ctor_preeval_data *data)
    5899              : {
    5900       427375 :   enum gimplify_status one;
    5901              : 
    5902              :   /* If the value is constant, then there's nothing to pre-evaluate.  */
    5903       427375 :   if (TREE_CONSTANT (*expr_p))
    5904              :     {
    5905              :       /* Ensure it does not have side effects, it might contain a reference to
    5906              :          the object we're initializing.  */
    5907       208291 :       gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
    5908              :       return;
    5909              :     }
    5910              : 
    5911              :   /* If the type has non-trivial constructors, we can't pre-evaluate.  */
    5912       219084 :   if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
    5913              :     return;
    5914              : 
    5915              :   /* Recurse for nested constructors.  */
    5916       219084 :   if (TREE_CODE (*expr_p) == CONSTRUCTOR)
    5917              :     {
    5918        74288 :       unsigned HOST_WIDE_INT ix;
    5919        74288 :       constructor_elt *ce;
    5920        74288 :       vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
    5921              : 
    5922       273508 :       FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
    5923       199220 :         gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
    5924              : 
    5925              :       return;
    5926              :     }
    5927              : 
    5928              :   /* If this is a variable sized type, we must remember the size.  */
    5929       144796 :   maybe_with_size_expr (expr_p);
    5930              : 
    5931              :   /* Gimplify the constructor element to something appropriate for the rhs
    5932              :      of a MODIFY_EXPR.  Given that we know the LHS is an aggregate, we know
    5933              :      the gimplifier will consider this a store to memory.  Doing this
    5934              :      gimplification now means that we won't have to deal with complicated
    5935              :      language-specific trees, nor trees like SAVE_EXPR that can induce
    5936              :      exponential search behavior.  */
    5937       144796 :   one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
    5938       144796 :   if (one == GS_ERROR)
    5939              :     {
    5940            0 :       *expr_p = NULL;
    5941            0 :       return;
    5942              :     }
    5943              : 
    5944              :   /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
    5945              :      with the lhs, since "a = { .x=a }" doesn't make sense.  This will
    5946              :      always be true for all scalars, since is_gimple_mem_rhs insists on a
    5947              :      temporary variable for them.  */
    5948       144796 :   if (DECL_P (*expr_p))
    5949              :     return;
    5950              : 
    5951              :   /* If this is of variable size, we have no choice but to assume it doesn't
    5952              :      overlap since we can't make a temporary for it.  */
    5953       105200 :   if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
    5954              :     return;
    5955              : 
    5956              :   /* Otherwise, we must search for overlap ...  */
    5957       105200 :   if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
    5958              :     return;
    5959              : 
    5960              :   /* ... and if found, force the value into a temporary.  */
    5961          517 :   *expr_p = get_formal_tmp_var (*expr_p, pre_p);
    5962              : }
    5963              : 
    5964              : /* A subroutine of gimplify_init_ctor_eval.  Create a loop for
    5965              :    a RANGE_EXPR in a CONSTRUCTOR for an array.
    5966              : 
    5967              :       var = lower;
    5968              :     loop_entry:
    5969              :       object[var] = value;
    5970              :       if (var == upper)
    5971              :         goto loop_exit;
    5972              :       var = var + 1;
    5973              :       goto loop_entry;
    5974              :     loop_exit:
    5975              : 
    5976              :    We increment var _after_ the loop exit check because we might otherwise
    5977              :    fail if upper == TYPE_MAX_VALUE (type for upper).
    5978              : 
    5979              :    Note that we never have to deal with SAVE_EXPRs here, because this has
    5980              :    already been taken care of for us, in gimplify_init_ctor_preeval().  */
    5981              : 
    5982              : static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
    5983              :                                      gimple_seq *, bool);
    5984              : 
    5985              : static void
    5986          583 : gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
    5987              :                                tree value, tree array_elt_type,
    5988              :                                gimple_seq *pre_p, bool cleared)
    5989              : {
    5990          583 :   tree loop_entry_label, loop_exit_label, fall_thru_label;
    5991          583 :   tree var, var_type, cref, tmp;
    5992              : 
    5993          583 :   loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
    5994          583 :   loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
    5995          583 :   fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
    5996              : 
    5997              :   /* Create and initialize the index variable.  */
    5998          583 :   var_type = TREE_TYPE (upper);
    5999          583 :   var = create_tmp_var (var_type);
    6000          583 :   gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
    6001              : 
    6002              :   /* Add the loop entry label.  */
    6003          583 :   gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
    6004              : 
    6005              :   /* Build the reference.  */
    6006          583 :   cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
    6007              :                  var, NULL_TREE, NULL_TREE);
    6008              : 
    6009              :   /* If we are a constructor, just call gimplify_init_ctor_eval to do
    6010              :      the store.  Otherwise just assign value to the reference.  */
    6011              : 
    6012          583 :   if (TREE_CODE (value) == CONSTRUCTOR)
    6013              :     /* NB we might have to call ourself recursively through
    6014              :        gimplify_init_ctor_eval if the value is a constructor.  */
    6015          129 :     gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
    6016              :                              pre_p, cleared);
    6017              :   else
    6018              :     {
    6019          454 :       if (gimplify_expr (&value, pre_p, NULL, is_gimple_val, fb_rvalue)
    6020              :           != GS_ERROR)
    6021          454 :         gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
    6022              :     }
    6023              : 
    6024              :   /* We exit the loop when the index var is equal to the upper bound.  */
    6025         1166 :   gimplify_seq_add_stmt (pre_p,
    6026          583 :                          gimple_build_cond (EQ_EXPR, var, upper,
    6027              :                                             loop_exit_label, fall_thru_label));
    6028              : 
    6029          583 :   gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
    6030              : 
    6031              :   /* Otherwise, increment the index var...  */
    6032          583 :   tmp = build2 (PLUS_EXPR, var_type, var,
    6033              :                 fold_convert (var_type, integer_one_node));
    6034          583 :   gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
    6035              : 
    6036              :   /* ...and jump back to the loop entry.  */
    6037          583 :   gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
    6038              : 
    6039              :   /* Add the loop exit label.  */
    6040          583 :   gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
    6041          583 : }
    6042              : 
    6043              : /* A subroutine of gimplify_init_constructor.  Generate individual
    6044              :    MODIFY_EXPRs for a CONSTRUCTOR.  OBJECT is the LHS against which the
    6045              :    assignments should happen.  ELTS is the CONSTRUCTOR_ELTS of the
    6046              :    CONSTRUCTOR.  CLEARED is true if the entire LHS object has been
    6047              :    zeroed first.  */
    6048              : 
    6049              : static void
    6050      1284322 : gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
    6051              :                          gimple_seq *pre_p, bool cleared)
    6052              : {
    6053      1284322 :   tree array_elt_type = NULL;
    6054      1284322 :   unsigned HOST_WIDE_INT ix;
    6055      1284322 :   tree purpose, value;
    6056              : 
    6057      1284322 :   if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
    6058       142611 :     array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
    6059              : 
    6060      4754432 :   FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
    6061              :     {
    6062      3470110 :       tree cref;
    6063              : 
    6064              :       /* NULL values are created above for gimplification errors.  */
    6065      3470110 :       if (value == NULL)
    6066       336787 :         continue;
    6067              : 
    6068      3470110 :       if (cleared && initializer_zerop (value))
    6069       336034 :         continue;
    6070              : 
    6071              :       /* ??? Here's to hoping the front end fills in all of the indices,
    6072              :          so we don't have to figure out what's missing ourselves.  */
    6073      3134076 :       gcc_assert (purpose);
    6074              : 
    6075              :       /* Skip zero-sized fields, unless value has side-effects.  This can
    6076              :          happen with calls to functions returning a empty type, which
    6077              :          we shouldn't discard.  As a number of downstream passes don't
    6078              :          expect sets of empty type fields, we rely on the gimplification of
    6079              :          the MODIFY_EXPR we make below to drop the assignment statement.  */
    6080      3134076 :       if (!TREE_SIDE_EFFECTS (value)
    6081      2838535 :           && TREE_CODE (purpose) == FIELD_DECL
    6082      5498195 :           && is_empty_type (TREE_TYPE (purpose)))
    6083          170 :         continue;
    6084              : 
    6085              :       /* If we have a RANGE_EXPR, we have to build a loop to assign the
    6086              :          whole range.  */
    6087      3133906 :       if (TREE_CODE (purpose) == RANGE_EXPR)
    6088              :         {
    6089          599 :           tree lower = TREE_OPERAND (purpose, 0);
    6090          599 :           tree upper = TREE_OPERAND (purpose, 1);
    6091              : 
    6092              :           /* If the lower bound is equal to upper, just treat it as if
    6093              :              upper was the index.  */
    6094          599 :           if (simple_cst_equal (lower, upper))
    6095              :             purpose = upper;
    6096              :           else
    6097              :             {
    6098          583 :               gimplify_init_ctor_eval_range (object, lower, upper, value,
    6099              :                                              array_elt_type, pre_p, cleared);
    6100          583 :               continue;
    6101              :             }
    6102              :         }
    6103              : 
    6104      3133323 :       if (array_elt_type)
    6105              :         {
    6106              :           /* Do not use bitsizetype for ARRAY_REF indices.  */
    6107       564655 :           if (TYPE_DOMAIN (TREE_TYPE (object)))
    6108       564655 :             purpose
    6109       564655 :               = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
    6110              :                               purpose);
    6111       564655 :           cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
    6112              :                          purpose, NULL_TREE, NULL_TREE);
    6113              :         }
    6114              :       else
    6115              :         {
    6116      2568668 :           gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
    6117      2568668 :           cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
    6118              :                          unshare_expr (object), purpose, NULL_TREE);
    6119              :         }
    6120              : 
    6121      3133323 :       if (TREE_CODE (value) == CONSTRUCTOR
    6122      3133323 :           && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
    6123       303243 :         gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
    6124              :                                  pre_p, cleared);
    6125      2830080 :       else if (TREE_CODE (value) == RAW_DATA_CST)
    6126              :         {
    6127           43 :           if (RAW_DATA_LENGTH (value) <= 32)
    6128              :             {
    6129           13 :               for (unsigned int i = 0; i < (unsigned) RAW_DATA_LENGTH (value);
    6130              :                    ++i)
    6131           12 :                 if (!cleared || RAW_DATA_POINTER (value)[i])
    6132              :                   {
    6133           12 :                     if (i)
    6134              :                       {
    6135           11 :                         tree p
    6136           11 :                           = fold_build2 (PLUS_EXPR, TREE_TYPE (purpose),
    6137              :                                          purpose,
    6138              :                                          build_int_cst (TREE_TYPE (purpose),
    6139              :                                                         i));
    6140           11 :                         cref = build4 (ARRAY_REF, array_elt_type,
    6141              :                                        unshare_expr (object), p, NULL_TREE,
    6142              :                                        NULL_TREE);
    6143              :                       }
    6144           12 :                     tree init
    6145           12 :                       = build2 (INIT_EXPR, TREE_TYPE (cref), cref,
    6146           12 :                                 build_int_cst (TREE_TYPE (value),
    6147           12 :                                                RAW_DATA_UCHAR_ELT (value, i)));
    6148           12 :                     gimplify_and_add (init, pre_p);
    6149           12 :                     ggc_free (init);
    6150              :                   }
    6151              :             }
    6152              :           else
    6153              :             {
    6154           42 :               tree rtype = build_array_type_nelts (TREE_TYPE (value),
    6155           42 :                                                    RAW_DATA_LENGTH (value));
    6156           42 :               tree rctor = build_constructor_single (rtype, bitsize_zero_node,
    6157              :                                                      value);
    6158           42 :               tree addr = build_fold_addr_expr (cref);
    6159           42 :               cref = build2 (MEM_REF, rtype, addr,
    6160              :                              build_int_cst (ptr_type_node, 0));
    6161           42 :               rctor = tree_output_constant_def (rctor);
    6162           42 :               if (!useless_type_conversion_p (rtype, TREE_TYPE (rctor)))
    6163            4 :                 rctor = build1 (VIEW_CONVERT_EXPR, rtype, rctor);
    6164           42 :               if (gimplify_expr (&cref, pre_p, NULL, is_gimple_lvalue,
    6165              :                                  fb_lvalue) != GS_ERROR)
    6166           84 :                 gimplify_seq_add_stmt (pre_p,
    6167           42 :                                        gimple_build_assign (cref, rctor));
    6168              :             }
    6169              :         }
    6170              :       else
    6171              :         {
    6172      2830037 :           tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
    6173      2830037 :           gimplify_and_add (init, pre_p);
    6174      2830037 :           ggc_free (init);
    6175              :         }
    6176              :     }
    6177      1284322 : }
    6178              : 
    6179              : /* Return the appropriate RHS predicate for this LHS.  */
    6180              : 
    6181              : gimple_predicate
    6182     50306400 : rhs_predicate_for (tree lhs)
    6183              : {
    6184     50306400 :   if (is_gimple_reg (lhs))
    6185              :     return is_gimple_reg_rhs_or_call;
    6186              :   else
    6187     11923659 :     return is_gimple_mem_rhs_or_call;
    6188              : }
    6189              : 
    6190              : /* Return the initial guess for an appropriate RHS predicate for this LHS,
    6191              :    before the LHS has been gimplified.  */
    6192              : 
    6193              : static gimple_predicate
    6194     49076493 : initial_rhs_predicate_for (tree lhs)
    6195              : {
    6196     49076493 :   if (is_gimple_reg_type (TREE_TYPE (lhs)))
    6197              :     return is_gimple_reg_rhs_or_call;
    6198              :   else
    6199      2463400 :     return is_gimple_mem_rhs_or_call;
    6200              : }
    6201              : 
    6202              : /* Gimplify a C99 compound literal expression.  This just means adding
    6203              :    the DECL_EXPR before the current statement and using its anonymous
    6204              :    decl instead.  */
    6205              : 
    6206              : static enum gimplify_status
    6207        37446 : gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
    6208              :                                 bool (*gimple_test_f) (tree),
    6209              :                                 fallback_t fallback)
    6210              : {
    6211        37446 :   tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
    6212        37446 :   tree decl = DECL_EXPR_DECL (decl_s);
    6213        37446 :   tree init = DECL_INITIAL (decl);
    6214              :   /* Mark the decl as addressable if the compound literal
    6215              :      expression is addressable now, otherwise it is marked too late
    6216              :      after we gimplify the initialization expression.  */
    6217        37446 :   if (TREE_ADDRESSABLE (*expr_p))
    6218          537 :     TREE_ADDRESSABLE (decl) = 1;
    6219              :   /* Otherwise, if we don't need an lvalue and have a literal directly
    6220              :      substitute it.  Check if it matches the gimple predicate, as
    6221              :      otherwise we'd generate a new temporary, and we can as well just
    6222              :      use the decl we already have.  */
    6223        36909 :   else if (!TREE_ADDRESSABLE (decl)
    6224        36909 :            && !TREE_THIS_VOLATILE (decl)
    6225        36904 :            && init
    6226        36896 :            && (fallback & fb_lvalue) == 0
    6227        73625 :            && gimple_test_f (init))
    6228              :     {
    6229        35623 :       *expr_p = init;
    6230        35623 :       return GS_OK;
    6231              :     }
    6232              : 
    6233              :   /* If the decl is not addressable, then it is being used in some
    6234              :      expression or on the right hand side of a statement, and it can
    6235              :      be put into a readonly data section.  */
    6236         1823 :   if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
    6237         1098 :     TREE_READONLY (decl) = 1;
    6238              : 
    6239              :   /* This decl isn't mentioned in the enclosing block, so add it to the
    6240              :      list of temps.  FIXME it seems a bit of a kludge to say that
    6241              :      anonymous artificial vars aren't pushed, but everything else is.  */
    6242         1823 :   if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
    6243            2 :     gimple_add_tmp_var (decl);
    6244              : 
    6245         1823 :   gimplify_and_add (decl_s, pre_p);
    6246         1823 :   *expr_p = decl;
    6247         1823 :   return GS_OK;
    6248              : }
    6249              : 
    6250              : /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
    6251              :    return a new CONSTRUCTOR if something changed.  */
    6252              : 
    6253              : static tree
    6254      1918202 : optimize_compound_literals_in_ctor (tree orig_ctor)
    6255              : {
    6256      1918202 :   tree ctor = orig_ctor;
    6257      1918202 :   vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
    6258      1918202 :   unsigned int idx, num = vec_safe_length (elts);
    6259              : 
    6260      6352586 :   for (idx = 0; idx < num; idx++)
    6261              :     {
    6262      4434384 :       tree value = (*elts)[idx].value;
    6263      4434384 :       tree newval = value;
    6264      4434384 :       if (TREE_CODE (value) == CONSTRUCTOR)
    6265       545649 :         newval = optimize_compound_literals_in_ctor (value);
    6266      3888735 :       else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
    6267              :         {
    6268          103 :           tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
    6269          103 :           tree decl = DECL_EXPR_DECL (decl_s);
    6270          103 :           tree init = DECL_INITIAL (decl);
    6271              : 
    6272          103 :           if (!TREE_ADDRESSABLE (value)
    6273          103 :               && !TREE_ADDRESSABLE (decl)
    6274          103 :               && init
    6275          103 :               && TREE_CODE (init) == CONSTRUCTOR)
    6276           52 :             newval = optimize_compound_literals_in_ctor (init);
    6277              :         }
    6278      4434384 :       if (newval == value)
    6279      4434324 :         continue;
    6280              : 
    6281           60 :       if (ctor == orig_ctor)
    6282              :         {
    6283           49 :           ctor = copy_node (orig_ctor);
    6284           98 :           CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
    6285           49 :           elts = CONSTRUCTOR_ELTS (ctor);
    6286              :         }
    6287           60 :       (*elts)[idx].value = newval;
    6288              :     }
    6289      1918202 :   return ctor;
    6290              : }
    6291              : 
    6292              : /* A subroutine of gimplify_modify_expr.  Break out elements of a
    6293              :    CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
    6294              : 
    6295              :    Note that we still need to clear any elements that don't have explicit
    6296              :    initializers, so if not all elements are initialized we keep the
    6297              :    original MODIFY_EXPR, we just remove all of the constructor elements.
    6298              : 
    6299              :    If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
    6300              :    GS_ERROR if we would have to create a temporary when gimplifying
    6301              :    this constructor.  Otherwise, return GS_OK.
    6302              : 
    6303              :    If NOTIFY_TEMP_CREATION is false, just do the gimplification.  */
    6304              : 
    6305              : static enum gimplify_status
    6306      1372501 : gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
    6307              :                            bool want_value, bool notify_temp_creation)
    6308              : {
    6309      1372501 :   tree object, ctor, type;
    6310      1372501 :   enum gimplify_status ret;
    6311      1372501 :   vec<constructor_elt, va_gc> *elts;
    6312      1372501 :   bool cleared = false;
    6313      1372501 :   bool is_empty_ctor = false;
    6314      1372501 :   bool is_init_expr = (TREE_CODE (*expr_p) == INIT_EXPR);
    6315              : 
    6316      1372501 :   gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
    6317              : 
    6318      1372501 :   if (!notify_temp_creation)
    6319              :     {
    6320      1363833 :       ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
    6321              :                            is_gimple_lvalue, fb_lvalue);
    6322      1363833 :       if (ret == GS_ERROR)
    6323              :         return ret;
    6324              :     }
    6325              : 
    6326      1372501 :   object = TREE_OPERAND (*expr_p, 0);
    6327      1372501 :   ctor = TREE_OPERAND (*expr_p, 1)
    6328      1372501 :     = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
    6329      1372501 :   type = TREE_TYPE (ctor);
    6330      1372501 :   elts = CONSTRUCTOR_ELTS (ctor);
    6331      1372501 :   ret = GS_ALL_DONE;
    6332              : 
    6333      1372501 :   switch (TREE_CODE (type))
    6334              :     {
    6335      1363737 :     case RECORD_TYPE:
    6336      1363737 :     case UNION_TYPE:
    6337      1363737 :     case QUAL_UNION_TYPE:
    6338      1363737 :     case ARRAY_TYPE:
    6339      1363737 :       {
    6340              :         /* Use readonly data for initializers of this or smaller size
    6341              :            regardless of the num_nonzero_elements / num_unique_nonzero_elements
    6342              :            ratio.  */
    6343      1363737 :         const HOST_WIDE_INT min_unique_size = 64;
    6344              :         /* If num_nonzero_elements / num_unique_nonzero_elements ratio
    6345              :            is smaller than this, use readonly data.  */
    6346      1363737 :         const int unique_nonzero_ratio = 8;
    6347              :         /* True if a single access of the object must be ensured.  This is the
    6348              :            case if the target is volatile, the type is non-addressable and more
    6349              :            than one field need to be assigned.  */
    6350      1363737 :         const bool ensure_single_access
    6351      1363737 :           = TREE_THIS_VOLATILE (object)
    6352          275 :             && !TREE_ADDRESSABLE (type)
    6353      1363950 :             && vec_safe_length (elts) > 1;
    6354      1363737 :         struct gimplify_init_ctor_preeval_data preeval_data;
    6355      1363737 :         HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
    6356      1363737 :         HOST_WIDE_INT num_unique_nonzero_elements;
    6357      1363737 :         ctor_completeness complete_p;
    6358      1363737 :         bool valid_const_initializer;
    6359              : 
    6360              :         /* Aggregate types must lower constructors to initialization of
    6361              :            individual elements.  The exception is that a CONSTRUCTOR node
    6362              :            with no elements indicates zero-initialization of the whole.  */
    6363      1363737 :         if (vec_safe_is_empty (elts))
    6364              :           {
    6365       347812 :             if (notify_temp_creation)
    6366        10206 :               return GS_OK;
    6367              : 
    6368              :             /* The var will be initialized and so appear on lhs of
    6369              :                assignment, it can't be TREE_READONLY anymore.  */
    6370       347764 :             if (VAR_P (object))
    6371       189686 :               TREE_READONLY (object) = 0;
    6372              : 
    6373              :             is_empty_ctor = true;
    6374       352330 :             break;
    6375              :           }
    6376              : 
    6377              :         /* Fetch information about the constructor to direct later processing.
    6378              :            We might want to make static versions of it in various cases, and
    6379              :            can only do so if it known to be a valid constant initializer.  */
    6380      1015925 :         valid_const_initializer
    6381      1015925 :           = categorize_ctor_elements (ctor, &num_nonzero_elements,
    6382              :                                       &num_unique_nonzero_elements,
    6383              :                                       &num_ctor_elements, &complete_p);
    6384              : 
    6385              :         /* If a const aggregate variable is being initialized, then it
    6386              :            should never be a lose to promote the variable to be static.  */
    6387      1015925 :         if (valid_const_initializer
    6388       632116 :             && num_nonzero_elements > 1
    6389       464627 :             && TREE_READONLY (object)
    6390         8298 :             && VAR_P (object)
    6391         7921 :             && !DECL_REGISTER (object)
    6392         7914 :             && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)
    6393         5763 :                 || DECL_MERGEABLE (object))
    6394              :             /* For ctors that have many repeated nonzero elements
    6395              :                represented through RANGE_EXPRs, prefer initializing
    6396              :                those through runtime loops over copies of large amounts
    6397              :                of data from readonly data section.  */
    6398      1015925 :             && (num_unique_nonzero_elements
    6399         4566 :                 > num_nonzero_elements / unique_nonzero_ratio
    6400            0 :                 || ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
    6401              :                     <= (unsigned HOST_WIDE_INT) min_unique_size)))
    6402              :           {
    6403         4566 :             if (notify_temp_creation)
    6404              :               return GS_ERROR;
    6405              : 
    6406         4566 :             DECL_INITIAL (object) = ctor;
    6407         4566 :             TREE_STATIC (object) = 1;
    6408         4566 :             if (!DECL_NAME (object) || DECL_NAMELESS (object))
    6409         3600 :               DECL_NAME (object) = create_tmp_var_name ("C");
    6410         4566 :             walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
    6411              : 
    6412              :             /* ??? C++ doesn't automatically append a .<number> to the
    6413              :                assembler name, and even when it does, it looks at FE private
    6414              :                data structures to figure out what that number should be,
    6415              :                which are not set for this variable.  I suppose this is
    6416              :                important for local statics for inline functions, which aren't
    6417              :                "local" in the object file sense.  So in order to get a unique
    6418              :                TU-local symbol, we must invoke the lhd version now.  */
    6419         4566 :             lhd_set_decl_assembler_name (object);
    6420              : 
    6421         4566 :             *expr_p = NULL_TREE;
    6422         4566 :             break;
    6423              :           }
    6424              : 
    6425              :         /* The var will be initialized and so appear on lhs of
    6426              :            assignment, it can't be TREE_READONLY anymore.  */
    6427      1011359 :         if (VAR_P (object) && !notify_temp_creation)
    6428       792061 :           TREE_READONLY (object) = 0;
    6429              : 
    6430              :         /* If there are "lots" of initialized elements, even discounting
    6431              :            those that are not address constants (and thus *must* be
    6432              :            computed at runtime), then partition the constructor into
    6433              :            constant and non-constant parts.  Block copy the constant
    6434              :            parts in, then generate code for the non-constant parts.  */
    6435              :         /* TODO.  There's code in cp/typeck.cc to do this.  */
    6436              : 
    6437      1011359 :         if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
    6438              :           /* store_constructor will ignore the clearing of variable-sized
    6439              :              objects.  Initializers for such objects must explicitly set
    6440              :              every field that needs to be set.  */
    6441              :           cleared = false;
    6442      1011339 :         else if (complete_p.sparse)
    6443              :           /* If the constructor isn't complete, clear the whole object
    6444              :              beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
    6445              : 
    6446              :              ??? This ought not to be needed.  For any element not present
    6447              :              in the initializer, we should simply set them to zero.  Except
    6448              :              we'd need to *find* the elements that are not present, and that
    6449              :              requires trickery to avoid quadratic compile-time behavior in
    6450              :              large cases or excessive memory use in small cases.  */
    6451       200596 :           cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
    6452       810743 :         else if (num_ctor_elements - num_nonzero_elements
    6453       810743 :                  > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
    6454       810743 :                  && num_nonzero_elements < num_ctor_elements / 4)
    6455              :           /* If there are "lots" of zeros, it's more efficient to clear
    6456              :              the memory and then set the nonzero elements.  */
    6457              :           cleared = true;
    6458       809703 :         else if (ensure_single_access && num_nonzero_elements == 0)
    6459              :           /* If a single access to the target must be ensured and all elements
    6460              :              are zero, then it's optimal to clear whatever their number.  */
    6461              :           cleared = true;
    6462              :         /* If the object is small enough to go in registers, and it's
    6463              :            not required to be constructed in memory, clear it first.
    6464              :            That will avoid wasting cycles preserving any padding bits
    6465              :            that might be there, and if there aren't any, the compiler
    6466              :            is smart enough to optimize the clearing out.  */
    6467       809694 :         else if ((complete_p.sparse || complete_p.padded_union
    6468       809314 :                   || complete_p.padded_non_union)
    6469        15263 :                  && !TREE_ADDRESSABLE (ctor)
    6470        15249 :                  && !TREE_THIS_VOLATILE (object)
    6471        15246 :                  && (TYPE_MODE (type) != BLKmode || TYPE_NO_FORCE_BLK (type))
    6472       817015 :                  && optimize)
    6473              :           cleared = true;
    6474              :         else
    6475              :           cleared = false;
    6476              : 
    6477              :         /* If there are "lots" of initialized elements, and all of them
    6478              :            are valid address constants, then the entire initializer can
    6479              :            be dropped to memory, and then memcpy'd out.  Don't do this
    6480              :            for sparse arrays, though, as it's more efficient to follow
    6481              :            the standard CONSTRUCTOR behavior of memset followed by
    6482              :            individual element initialization.  Also don't do this for small
    6483              :            all-zero initializers (which aren't big enough to merit
    6484              :            clearing), and don't try to make bitwise copies of
    6485              :            TREE_ADDRESSABLE types.  */
    6486      1011359 :         if (valid_const_initializer
    6487       627550 :             && !complete_p.sparse
    6488       442252 :             && !(cleared || num_nonzero_elements == 0)
    6489       345362 :             && !TREE_ADDRESSABLE (type))
    6490              :           {
    6491       343474 :             HOST_WIDE_INT size = int_size_in_bytes (type);
    6492       343474 :             unsigned int align;
    6493              : 
    6494              :             /* ??? We can still get unbounded array types, at least
    6495              :                from the C++ front end.  This seems wrong, but attempt
    6496              :                to work around it for now.  */
    6497       343474 :             if (size < 0)
    6498              :               {
    6499            9 :                 size = int_size_in_bytes (TREE_TYPE (object));
    6500            9 :                 if (size >= 0)
    6501            0 :                   TREE_TYPE (ctor) = type = TREE_TYPE (object);
    6502              :               }
    6503              : 
    6504              :             /* Find the maximum alignment we can assume for the object.  */
    6505              :             /* ??? Make use of DECL_OFFSET_ALIGN.  */
    6506       343474 :             if (DECL_P (object))
    6507       329114 :               align = DECL_ALIGN (object);
    6508              :             else
    6509        14360 :               align = TYPE_ALIGN (type);
    6510              : 
    6511              :             /* Do a block move either if the size is so small as to make
    6512              :                each individual move a sub-unit move on average, or if it
    6513              :                is so large as to make individual moves inefficient.  */
    6514       343474 :             if (size > 0
    6515       343465 :                 && num_nonzero_elements > 1
    6516              :                 /* For ctors that have many repeated nonzero elements
    6517              :                    represented through RANGE_EXPRs, prefer initializing
    6518              :                    those through runtime loops over copies of large amounts
    6519              :                    of data from readonly data section.  */
    6520       304074 :                 && (num_unique_nonzero_elements
    6521       304074 :                     > num_nonzero_elements / unique_nonzero_ratio
    6522           56 :                     || size <= min_unique_size)
    6523       647492 :                 && (size < num_nonzero_elements
    6524       303912 :                     || !can_move_by_pieces (size, align)))
    6525              :               {
    6526         2750 :                 if (notify_temp_creation)
    6527              :                   return GS_ERROR;
    6528              : 
    6529         1377 :                 walk_tree (&ctor, force_labels_r, NULL, NULL);
    6530         1377 :                 ctor = tree_output_constant_def (ctor);
    6531         1377 :                 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
    6532            0 :                   ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
    6533         1377 :                 TREE_OPERAND (*expr_p, 1) = ctor;
    6534              : 
    6535              :                 /* This is no longer an assignment of a CONSTRUCTOR, but
    6536              :                    we still may have processing to do on the LHS.  So
    6537              :                    pretend we didn't do anything here to let that happen.  */
    6538         1377 :                 return GS_UNHANDLED;
    6539              :               }
    6540              :           }
    6541              : 
    6542      1003813 :         if (!cleared)
    6543              :           {
    6544       800417 :             if (complete_p.padded_non_union
    6545         8158 :                 && warn_zero_init_padding_bits >= ZERO_INIT_PADDING_BITS_ALL)
    6546           12 :                 warning (OPT_Wzero_init_padding_bits_,
    6547              :                          "padding might not be initialized to zero; "
    6548              :                          "if code relies on it being zero, consider "
    6549              :                          "using %<-fzero-init-padding-bits=all%>");
    6550       800405 :             else if (complete_p.padded_union
    6551          124 :                      && warn_zero_init_padding_bits
    6552              :                         >= ZERO_INIT_PADDING_BITS_UNIONS)
    6553           14 :                 warning (OPT_Wzero_init_padding_bits_,
    6554              :                          "padding might not be initialized to zero; "
    6555              :                          "if code relies on it being zero, consider "
    6556              :                          "using %<-fzero-init-padding-bits=unions%> "
    6557              :                          "or %<-fzero-init-padding-bits=all%>");
    6558              :           }
    6559              : 
    6560              :         /* If a single access to the target must be ensured and there are
    6561              :            nonzero elements or the zero elements are not assigned en masse,
    6562              :            initialize the target from a temporary.  */
    6563      1008609 :         if (ensure_single_access && (num_nonzero_elements > 0 || !cleared))
    6564              :           {
    6565          167 :             if (notify_temp_creation)
    6566              :               return GS_ERROR;
    6567              : 
    6568          161 :             tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
    6569          161 :             TREE_OPERAND (*expr_p, 0) = temp;
    6570          161 :             *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
    6571              :                               *expr_p,
    6572              :                               build2 (MODIFY_EXPR, void_type_node,
    6573              :                                       object, temp));
    6574          161 :             return GS_OK;
    6575              :           }
    6576              : 
    6577      1008442 :         if (notify_temp_creation)
    6578              :           return GS_OK;
    6579              : 
    6580              :         /* If there are nonzero elements and if needed, pre-evaluate to capture
    6581              :            elements overlapping with the lhs into temporaries.  We must do this
    6582              :            before clearing to fetch the values before they are zeroed-out.  */
    6583      1001201 :         if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
    6584              :           {
    6585       228155 :             preeval_data.lhs_base_decl = get_base_address (object);
    6586       228155 :             if (!DECL_P (preeval_data.lhs_base_decl))
    6587        41809 :               preeval_data.lhs_base_decl = NULL;
    6588       228155 :             preeval_data.lhs_alias_set = get_alias_set (object);
    6589              : 
    6590       228155 :             gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
    6591              :                                         pre_p, post_p, &preeval_data);
    6592              :           }
    6593              : 
    6594      1001201 :         bool ctor_has_side_effects_p
    6595      1001201 :           = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
    6596              : 
    6597      1001201 :         if (cleared)
    6598              :           {
    6599              :             /* Zap the CONSTRUCTOR element list, which simplifies this case.
    6600              :                Note that we still have to gimplify, in order to handle the
    6601              :                case of variable sized types.  Avoid shared tree structures.  */
    6602       207947 :             CONSTRUCTOR_ELTS (ctor) = NULL;
    6603       207947 :             TREE_SIDE_EFFECTS (ctor) = 0;
    6604       207947 :             object = unshare_expr (object);
    6605       207947 :             gimplify_stmt (expr_p, pre_p);
    6606              :           }
    6607              : 
    6608              :         /* If we have not block cleared the object, or if there are nonzero
    6609              :            elements in the constructor, or if the constructor has side effects,
    6610              :            add assignments to the individual scalar fields of the object.  */
    6611       207947 :         if (!cleared
    6612       207947 :             || num_nonzero_elements > 0
    6613        20647 :             || ctor_has_side_effects_p)
    6614       980950 :           gimplify_init_ctor_eval (object, elts, pre_p, cleared);
    6615              : 
    6616      1001201 :         *expr_p = NULL_TREE;
    6617              :       }
    6618      1001201 :       break;
    6619              : 
    6620            0 :     case COMPLEX_TYPE:
    6621            0 :       {
    6622            0 :         tree r, i;
    6623              : 
    6624            0 :         if (notify_temp_creation)
    6625              :           return GS_OK;
    6626              : 
    6627              :         /* Extract the real and imaginary parts out of the ctor.  */
    6628            0 :         gcc_assert (elts->length () == 2);
    6629            0 :         r = (*elts)[0].value;
    6630            0 :         i = (*elts)[1].value;
    6631            0 :         if (r == NULL || i == NULL)
    6632              :           {
    6633            0 :             tree zero = build_zero_cst (TREE_TYPE (type));
    6634            0 :             if (r == NULL)
    6635            0 :               r = zero;
    6636            0 :             if (i == NULL)
    6637            0 :               i = zero;
    6638              :           }
    6639              : 
    6640              :         /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
    6641              :            represent creation of a complex value.  */
    6642            0 :         if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
    6643              :           {
    6644            0 :             ctor = build_complex (type, r, i);
    6645            0 :             TREE_OPERAND (*expr_p, 1) = ctor;
    6646              :           }
    6647              :         else
    6648              :           {
    6649            0 :             ctor = build2 (COMPLEX_EXPR, type, r, i);
    6650            0 :             TREE_OPERAND (*expr_p, 1) = ctor;
    6651            0 :             ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
    6652              :                                  pre_p,
    6653              :                                  post_p,
    6654            0 :                                  rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
    6655              :                                  fb_rvalue);
    6656              :           }
    6657              :       }
    6658              :       break;
    6659              : 
    6660         8764 :     case VECTOR_TYPE:
    6661         8764 :       {
    6662         8764 :         unsigned HOST_WIDE_INT ix;
    6663         8764 :         constructor_elt *ce;
    6664              : 
    6665         8764 :         if (notify_temp_creation)
    6666      1372501 :           return GS_OK;
    6667              : 
    6668              :         /* Vector types use CONSTRUCTOR all the way through gimple
    6669              :            compilation as a general initializer.  */
    6670        76216 :         FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
    6671              :           {
    6672        67452 :             enum gimplify_status tret;
    6673        67452 :             tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
    6674              :                                   fb_rvalue);
    6675        67452 :             if (tret == GS_ERROR)
    6676              :               ret = GS_ERROR;
    6677        67451 :             else if (TREE_STATIC (ctor)
    6678        67484 :                      && !initializer_constant_valid_p (ce->value,
    6679           33 :                                                        TREE_TYPE (ce->value)))
    6680           33 :               TREE_STATIC (ctor) = 0;
    6681              :           }
    6682         8764 :         recompute_constructor_flags (ctor);
    6683              : 
    6684              :         /* Go ahead and simplify constant constructors to VECTOR_CST.  */
    6685         8764 :         if (TREE_CONSTANT (ctor))
    6686              :           {
    6687         2705 :             bool constant_p = true;
    6688              :             tree value;
    6689              : 
    6690              :             /* Even when ctor is constant, it might contain non-*_CST
    6691              :                elements, such as addresses or trapping values like
    6692              :                1.0/0.0 - 1.0/0.0.  Such expressions don't belong
    6693              :                in VECTOR_CST nodes.  */
    6694         2705 :             FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
    6695         2483 :               if (!CONSTANT_CLASS_P (value))
    6696              :                 {
    6697              :                   constant_p = false;
    6698              :                   break;
    6699              :                 }
    6700              : 
    6701          222 :             if (constant_p)
    6702              :               {
    6703          222 :                 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
    6704          222 :                 break;
    6705              :               }
    6706              :           }
    6707              : 
    6708         8542 :         if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
    6709          966 :           TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
    6710              :       }
    6711              :       break;
    6712              : 
    6713            0 :     default:
    6714              :       /* So how did we get a CONSTRUCTOR for a scalar type?  */
    6715            0 :       gcc_unreachable ();
    6716              :     }
    6717              : 
    6718      1362295 :   if (ret == GS_ERROR)
    6719              :     return GS_ERROR;
    6720              :   /* If we have gimplified both sides of the initializer but have
    6721              :      not emitted an assignment, do so now.   */
    6722      1362294 :   if (*expr_p
    6723              :       /* If the type is an empty type, we don't need to emit the
    6724              :          assignment. */
    6725      1362294 :       && !is_empty_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
    6726              :     {
    6727       352359 :       tree lhs = TREE_OPERAND (*expr_p, 0);
    6728       352359 :       tree rhs = TREE_OPERAND (*expr_p, 1);
    6729       352359 :       if (want_value && object == lhs)
    6730           12 :         lhs = unshare_expr (lhs);
    6731       352359 :       gassign *init = gimple_build_assign (lhs, rhs);
    6732       352359 :       gimplify_seq_add_stmt (pre_p, init);
    6733              :     }
    6734      1362294 :   if (want_value)
    6735              :     {
    6736              :       *expr_p = object;
    6737      1362294 :       ret = GS_OK;
    6738              :     }
    6739              :   else
    6740              :     {
    6741      1362275 :       *expr_p = NULL;
    6742      1362275 :       ret = GS_ALL_DONE;
    6743              :     }
    6744              : 
    6745              :   /* If the user requests to initialize automatic variables, we
    6746              :      should initialize paddings inside the variable.  Add a call to
    6747              :      __builtin_clear_pading (&object, 0, for_auto_init = true) to
    6748              :      initialize paddings of object always to zero regardless of
    6749              :      INIT_TYPE.  Note, we will not insert this call if the aggregate
    6750              :      variable has be completely cleared already or it's initialized
    6751              :      with an empty constructor.  We cannot insert this call if the
    6752              :      variable is a gimple register since __builtin_clear_padding will take
    6753              :      the address of the variable.  As a result, if a long double/_Complex long
    6754              :      double variable will be spilled into stack later, its padding cannot
    6755              :      be cleared with __builtin_clear_padding.  We should clear its padding
    6756              :      when it is spilled into memory.  */
    6757      1362294 :   if (is_init_expr
    6758       958087 :       && !is_gimple_reg (object)
    6759       952627 :       && clear_padding_type_may_have_padding_p (type)
    6760       906498 :       && ((AGGREGATE_TYPE_P (type) && !cleared && !is_empty_ctor)
    6761       230270 :           || !AGGREGATE_TYPE_P (type))
    6762       676228 :       && var_needs_auto_init_p (object)
    6763      1398970 :       && flag_auto_var_init != AUTO_INIT_CXX26)
    6764           41 :     gimple_add_padding_init_for_auto_var (object, false, pre_p);
    6765              : 
    6766              :   return ret;
    6767              : }
    6768              : 
    6769              : /* Given a pointer value OP0, return a simplified version of an
    6770              :    indirection through OP0, or NULL_TREE if no simplification is
    6771              :    possible.  This may only be applied to a rhs of an expression.
    6772              :    Note that the resulting type may be different from the type pointed
    6773              :    to in the sense that it is still compatible from the langhooks
    6774              :    point of view. */
    6775              : 
    6776              : static tree
    6777       534223 : gimple_fold_indirect_ref_rhs (tree t)
    6778              : {
    6779            0 :   return gimple_fold_indirect_ref (t);
    6780              : }
    6781              : 
    6782              : /* Subroutine of gimplify_modify_expr to do simplifications of
    6783              :    MODIFY_EXPRs based on the code of the RHS.  We loop for as long as
    6784              :    something changes.  */
    6785              : 
    6786              : static enum gimplify_status
    6787    100252660 : gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
    6788              :                           gimple_seq *pre_p, gimple_seq *post_p,
    6789              :                           bool want_value)
    6790              : {
    6791    100252660 :   enum gimplify_status ret = GS_UNHANDLED;
    6792    100906682 :   bool changed;
    6793              : 
    6794     99388362 :   do
    6795              :     {
    6796    100906682 :       changed = false;
    6797    100906682 :       switch (TREE_CODE (*from_p))
    6798              :         {
    6799     12146703 :         case VAR_DECL:
    6800              :           /* If we're assigning from a read-only variable initialized with
    6801              :              a constructor and not volatile, do the direct assignment from
    6802              :              the constructor, but only if the target is not volatile either
    6803              :              since this latter assignment might end up being done on a per
    6804              :              field basis.  However, if the target is volatile and the type
    6805              :              is aggregate and non-addressable, gimplify_init_constructor
    6806              :              knows that it needs to ensure a single access to the target
    6807              :              and it will return GS_OK only in this case.  */
    6808     12146703 :           if (TREE_READONLY (*from_p)
    6809        95323 :               && DECL_INITIAL (*from_p)
    6810        61601 :               && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR
    6811         8668 :               && !TREE_THIS_VOLATILE (*from_p)
    6812     12155371 :               && (!TREE_THIS_VOLATILE (*to_p)
    6813            6 :                   || (AGGREGATE_TYPE_P (TREE_TYPE (*to_p))
    6814            6 :                       && !TREE_ADDRESSABLE (TREE_TYPE (*to_p)))))
    6815              :             {
    6816         8668 :               tree old_from = *from_p;
    6817         8668 :               enum gimplify_status subret;
    6818              : 
    6819              :               /* Move the constructor into the RHS.  */
    6820         8668 :               *from_p = unshare_expr (DECL_INITIAL (*from_p));
    6821              : 
    6822              :               /* Let's see if gimplify_init_constructor will need to put
    6823              :                  it in memory.  */
    6824         8668 :               subret = gimplify_init_constructor (expr_p, NULL, NULL,
    6825              :                                                   false, true);
    6826         8668 :               if (subret == GS_ERROR)
    6827              :                 {
    6828              :                   /* If so, revert the change.  */
    6829         1379 :                   *from_p = old_from;
    6830              :                 }
    6831              :               else
    6832              :                 {
    6833              :                   ret = GS_OK;
    6834              :                   changed = true;
    6835              :                 }
    6836              :             }
    6837              :           break;
    6838       542465 :         case INDIRECT_REF:
    6839       542465 :           if (!TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
    6840              :             /* If we have code like
    6841              : 
    6842              :              *(const A*)(A*)&x
    6843              : 
    6844              :              where the type of "x" is a (possibly cv-qualified variant
    6845              :              of "A"), treat the entire expression as identical to "x".
    6846              :              This kind of code arises in C++ when an object is bound
    6847              :              to a const reference, and if "x" is a TARGET_EXPR we want
    6848              :              to take advantage of the optimization below.  But not if
    6849              :              the type is TREE_ADDRESSABLE; then C++17 says that the
    6850              :              TARGET_EXPR needs to be a temporary.  */
    6851      1068446 :             if (tree t
    6852       534223 :                 = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0)))
    6853              :               {
    6854       130343 :                 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
    6855       130343 :                 if (TREE_THIS_VOLATILE (t) != volatile_p)
    6856              :                   {
    6857            5 :                     if (DECL_P (t))
    6858            0 :                       t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
    6859              :                                                     build_fold_addr_expr (t));
    6860            5 :                     if (REFERENCE_CLASS_P (t))
    6861            5 :                       TREE_THIS_VOLATILE (t) = volatile_p;
    6862              :                   }
    6863       130343 :                 *from_p = t;
    6864       130343 :                 ret = GS_OK;
    6865       130343 :                 changed = true;
    6866              :               }
    6867              :           break;
    6868              : 
    6869       199204 :         case TARGET_EXPR:
    6870       199204 :           {
    6871              :             /* If we are initializing something from a TARGET_EXPR, strip the
    6872              :                TARGET_EXPR and initialize it directly, if possible.  This can't
    6873              :                be done if the initializer is void, since that implies that the
    6874              :                temporary is set in some non-trivial way.
    6875              : 
    6876              :                ??? What about code that pulls out the temp and uses it
    6877              :                elsewhere? I think that such code never uses the TARGET_EXPR as
    6878              :                an initializer.  If I'm wrong, we'll die because the temp won't
    6879              :                have any RTL.  In that case, I guess we'll need to replace
    6880              :                references somehow.  */
    6881       199204 :             tree init = TARGET_EXPR_INITIAL (*from_p);
    6882              : 
    6883       199204 :             if (init
    6884       198487 :                 && (TREE_CODE (*expr_p) != MODIFY_EXPR
    6885        51715 :                     || !TARGET_EXPR_NO_ELIDE (*from_p))
    6886       397473 :                 && !VOID_TYPE_P (TREE_TYPE (init)))
    6887              :               {
    6888       182598 :                 *from_p = init;
    6889       182598 :                 ret = GS_OK;
    6890       182598 :                 changed = true;
    6891              :               }
    6892              :           }
    6893              :           break;
    6894              : 
    6895       333426 :         case COMPOUND_EXPR:
    6896              :           /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
    6897              :              caught.  */
    6898       333426 :           gimplify_compound_expr (from_p, pre_p, true);
    6899       333426 :           ret = GS_OK;
    6900       333426 :           changed = true;
    6901       333426 :           break;
    6902              : 
    6903      1384784 :         case CONSTRUCTOR:
    6904              :           /* If we already made some changes, let the front end have a
    6905              :              crack at this before we break it down.  */
    6906      1384784 :           if (ret != GS_UNHANDLED)
    6907              :             break;
    6908              : 
    6909              :           /* If we're initializing from a CONSTRUCTOR, break this into
    6910              :              individual MODIFY_EXPRs.  */
    6911      1363833 :           ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
    6912              :                                            false);
    6913      1363833 :           return ret;
    6914              : 
    6915       229779 :         case COND_EXPR:
    6916              :           /* If we're assigning to a non-register type, push the assignment
    6917              :              down into the branches.  This is mandatory for ADDRESSABLE types,
    6918              :              since we cannot generate temporaries for such, but it saves a
    6919              :              copy in other cases as well.
    6920              :              Also avoid an extra temporary and copy when assigning to
    6921              :              a register.  */
    6922       229779 :           if (!is_gimple_reg_type (TREE_TYPE (*from_p))
    6923       229779 :               || (is_gimple_reg (*to_p) && !gimplify_ctxp->allow_rhs_cond_expr))
    6924              :             {
    6925              :               /* This code should mirror the code in gimplify_cond_expr. */
    6926       147441 :               enum tree_code code = TREE_CODE (*expr_p);
    6927       147441 :               tree cond = *from_p;
    6928       147441 :               tree result = *to_p;
    6929              : 
    6930       147441 :               ret = gimplify_expr (&result, pre_p, post_p,
    6931              :                                    is_gimple_lvalue, fb_lvalue);
    6932       147441 :               if (ret != GS_ERROR)
    6933       147441 :                 ret = GS_OK;
    6934              : 
    6935              :               /* If we are going to write RESULT more than once, clear
    6936              :                  TREE_READONLY flag, otherwise we might incorrectly promote
    6937              :                  the variable to static const and initialize it at compile
    6938              :                  time in one of the branches.  */
    6939       147441 :               if (VAR_P (result)
    6940       146409 :                   && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
    6941       284777 :                   && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
    6942       137215 :                 TREE_READONLY (result) = 0;
    6943       147441 :               if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
    6944       138232 :                 TREE_OPERAND (cond, 1)
    6945       276464 :                   = build2 (code, void_type_node, result,
    6946       138232 :                             TREE_OPERAND (cond, 1));
    6947       147441 :               if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
    6948       147320 :                 TREE_OPERAND (cond, 2)
    6949       294640 :                   = build2 (code, void_type_node, unshare_expr (result),
    6950       147320 :                             TREE_OPERAND (cond, 2));
    6951              : 
    6952       147441 :               TREE_TYPE (cond) = void_type_node;
    6953       147441 :               recalculate_side_effects (cond);
    6954              : 
    6955       147441 :               if (want_value)
    6956              :                 {
    6957           68 :                   gimplify_and_add (cond, pre_p);
    6958           68 :                   *expr_p = unshare_expr (result);
    6959              :                 }
    6960              :               else
    6961              :                 *expr_p = cond;
    6962       147441 :               return ret;
    6963              :             }
    6964              :           break;
    6965              : 
    6966     10315120 :         case CALL_EXPR:
    6967              :           /* For calls that return in memory, give *to_p as the CALL_EXPR's
    6968              :              return slot so that we don't generate a temporary.  */
    6969     10315120 :           if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
    6970     10315120 :               && aggregate_value_p (*from_p, *from_p))
    6971              :             {
    6972       254831 :               bool use_target;
    6973              : 
    6974       254831 :               if (!(rhs_predicate_for (*to_p))(*from_p))
    6975              :                 /* If we need a temporary, *to_p isn't accurate.  */
    6976              :                 use_target = false;
    6977              :               /* It's OK to use the return slot directly unless it's an NRV. */
    6978       254010 :               else if (TREE_CODE (*to_p) == RESULT_DECL
    6979         6794 :                        && DECL_NAME (*to_p) == NULL_TREE
    6980       260659 :                        && needs_to_live_in_memory (*to_p))
    6981              :                 use_target = true;
    6982       247361 :               else if (is_gimple_reg_type (TREE_TYPE (*to_p))
    6983       247361 :                        || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
    6984              :                 /* Don't force regs into memory.  */
    6985              :                 use_target = false;
    6986       232137 :               else if (TREE_CODE (*expr_p) == INIT_EXPR)
    6987              :                 /* It's OK to use the target directly if it's being
    6988              :                    initialized. */
    6989              :                 use_target = true;
    6990        14883 :               else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
    6991              :                        != INTEGER_CST)
    6992              :                 /* Always use the target and thus RSO for variable-sized types.
    6993              :                    GIMPLE cannot deal with a variable-sized assignment
    6994              :                    embedded in a call statement.  */
    6995              :                 use_target = true;
    6996        14855 :               else if (TREE_CODE (*to_p) != SSA_NAME
    6997        14855 :                       && (!is_gimple_variable (*to_p)
    6998        11464 :                           || needs_to_live_in_memory (*to_p)))
    6999              :                 /* Don't use the original target if it's already addressable;
    7000              :                    if its address escapes, and the called function uses the
    7001              :                    NRV optimization, a conforming program could see *to_p
    7002              :                    change before the called function returns; see c++/19317.
    7003              :                    When optimizing, the return_slot pass marks more functions
    7004              :                    as safe after we have escape info.  */
    7005              :                 use_target = false;
    7006              :               else
    7007              :                 use_target = true;
    7008              : 
    7009              :               if (use_target)
    7010              :                 {
    7011       226157 :                   CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
    7012       226157 :                   mark_addressable (*to_p);
    7013              :                 }
    7014              :             }
    7015              :           break;
    7016              : 
    7017         1567 :         case WITH_SIZE_EXPR:
    7018              :           /* Likewise for calls that return an aggregate of non-constant size,
    7019              :              since we would not be able to generate a temporary at all.  */
    7020         1567 :           if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
    7021              :             {
    7022          353 :               *from_p = TREE_OPERAND (*from_p, 0);
    7023              :               /* We don't change ret in this case because the
    7024              :                  WITH_SIZE_EXPR might have been added in
    7025              :                  gimplify_modify_expr, so returning GS_OK would lead to an
    7026              :                  infinite loop.  */
    7027          353 :               changed = true;
    7028              :             }
    7029              :           break;
    7030              : 
    7031              :           /* If we're initializing from a container, push the initialization
    7032              :              inside it.  */
    7033         2048 :         case CLEANUP_POINT_EXPR:
    7034         2048 :         case BIND_EXPR:
    7035         2048 :         case STATEMENT_LIST:
    7036         2048 :           {
    7037         2048 :             tree wrap = *from_p;
    7038         2048 :             tree t;
    7039              : 
    7040         2048 :             ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
    7041              :                                  fb_lvalue);
    7042         2048 :             if (ret != GS_ERROR)
    7043         2048 :               ret = GS_OK;
    7044              : 
    7045         2048 :             t = voidify_wrapper_expr (wrap, *expr_p);
    7046         2048 :             gcc_assert (t == *expr_p);
    7047              : 
    7048         2048 :             if (want_value)
    7049              :               {
    7050            6 :                 gimplify_and_add (wrap, pre_p);
    7051            6 :                 *expr_p = unshare_expr (*to_p);
    7052              :               }
    7053              :             else
    7054         2042 :               *expr_p = wrap;
    7055              :             return GS_OK;
    7056              :           }
    7057              : 
    7058      9989788 :         case NOP_EXPR:
    7059              :           /* Pull out compound literal expressions from a NOP_EXPR.
    7060              :              Those are created in the C FE to drop qualifiers during
    7061              :              lvalue conversion.  */
    7062      9989788 :           if ((TREE_CODE (TREE_OPERAND (*from_p, 0)) == COMPOUND_LITERAL_EXPR)
    7063      9989788 :               && tree_ssa_useless_type_conversion (*from_p))
    7064              :             {
    7065           13 :               *from_p = TREE_OPERAND (*from_p, 0);
    7066           13 :               ret = GS_OK;
    7067           13 :               changed = true;
    7068              :             }
    7069              :           break;
    7070              : 
    7071         4998 :         case COMPOUND_LITERAL_EXPR:
    7072         4998 :           {
    7073         4998 :             tree complit = TREE_OPERAND (*expr_p, 1);
    7074         4998 :             tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
    7075         4998 :             tree decl = DECL_EXPR_DECL (decl_s);
    7076         4998 :             tree init = DECL_INITIAL (decl);
    7077              : 
    7078              :             /* struct T x = (struct T) { 0, 1, 2 } can be optimized
    7079              :                into struct T x = { 0, 1, 2 } if the address of the
    7080              :                compound literal has never been taken.  */
    7081         4998 :             if (!TREE_ADDRESSABLE (complit)
    7082         4998 :                 && !TREE_ADDRESSABLE (decl)
    7083         4998 :                 && init)
    7084              :               {
    7085         4998 :                 *expr_p = copy_node (*expr_p);
    7086         4998 :                 TREE_OPERAND (*expr_p, 1) = init;
    7087         4998 :                 return GS_OK;
    7088              :               }
    7089              :           }
    7090              : 
    7091              :         default:
    7092              :           break;
    7093              :         }
    7094              :     }
    7095              :   while (changed);
    7096              : 
    7097              :   return ret;
    7098              : }
    7099              : 
    7100              : 
    7101              : /* Return true if T looks like a valid GIMPLE statement.  */
    7102              : 
    7103              : static bool
    7104     19702785 : is_gimple_stmt (tree t)
    7105              : {
    7106     19702785 :   const enum tree_code code = TREE_CODE (t);
    7107              : 
    7108     19702785 :   switch (code)
    7109              :     {
    7110      1761845 :     case NOP_EXPR:
    7111              :       /* The only valid NOP_EXPR is the empty statement.  */
    7112      1761845 :       return IS_EMPTY_STMT (t);
    7113              : 
    7114            0 :     case BIND_EXPR:
    7115            0 :     case COND_EXPR:
    7116              :       /* These are only valid if they're void.  */
    7117            0 :       return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
    7118              : 
    7119              :     case SWITCH_EXPR:
    7120              :     case GOTO_EXPR:
    7121              :     case RETURN_EXPR:
    7122              :     case LABEL_EXPR:
    7123              :     case CASE_LABEL_EXPR:
    7124              :     case TRY_CATCH_EXPR:
    7125              :     case TRY_FINALLY_EXPR:
    7126              :     case EH_FILTER_EXPR:
    7127              :     case CATCH_EXPR:
    7128              :     case ASM_EXPR:
    7129              :     case STATEMENT_LIST:
    7130              :     case OACC_PARALLEL:
    7131              :     case OACC_KERNELS:
    7132              :     case OACC_SERIAL:
    7133              :     case OACC_DATA:
    7134              :     case OACC_HOST_DATA:
    7135              :     case OACC_DECLARE:
    7136              :     case OACC_UPDATE:
    7137              :     case OACC_ENTER_DATA:
    7138              :     case OACC_EXIT_DATA:
    7139              :     case OACC_CACHE:
    7140              :     case OMP_PARALLEL:
    7141              :     case OMP_FOR:
    7142              :     case OMP_SIMD:
    7143              :     case OMP_DISTRIBUTE:
    7144              :     case OMP_LOOP:
    7145              :     case OMP_TILE:
    7146              :     case OMP_UNROLL:
    7147              :     case OACC_LOOP:
    7148              :     case OMP_SCAN:
    7149              :     case OMP_SCOPE:
    7150              :     case OMP_DISPATCH:
    7151              :     case OMP_SECTIONS:
    7152              :     case OMP_SECTION:
    7153              :     case OMP_STRUCTURED_BLOCK:
    7154              :     case OMP_SINGLE:
    7155              :     case OMP_MASTER:
    7156              :     case OMP_MASKED:
    7157              :     case OMP_TASKGROUP:
    7158              :     case OMP_ORDERED:
    7159              :     case OMP_CRITICAL:
    7160              :     case OMP_METADIRECTIVE:
    7161              :     case OMP_TASK:
    7162              :     case OMP_TARGET:
    7163              :     case OMP_TARGET_DATA:
    7164              :     case OMP_TARGET_UPDATE:
    7165              :     case OMP_TARGET_ENTER_DATA:
    7166              :     case OMP_TARGET_EXIT_DATA:
    7167              :     case OMP_TASKLOOP:
    7168              :     case OMP_TEAMS:
    7169              :       /* These are always void.  */
    7170              :       return true;
    7171              : 
    7172              :     case CALL_EXPR:
    7173              :     case MODIFY_EXPR:
    7174              :     case PREDICT_EXPR:
    7175              :       /* These are valid regardless of their type.  */
    7176              :       return true;
    7177              : 
    7178              :     default:
    7179              :       return false;
    7180              :     }
    7181              : }
    7182              : 
    7183              : 
    7184              : /* Promote partial stores to COMPLEX variables to total stores.  *EXPR_P is
    7185              :    a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a gimple register.
    7186              : 
    7187              :    IMPORTANT NOTE: This promotion is performed by introducing a load of the
    7188              :    other, unmodified part of the complex object just before the total store.
    7189              :    As a consequence, if the object is still uninitialized, an undefined value
    7190              :    will be loaded into a register, which may result in a spurious exception
    7191              :    if the register is floating-point and the value happens to be a signaling
    7192              :    NaN for example.  Then the fully-fledged complex operations lowering pass
    7193              :    followed by a DCE pass are necessary in order to fix things up.  */
    7194              : 
    7195              : static enum gimplify_status
    7196         1960 : gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
    7197              :                                    bool want_value)
    7198              : {
    7199         1960 :   enum tree_code code, ocode;
    7200         1960 :   tree lhs, rhs, new_rhs, other, realpart, imagpart;
    7201              : 
    7202         1960 :   lhs = TREE_OPERAND (*expr_p, 0);
    7203         1960 :   rhs = TREE_OPERAND (*expr_p, 1);
    7204         1960 :   code = TREE_CODE (lhs);
    7205         1960 :   lhs = TREE_OPERAND (lhs, 0);
    7206              : 
    7207         1960 :   ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
    7208         1960 :   other = build1 (ocode, TREE_TYPE (rhs), lhs);
    7209         1960 :   suppress_warning (other);
    7210         1960 :   other = get_formal_tmp_var (other, pre_p);
    7211              : 
    7212         1960 :   realpart = code == REALPART_EXPR ? rhs : other;
    7213          969 :   imagpart = code == REALPART_EXPR ? other : rhs;
    7214              : 
    7215         1960 :   if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
    7216            0 :     new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
    7217              :   else
    7218         1960 :     new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
    7219              : 
    7220         1960 :   gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
    7221         1960 :   *expr_p = (want_value) ? rhs : NULL_TREE;
    7222              : 
    7223         1960 :   return GS_ALL_DONE;
    7224              : }
    7225              : 
    7226              : /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
    7227              : 
    7228              :       modify_expr
    7229              :               : varname '=' rhs
    7230              :               | '*' ID '=' rhs
    7231              : 
    7232              :     PRE_P points to the list where side effects that must happen before
    7233              :         *EXPR_P should be stored.
    7234              : 
    7235              :     POST_P points to the list where side effects that must happen after
    7236              :         *EXPR_P should be stored.
    7237              : 
    7238              :     WANT_VALUE is nonzero iff we want to use the value of this expression
    7239              :         in another expression.  */
    7240              : 
    7241              : static enum gimplify_status
    7242     51789572 : gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
    7243              :                       bool want_value)
    7244              : {
    7245     51789572 :   tree *from_p = &TREE_OPERAND (*expr_p, 1);
    7246     51789572 :   tree *to_p = &TREE_OPERAND (*expr_p, 0);
    7247     51789572 :   enum gimplify_status ret = GS_UNHANDLED;
    7248     51789572 :   gimple *assign;
    7249     51789572 :   location_t loc = EXPR_LOCATION (*expr_p);
    7250     51789572 :   gimple_stmt_iterator gsi;
    7251              : 
    7252     51789572 :   if (error_operand_p (*from_p) || error_operand_p (*to_p))
    7253              :     return GS_ERROR;
    7254              : 
    7255     51789544 :   gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
    7256              :               || TREE_CODE (*expr_p) == INIT_EXPR);
    7257              : 
    7258              :   /* Trying to simplify a clobber using normal logic doesn't work,
    7259              :      so handle it here.  */
    7260     51789544 :   if (TREE_CLOBBER_P (*from_p))
    7261              :     {
    7262       613068 :       ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
    7263       613068 :       if (ret == GS_ERROR)
    7264              :         return ret;
    7265       613068 :       gcc_assert (!want_value);
    7266       613068 :       if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
    7267              :         {
    7268          208 :           tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
    7269              :                                                pre_p, post_p);
    7270          208 :           *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
    7271              :         }
    7272       613068 :       gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
    7273       613068 :       *expr_p = NULL;
    7274       613068 :       return GS_ALL_DONE;
    7275              :     }
    7276              : 
    7277              :   /* Convert initialization from an empty variable-size CONSTRUCTOR to
    7278              :      memset.  */
    7279     51176476 :   if (TREE_TYPE (*from_p) != error_mark_node
    7280     51176476 :       && TYPE_SIZE_UNIT (TREE_TYPE (*from_p))
    7281     51176476 :       && !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (*from_p)))
    7282         1600 :       && TREE_CODE (*from_p) == CONSTRUCTOR
    7283     51176580 :       && CONSTRUCTOR_NELTS (*from_p) == 0)
    7284              :     {
    7285           84 :       maybe_with_size_expr (from_p);
    7286           84 :       gcc_assert (TREE_CODE (*from_p) == WITH_SIZE_EXPR);
    7287           84 :       return gimplify_modify_expr_to_memset (expr_p,
    7288           84 :                                              TREE_OPERAND (*from_p, 1),
    7289           84 :                                              want_value, pre_p);
    7290              :     }
    7291              : 
    7292              :   /* Insert pointer conversions required by the middle-end that are not
    7293              :      required by the frontend.  This fixes middle-end type checking for
    7294              :      for example gcc.dg/redecl-6.c.  */
    7295     51176392 :   if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
    7296              :     {
    7297     13798021 :       STRIP_USELESS_TYPE_CONVERSION (*from_p);
    7298     13798021 :       if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
    7299          754 :         *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
    7300              :     }
    7301              : 
    7302              :   /* See if any simplifications can be done based on what the RHS is.  */
    7303     51176392 :   ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
    7304              :                                   want_value);
    7305     51176392 :   if (ret != GS_UNHANDLED)
    7306              :     return ret;
    7307              : 
    7308              :   /* For empty types only gimplify the left hand side and right hand
    7309              :      side as statements and throw away the assignment.  Do this after
    7310              :      gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
    7311              :      types properly.  */
    7312     49078556 :   if (is_empty_type (TREE_TYPE (*from_p))
    7313        13315 :       && !want_value
    7314              :       /* Don't do this for calls that return addressable types, expand_call
    7315              :          relies on those having a lhs.  */
    7316     49091864 :       && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
    7317        11245 :            && TREE_CODE (*from_p) == CALL_EXPR))
    7318              :     {
    7319         2063 :       suppress_warning (*from_p, OPT_Wunused_result);
    7320         2063 :       gimplify_stmt (from_p, pre_p);
    7321         2063 :       gimplify_stmt (to_p, pre_p);
    7322         2063 :       *expr_p = NULL_TREE;
    7323         2063 :       return GS_ALL_DONE;
    7324              :     }
    7325              : 
    7326              :   /* If the value being copied is of variable width, compute the length
    7327              :      of the copy into a WITH_SIZE_EXPR.   Note that we need to do this
    7328              :      before gimplifying any of the operands so that we can resolve any
    7329              :      PLACEHOLDER_EXPRs in the size.  Also note that the RTL expander uses
    7330              :      the size of the expression to be copied, not of the destination, so
    7331              :      that is what we must do here.  */
    7332     49076493 :   maybe_with_size_expr (from_p);
    7333              : 
    7334              :   /* As a special case, we have to temporarily allow for assignments
    7335              :      with a CALL_EXPR on the RHS.  Since in GIMPLE a function call is
    7336              :      a toplevel statement, when gimplifying the GENERIC expression
    7337              :      MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
    7338              :      GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
    7339              : 
    7340              :      Instead, we need to create the tuple GIMPLE_CALL <a, foo>.  To
    7341              :      prevent gimplify_expr from trying to create a new temporary for
    7342              :      foo's LHS, we tell it that it should only gimplify until it
    7343              :      reaches the CALL_EXPR.  On return from gimplify_expr, the newly
    7344              :      created GIMPLE_CALL <foo> will be the last statement in *PRE_P
    7345              :      and all we need to do here is set 'a' to be its LHS.  */
    7346              : 
    7347              :   /* Gimplify the RHS first for C++17 and bug 71104.  */
    7348     49076493 :   gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
    7349     49076493 :   ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
    7350     49076493 :   if (ret == GS_ERROR)
    7351              :     return ret;
    7352              : 
    7353              :   /* Then gimplify the LHS.  */
    7354              :   /* If we gimplified the RHS to a CALL_EXPR and that call may return
    7355              :      twice we have to make sure to gimplify into non-SSA as otherwise
    7356              :      the abnormal edge added later will make those defs not dominate
    7357              :      their uses.
    7358              :      ???  Technically this applies only to the registers used in the
    7359              :      resulting non-register *TO_P.  */
    7360     49076343 :   bool saved_into_ssa = gimplify_ctxp->into_ssa;
    7361     49076343 :   if (saved_into_ssa
    7362     46621123 :       && TREE_CODE (*from_p) == CALL_EXPR
    7363     54332895 :       && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
    7364         1539 :     gimplify_ctxp->into_ssa = false;
    7365     49076343 :   ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
    7366     49076343 :   gimplify_ctxp->into_ssa = saved_into_ssa;
    7367     49076343 :   if (ret == GS_ERROR)
    7368              :     return ret;
    7369              : 
    7370              :   /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
    7371              :      guess for the predicate was wrong.  */
    7372     49076268 :   gimple_predicate final_pred = rhs_predicate_for (*to_p);
    7373     49076268 :   if (final_pred != initial_pred)
    7374              :     {
    7375      8421936 :       ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
    7376      8421936 :       if (ret == GS_ERROR)
    7377              :         return ret;
    7378              :     }
    7379              : 
    7380              :   /* In case of va_arg internal fn wrapped in a WITH_SIZE_EXPR, add the type
    7381              :      size as argument to the call.  */
    7382     49076268 :   if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
    7383              :     {
    7384         1464 :       tree call = TREE_OPERAND (*from_p, 0);
    7385         1464 :       tree vlasize = TREE_OPERAND (*from_p, 1);
    7386              : 
    7387         1464 :       if (TREE_CODE (call) == CALL_EXPR
    7388         1464 :           && CALL_EXPR_IFN (call) == IFN_VA_ARG)
    7389              :         {
    7390           95 :           int nargs = call_expr_nargs (call);
    7391           95 :           tree type = TREE_TYPE (call);
    7392           95 :           tree ap = CALL_EXPR_ARG (call, 0);
    7393           95 :           tree tag = CALL_EXPR_ARG (call, 1);
    7394           95 :           tree aptag = CALL_EXPR_ARG (call, 2);
    7395           95 :           tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
    7396              :                                                        IFN_VA_ARG, type,
    7397              :                                                        nargs + 1, ap, tag,
    7398              :                                                        aptag, vlasize);
    7399           95 :           TREE_OPERAND (*from_p, 0) = newcall;
    7400              :         }
    7401              :     }
    7402              : 
    7403              :   /* Now see if the above changed *from_p to something we handle specially.  */
    7404     49076268 :   ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
    7405              :                                   want_value);
    7406     49076268 :   if (ret != GS_UNHANDLED)
    7407              :     return ret;
    7408              : 
    7409              :   /* If we've got a variable sized assignment between two lvalues (i.e. does
    7410              :      not involve a call), then we can make things a bit more straightforward
    7411              :      by converting the assignment to memcpy or memset.  */
    7412     49075299 :   if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
    7413              :     {
    7414         1111 :       tree from = TREE_OPERAND (*from_p, 0);
    7415         1111 :       tree size = TREE_OPERAND (*from_p, 1);
    7416              : 
    7417         1111 :       if (TREE_CODE (from) == CONSTRUCTOR)
    7418            0 :         return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
    7419         1111 :       else if (is_gimple_addressable (from)
    7420         1111 :                && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (*to_p)))
    7421         2221 :                && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (from))))
    7422              :         {
    7423         1110 :           *from_p = from;
    7424         1110 :           return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
    7425         1110 :                                                  pre_p);
    7426              :         }
    7427              :     }
    7428              : 
    7429              :   /* Transform partial stores to non-addressable complex variables into
    7430              :      total stores.  This allows us to use real instead of virtual operands
    7431              :      for these variables, which improves optimization.  */
    7432     49074189 :   if ((TREE_CODE (*to_p) == REALPART_EXPR
    7433     49074189 :        || TREE_CODE (*to_p) == IMAGPART_EXPR)
    7434     49074189 :       && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
    7435         1960 :     return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
    7436              : 
    7437              :   /* Try to alleviate the effects of the gimplification creating artificial
    7438              :      temporaries (see for example is_gimple_reg_rhs) on the debug info, but
    7439              :      make sure not to create DECL_DEBUG_EXPR links across functions.  */
    7440     49072229 :   if (!gimplify_ctxp->into_ssa
    7441      2454290 :       && VAR_P (*from_p)
    7442       486679 :       && DECL_IGNORED_P (*from_p)
    7443       360091 :       && DECL_P (*to_p)
    7444       142624 :       && !DECL_IGNORED_P (*to_p)
    7445        40951 :       && decl_function_context (*to_p) == current_function_decl
    7446     49106886 :       && decl_function_context (*from_p) == current_function_decl)
    7447              :     {
    7448        34652 :       if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
    7449        29271 :         DECL_NAME (*from_p)
    7450        58542 :           = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
    7451        34652 :       DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
    7452        34652 :       SET_DECL_DEBUG_EXPR (*from_p, *to_p);
    7453              :    }
    7454              : 
    7455     49072229 :   if (want_value && TREE_THIS_VOLATILE (*to_p))
    7456          522 :     *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
    7457              : 
    7458     49072229 :   if (TREE_CODE (*from_p) == CALL_EXPR)
    7459              :     {
    7460              :       /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
    7461              :          instead of a GIMPLE_ASSIGN.  */
    7462      4935266 :       gcall *call_stmt;
    7463      4935266 :       if (CALL_EXPR_FN (*from_p) == NULL_TREE)
    7464              :         {
    7465              :           /* Gimplify internal functions created in the FEs.  */
    7466       348290 :           int nargs = call_expr_nargs (*from_p), i;
    7467       348290 :           enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
    7468       348290 :           auto_vec<tree> vargs (nargs);
    7469              : 
    7470      1753326 :           for (i = 0; i < nargs; i++)
    7471              :             {
    7472      1056746 :               gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
    7473      1056746 :                             EXPR_LOCATION (*from_p));
    7474      1056746 :               vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
    7475              :             }
    7476       348290 :           call_stmt = gimple_build_call_internal_vec (ifn, vargs);
    7477       348290 :           gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
    7478       696580 :           gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
    7479       348290 :         }
    7480              :       else
    7481              :         {
    7482      4586976 :           tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
    7483      4586976 :           CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
    7484      4586976 :           STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
    7485      4586976 :           tree fndecl = get_callee_fndecl (*from_p);
    7486      4586976 :           if (fndecl
    7487      4470825 :               && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
    7488      4726856 :               && call_expr_nargs (*from_p) == 3)
    7489        70242 :             call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
    7490        70242 :                                                     CALL_EXPR_ARG (*from_p, 0),
    7491        70242 :                                                     CALL_EXPR_ARG (*from_p, 1),
    7492        70242 :                                                     CALL_EXPR_ARG (*from_p, 2));
    7493              :           else
    7494              :             {
    7495      4516734 :               call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
    7496              :             }
    7497              :         }
    7498      4935266 :       notice_special_calls (call_stmt);
    7499      4935266 :       if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
    7500      4935022 :         gimple_call_set_lhs (call_stmt, *to_p);
    7501          244 :       else if (TREE_CODE (*to_p) == SSA_NAME)
    7502              :         /* The above is somewhat premature, avoid ICEing later for a
    7503              :            SSA name w/o a definition.  We may have uses in the GIMPLE IL.
    7504              :            ???  This doesn't make it a default-def.  */
    7505          137 :         SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
    7506              : 
    7507              :       assign = call_stmt;
    7508              :     }
    7509              :   else
    7510              :     {
    7511     44136963 :       assign = gimple_build_assign (*to_p, *from_p);
    7512     44136963 :       gimple_set_location (assign, EXPR_LOCATION (*expr_p));
    7513     44136963 :       if (COMPARISON_CLASS_P (*from_p))
    7514      1256509 :         copy_warning (assign, *from_p);
    7515              :     }
    7516              : 
    7517     49072229 :   if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
    7518              :     {
    7519              :       /* We should have got an SSA name from the start.  */
    7520     36225544 :       gcc_assert (TREE_CODE (*to_p) == SSA_NAME
    7521              :                   || ! gimple_in_ssa_p (cfun));
    7522              :     }
    7523              : 
    7524     49072229 :   gimplify_seq_add_stmt (pre_p, assign);
    7525     49072229 :   gsi = gsi_last (*pre_p);
    7526     49072229 :   maybe_fold_stmt (&gsi);
    7527              : 
    7528     49072229 :   if (want_value)
    7529              :     {
    7530       713160 :       *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
    7531       713160 :       return GS_OK;
    7532              :     }
    7533              :   else
    7534     48359069 :     *expr_p = NULL;
    7535              : 
    7536     48359069 :   return GS_ALL_DONE;
    7537              : }
    7538              : 
    7539              : /* Gimplify a comparison between two variable-sized objects.  Do this
    7540              :    with a call to BUILT_IN_MEMCMP.  */
    7541              : 
    7542              : static enum gimplify_status
    7543            0 : gimplify_variable_sized_compare (tree *expr_p)
    7544              : {
    7545            0 :   location_t loc = EXPR_LOCATION (*expr_p);
    7546            0 :   tree op0 = TREE_OPERAND (*expr_p, 0);
    7547            0 :   tree op1 = TREE_OPERAND (*expr_p, 1);
    7548            0 :   tree t, arg, dest, src, expr;
    7549              : 
    7550            0 :   arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
    7551            0 :   arg = unshare_expr (arg);
    7552            0 :   arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
    7553            0 :   src = build_fold_addr_expr_loc (loc, op1);
    7554            0 :   dest = build_fold_addr_expr_loc (loc, op0);
    7555            0 :   t = builtin_decl_implicit (BUILT_IN_MEMCMP);
    7556            0 :   t = build_call_expr_loc (loc, t, 3, dest, src, arg);
    7557              : 
    7558            0 :   expr
    7559            0 :     = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
    7560            0 :   SET_EXPR_LOCATION (expr, loc);
    7561            0 :   *expr_p = expr;
    7562              : 
    7563            0 :   return GS_OK;
    7564              : }
    7565              : 
    7566              : /* Gimplify a comparison between two aggregate objects of integral scalar
    7567              :    mode as a comparison between the bitwise equivalent scalar values.  */
    7568              : 
    7569              : static enum gimplify_status
    7570           16 : gimplify_scalar_mode_aggregate_compare (tree *expr_p)
    7571              : {
    7572           16 :   const location_t loc = EXPR_LOCATION (*expr_p);
    7573           16 :   const enum tree_code code = TREE_CODE (*expr_p);
    7574           16 :   tree op0 = TREE_OPERAND (*expr_p, 0);
    7575           16 :   tree op1 = TREE_OPERAND (*expr_p, 1);
    7576           16 :   tree type = TREE_TYPE (op0);
    7577           16 :   tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
    7578              : 
    7579           16 :   op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
    7580           16 :   op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
    7581              : 
    7582              :   /* We need to perform ordering comparisons in memory order like memcmp and,
    7583              :      therefore, may need to byte-swap operands for little-endian targets.  */
    7584           16 :   if (code != EQ_EXPR && code != NE_EXPR)
    7585              :     {
    7586            0 :       gcc_assert (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN);
    7587            0 :       gcc_assert (TREE_CODE (scalar_type) == INTEGER_TYPE);
    7588            0 :       tree fndecl;
    7589              : 
    7590            0 :       if (BYTES_BIG_ENDIAN)
    7591              :         fndecl = NULL_TREE;
    7592              :       else
    7593            0 :         switch (int_size_in_bytes (scalar_type))
    7594              :           {
    7595              :           case 1:
    7596              :             fndecl = NULL_TREE;
    7597              :             break;
    7598            0 :           case 2:
    7599            0 :             fndecl = builtin_decl_implicit (BUILT_IN_BSWAP16);
    7600              :             break;
    7601            0 :           case 4:
    7602            0 :             fndecl = builtin_decl_implicit (BUILT_IN_BSWAP32);
    7603              :             break;
    7604            0 :           case 8:
    7605            0 :             fndecl = builtin_decl_implicit (BUILT_IN_BSWAP64);
    7606              :             break;
    7607            0 :           case 16:
    7608            0 :             fndecl = builtin_decl_implicit (BUILT_IN_BSWAP128);
    7609              :             break;
    7610            0 :           default:
    7611            0 :             gcc_unreachable ();
    7612              :           }
    7613              : 
    7614            0 :       if (fndecl)
    7615              :         {
    7616            0 :           op0 = build_call_expr_loc (loc, fndecl, 1, op0);
    7617            0 :           op1 = build_call_expr_loc (loc, fndecl, 1, op1);
    7618              :         }
    7619              :     }
    7620              : 
    7621           16 :   *expr_p = fold_build2_loc (loc, code, TREE_TYPE (*expr_p), op0, op1);
    7622              : 
    7623           16 :   return GS_OK;
    7624              : }
    7625              : 
    7626              : /* Gimplify an expression sequence.  This function gimplifies each
    7627              :    expression and rewrites the original expression with the last
    7628              :    expression of the sequence in GIMPLE form.
    7629              : 
    7630              :    PRE_P points to the list where the side effects for all the
    7631              :        expressions in the sequence will be emitted.
    7632              : 
    7633              :    WANT_VALUE is true when the result of the last COMPOUND_EXPR is used.  */
    7634              : 
    7635              : static enum gimplify_status
    7636      1136337 : gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
    7637              : {
    7638      1136337 :   tree t = *expr_p;
    7639              : 
    7640      1227961 :   do
    7641              :     {
    7642      1227961 :       tree *sub_p = &TREE_OPERAND (t, 0);
    7643              : 
    7644      1227961 :       if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
    7645       215241 :         gimplify_compound_expr (sub_p, pre_p, false);
    7646              :       else
    7647      1012720 :         gimplify_stmt (sub_p, pre_p);
    7648              : 
    7649      1227961 :       t = TREE_OPERAND (t, 1);
    7650              :     }
    7651      1227961 :   while (TREE_CODE (t) == COMPOUND_EXPR);
    7652              : 
    7653      1136337 :   *expr_p = t;
    7654      1136337 :   if (want_value)
    7655              :     return GS_OK;
    7656              :   else
    7657              :     {
    7658       635199 :       gimplify_stmt (expr_p, pre_p);
    7659       635199 :       return GS_ALL_DONE;
    7660              :     }
    7661              : }
    7662              : 
    7663              : /* Gimplify a SAVE_EXPR node.  EXPR_P points to the expression to
    7664              :    gimplify.  After gimplification, EXPR_P will point to a new temporary
    7665              :    that holds the original value of the SAVE_EXPR node.
    7666              : 
    7667              :    PRE_P points to the list where side effects that must happen before
    7668              :    *EXPR_P should be stored.  */
    7669              : 
    7670              : static enum gimplify_status
    7671       445674 : gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
    7672              : {
    7673       445674 :   enum gimplify_status ret = GS_ALL_DONE;
    7674       445674 :   tree val;
    7675              : 
    7676       445674 :   gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
    7677       445674 :   val = TREE_OPERAND (*expr_p, 0);
    7678              : 
    7679       445674 :   if (val && TREE_TYPE (val) == error_mark_node)
    7680              :     return GS_ERROR;
    7681              : 
    7682              :   /* If the SAVE_EXPR has not been resolved, then evaluate it once.  */
    7683       445672 :   if (!SAVE_EXPR_RESOLVED_P (*expr_p))
    7684              :     {
    7685              :       /* The operand may be a void-valued expression.  It is
    7686              :          being executed only for its side-effects.  */
    7687       171482 :       if (TREE_TYPE (val) == void_type_node)
    7688              :         {
    7689          751 :           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
    7690              :                                is_gimple_stmt, fb_none);
    7691          751 :           val = NULL;
    7692              :         }
    7693              :       else
    7694              :         /* The temporary may not be an SSA name as later abnormal and EH
    7695              :            control flow may invalidate use/def domination.  When in SSA
    7696              :            form then assume there are no such issues and SAVE_EXPRs only
    7697              :            appear via GENERIC foldings.  */
    7698       341462 :         val = get_initialized_tmp_var (val, pre_p, post_p,
    7699              :                                        gimple_in_ssa_p (cfun));
    7700              : 
    7701       171482 :       TREE_OPERAND (*expr_p, 0) = val;
    7702       171482 :       SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
    7703              :     }
    7704              : 
    7705       445672 :   *expr_p = val;
    7706              : 
    7707       445672 :   return ret;
    7708              : }
    7709              : 
    7710              : /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
    7711              : 
    7712              :       unary_expr
    7713              :               : ...
    7714              :               | '&' varname
    7715              :               ...
    7716              : 
    7717              :     PRE_P points to the list where side effects that must happen before
    7718              :         *EXPR_P should be stored.
    7719              : 
    7720              :     POST_P points to the list where side effects that must happen after
    7721              :         *EXPR_P should be stored.  */
    7722              : 
    7723              : static enum gimplify_status
    7724     34229061 : gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
    7725              : {
    7726     34229061 :   tree expr = *expr_p;
    7727     34229061 :   tree op0 = TREE_OPERAND (expr, 0);
    7728     34229061 :   enum gimplify_status ret;
    7729     34229061 :   location_t loc = EXPR_LOCATION (*expr_p);
    7730              : 
    7731     34229061 :   switch (TREE_CODE (op0))
    7732              :     {
    7733       104004 :     case INDIRECT_REF:
    7734       104004 :     do_indirect_ref:
    7735              :       /* Check if we are dealing with an expression of the form '&*ptr'.
    7736              :          While the front end folds away '&*ptr' into 'ptr', these
    7737              :          expressions may be generated internally by the compiler (e.g.,
    7738              :          builtins like __builtin_va_end).  */
    7739              :       /* Caution: the silent array decomposition semantics we allow for
    7740              :          ADDR_EXPR means we can't always discard the pair.  */
    7741              :       /* Gimplification of the ADDR_EXPR operand may drop
    7742              :          cv-qualification conversions, so make sure we add them if
    7743              :          needed.  */
    7744       104004 :       {
    7745       104004 :         tree op00 = TREE_OPERAND (op0, 0);
    7746       104004 :         tree t_expr = TREE_TYPE (expr);
    7747       104004 :         tree t_op00 = TREE_TYPE (op00);
    7748              : 
    7749       104004 :         if (!useless_type_conversion_p (t_expr, t_op00))
    7750            0 :           op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
    7751       104004 :         *expr_p = op00;
    7752       104004 :         ret = GS_OK;
    7753              :       }
    7754       104004 :       break;
    7755              : 
    7756            0 :     case VIEW_CONVERT_EXPR:
    7757              :       /* Take the address of our operand and then convert it to the type of
    7758              :          this ADDR_EXPR.
    7759              : 
    7760              :          ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
    7761              :          all clear.  The impact of this transformation is even less clear.  */
    7762              : 
    7763              :       /* If the operand is a useless conversion, look through it.  Doing so
    7764              :          guarantees that the ADDR_EXPR and its operand will remain of the
    7765              :          same type.  */
    7766            0 :       if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
    7767            0 :         op0 = TREE_OPERAND (op0, 0);
    7768              : 
    7769            0 :       *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
    7770              :                                   build_fold_addr_expr_loc (loc,
    7771            0 :                                                         TREE_OPERAND (op0, 0)));
    7772            0 :       ret = GS_OK;
    7773            0 :       break;
    7774              : 
    7775        68322 :     case MEM_REF:
    7776        68322 :       if (integer_zerop (TREE_OPERAND (op0, 1)))
    7777        39572 :         goto do_indirect_ref;
    7778              : 
    7779              :       /* fall through */
    7780              : 
    7781     34143625 :     default:
    7782              :       /* If we see a call to a declared builtin or see its address
    7783              :          being taken (we can unify those cases here) then we can mark
    7784              :          the builtin for implicit generation by GCC.  */
    7785     34143625 :       if (TREE_CODE (op0) == FUNCTION_DECL
    7786     16972527 :           && fndecl_built_in_p (op0, BUILT_IN_NORMAL)
    7787     38165294 :           && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
    7788      1314075 :         set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
    7789              : 
    7790              :       /* We use fb_either here because the C frontend sometimes takes
    7791              :          the address of a call that returns a struct; see
    7792              :          gcc.dg/c99-array-lval-1.c.  The gimplifier will correctly make
    7793              :          the implied temporary explicit.  */
    7794              : 
    7795              :       /* Make the operand addressable.  */
    7796     34143625 :       ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
    7797              :                            is_gimple_addressable, fb_either);
    7798     34143625 :       if (ret == GS_ERROR)
    7799              :         break;
    7800              : 
    7801              :       /* Then mark it.  Beware that it may not be possible to do so directly
    7802              :          if a temporary has been created by the gimplification.  */
    7803     34143621 :       prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
    7804              : 
    7805     34143621 :       op0 = TREE_OPERAND (expr, 0);
    7806              : 
    7807              :       /* For various reasons, the gimplification of the expression
    7808              :          may have made a new INDIRECT_REF.  */
    7809     34143621 :       if (INDIRECT_REF_P (op0)
    7810     34143621 :           || (TREE_CODE (op0) == MEM_REF
    7811        47318 :               && integer_zerop (TREE_OPERAND (op0, 1))))
    7812        18568 :         goto do_indirect_ref;
    7813              : 
    7814     34125053 :       mark_addressable (TREE_OPERAND (expr, 0));
    7815              : 
    7816              :       /* The FEs may end up building ADDR_EXPRs early on a decl with
    7817              :          an incomplete type.  Re-build ADDR_EXPRs in canonical form
    7818              :          here.  */
    7819     34125053 :       if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
    7820       151011 :         *expr_p = build_fold_addr_expr (op0);
    7821              : 
    7822              :       /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly.  */
    7823     34125053 :       if (TREE_CODE (*expr_p) == ADDR_EXPR)
    7824     34125053 :         recompute_tree_invariant_for_addr_expr (*expr_p);
    7825              : 
    7826              :       /* If we re-built the ADDR_EXPR add a conversion to the original type
    7827              :          if required.  */
    7828     34125053 :       if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
    7829            0 :         *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
    7830              : 
    7831              :       break;
    7832              :     }
    7833              : 
    7834     34229061 :   return ret;
    7835              : }
    7836              : 
    7837              : /* Return the number of times character C occurs in string S.  */
    7838              : 
    7839              : static int
    7840        96611 : num_occurrences (int c, const char *s)
    7841              : {
    7842        96611 :   int n = 0;
    7843       261905 :   while (*s)
    7844       165294 :     n += (*s++ == c);
    7845        96611 :   return n;
    7846              : }
    7847              : 
    7848              : /* A subroutine of gimplify_asm_expr.  Check that all operands have
    7849              :    the same number of alternatives.  Return -1 if this is violated.  Otherwise
    7850              :    return the number of alternatives.  */
    7851              : 
    7852              : static int
    7853       167200 : num_alternatives (const_tree link)
    7854              : {
    7855       167200 :   if (link == nullptr)
    7856              :     return 0;
    7857              : 
    7858        49429 :   const char *constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
    7859        49429 :   int num = num_occurrences (',', constraint);
    7860              : 
    7861        49429 :   if (num + 1 > MAX_RECOG_ALTERNATIVES)
    7862              :     return -1;
    7863              : 
    7864        96611 :   for (link = TREE_CHAIN (link); link; link = TREE_CHAIN (link))
    7865              :     {
    7866        47182 :       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
    7867        94364 :       if (num_occurrences (',', constraint) != num)
    7868              :         return -1;
    7869              :     }
    7870        49429 :   return num + 1;
    7871              : }
    7872              : 
    7873              : /* Gimplify the operands of an ASM_EXPR.  Input operands should be a gimple
    7874              :    value; output operands should be a gimple lvalue.  */
    7875              : 
    7876              : static enum gimplify_status
    7877        83600 : gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
    7878              : {
    7879        83600 :   tree expr;
    7880        83600 :   int noutputs;
    7881        83600 :   const char **oconstraints;
    7882        83600 :   int i;
    7883        83600 :   tree link;
    7884        83600 :   const char *constraint;
    7885        83600 :   bool allows_mem, allows_reg, is_inout;
    7886        83600 :   enum gimplify_status ret, tret;
    7887        83600 :   gasm *stmt;
    7888        83600 :   vec<tree, va_gc> *inputs;
    7889        83600 :   vec<tree, va_gc> *outputs;
    7890        83600 :   vec<tree, va_gc> *clobbers;
    7891        83600 :   vec<tree, va_gc> *labels;
    7892        83600 :   tree link_next;
    7893              : 
    7894        83600 :   expr = *expr_p;
    7895        83600 :   noutputs = list_length (ASM_OUTPUTS (expr));
    7896        83600 :   oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
    7897              : 
    7898        83600 :   inputs = NULL;
    7899        83600 :   outputs = NULL;
    7900        83600 :   clobbers = NULL;
    7901        83600 :   labels = NULL;
    7902              : 
    7903        83600 :   int num_alternatives_out = num_alternatives (ASM_OUTPUTS (expr));
    7904        83600 :   int num_alternatives_in = num_alternatives (ASM_INPUTS (expr));
    7905        83600 :   if (num_alternatives_out == -1 || num_alternatives_in == -1
    7906        83600 :       || (num_alternatives_out > 0 && num_alternatives_in > 0
    7907        17553 :           && num_alternatives_out != num_alternatives_in))
    7908              :     {
    7909            0 :       error ("operand constraints for %<asm%> differ "
    7910              :              "in number of alternatives");
    7911            0 :       return GS_ERROR;
    7912              :     }
    7913        83600 :   int num_alternatives = MAX (num_alternatives_out, num_alternatives_in);
    7914              : 
    7915        83600 :   gimplify_reg_info reg_info (num_alternatives, noutputs);
    7916              : 
    7917        83600 :   link_next = NULL_TREE;
    7918       164396 :   for (link = ASM_CLOBBERS (expr); link; link = link_next)
    7919              :     {
    7920              :       /* The clobber entry could also be an error marker.  */
    7921        80796 :       if (TREE_CODE (TREE_VALUE (link)) == STRING_CST)
    7922              :         {
    7923        80773 :           const char *regname= TREE_STRING_POINTER (TREE_VALUE (link));
    7924        80773 :           int regno = decode_reg_name (regname);
    7925        80773 :           if (regno >= 0)
    7926        41267 :             reg_info.set_clobbered (regno);
    7927              :         }
    7928        80796 :       link_next = TREE_CHAIN (link);
    7929        80796 :       TREE_CHAIN (link) = NULL_TREE;
    7930        80796 :       vec_safe_push (clobbers, link);
    7931              :     }
    7932              : 
    7933        83600 :   ret = GS_ALL_DONE;
    7934        83600 :   link_next = NULL_TREE;
    7935       145949 :   for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
    7936              :     {
    7937        62382 :       bool ok;
    7938        62382 :       size_t constraint_len;
    7939              : 
    7940        62382 :       if (error_operand_p (TREE_VALUE (link)))
    7941              :         return GS_ERROR;
    7942        62361 :       link_next = TREE_CHAIN (link);
    7943              : 
    7944       124722 :       oconstraints[i]
    7945        62361 :         = constraint
    7946        62361 :         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
    7947        62361 :       constraint_len = strlen (constraint);
    7948        62361 :       if (constraint_len == 0)
    7949            0 :         continue;
    7950              : 
    7951        62361 :       reg_info.operand = TREE_VALUE (link);
    7952        62361 :       ok = parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
    7953              :                                     &allows_reg, &is_inout, &reg_info);
    7954        62361 :       if (!ok)
    7955              :         {
    7956           11 :           ret = GS_ERROR;
    7957           11 :           is_inout = false;
    7958              :         }
    7959              : 
    7960        62361 :       if (constraint != oconstraints[i])
    7961              :         {
    7962              :           /* The constraint has been canonicalized, e.g., "r=" into "=r" or
    7963              :              "+r" into "=r" in case of an in/out operand.  Use the canonical
    7964              :              form from now on since otherwise we warn about
    7965              :              "output constraint '=' for operand ... is not at the beginning"
    7966              :              multiple times.  */
    7967         8757 :           TREE_PURPOSE (link) = unshare_expr (TREE_PURPOSE (link));
    7968         8757 :           TREE_VALUE (TREE_PURPOSE (link))
    7969        17514 :             = build_string (constraint_len, constraint);
    7970              :         }
    7971              : 
    7972              :       /* If we can't make copies, we can only accept memory.
    7973              :          Similarly for VLAs.  */
    7974        62361 :       tree outtype = TREE_TYPE (TREE_VALUE (link));
    7975        62361 :       if (TREE_ADDRESSABLE (outtype)
    7976        62335 :           || !COMPLETE_TYPE_P (outtype)
    7977       124655 :           || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (outtype)))
    7978              :         {
    7979          291 :           if (allows_mem)
    7980          279 :             allows_reg = 0;
    7981              :           else
    7982              :             {
    7983           12 :               error ("impossible constraint in %<asm%>");
    7984           12 :               error ("non-memory output %d must stay in memory", i);
    7985           12 :               return GS_ERROR;
    7986              :             }
    7987              :         }
    7988              : 
    7989        62349 :       if (!allows_reg && allows_mem)
    7990         2082 :         mark_addressable (TREE_VALUE (link));
    7991              : 
    7992        62349 :       tree orig = TREE_VALUE (link);
    7993       115954 :       tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
    7994              :                             is_inout ? is_gimple_min_lval : is_gimple_lvalue,
    7995              :                             fb_lvalue | fb_mayfail);
    7996        62349 :       if (tret == GS_ERROR)
    7997              :         {
    7998            0 :           if (orig != error_mark_node)
    7999            0 :             error ("invalid lvalue in %<asm%> output %d", i);
    8000              :           ret = tret;
    8001              :         }
    8002              : 
    8003              :       /* If the gimplified operand is a register we do not allow memory.  */
    8004        62349 :       if (allows_reg
    8005        60264 :           && allows_mem
    8006        68550 :           && (is_gimple_reg (TREE_VALUE (link))
    8007          273 :               || (handled_component_p (TREE_VALUE (link))
    8008           36 :                   && is_gimple_reg (TREE_OPERAND (TREE_VALUE (link), 0)))))
    8009         5929 :         allows_mem = 0;
    8010              : 
    8011              :       /* If the constraint does not allow memory make sure we gimplify
    8012              :          it to a register if it is not already but its base is.  This
    8013              :          happens for complex and vector components.  */
    8014        62349 :       if (!allows_mem)
    8015              :         {
    8016        59995 :           tree op = TREE_VALUE (link);
    8017        59995 :           if (! is_gimple_val (op)
    8018        21932 :               && is_gimple_reg_type (TREE_TYPE (op))
    8019        81926 :               && is_gimple_reg (get_base_address (op)))
    8020              :             {
    8021           19 :               tree tem = create_tmp_reg (TREE_TYPE (op));
    8022           19 :               tree ass;
    8023           19 :               if (is_inout)
    8024              :                 {
    8025            7 :                   ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
    8026              :                                 tem, unshare_expr (op));
    8027            7 :                   gimplify_and_add (ass, pre_p);
    8028              :                 }
    8029           19 :               ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
    8030           19 :               gimplify_and_add (ass, post_p);
    8031              : 
    8032           19 :               TREE_VALUE (link) = tem;
    8033           19 :               tret = GS_OK;
    8034              :             }
    8035              :         }
    8036              : 
    8037        62349 :       vec_safe_push (outputs, link);
    8038        62349 :       TREE_CHAIN (link) = NULL_TREE;
    8039              : 
    8040        62349 :       if (is_inout)
    8041              :         {
    8042              :           /* An input/output operand.  To give the optimizers more
    8043              :              flexibility, split it into separate input and output
    8044              :              operands.  */
    8045         8744 :           tree input;
    8046              :           /* Buffer big enough to format a 32-bit UINT_MAX into.  */
    8047         8744 :           char buf[11];
    8048         8744 :           char *p = xstrdup (constraint);
    8049              : 
    8050              :           /* And add a matching input constraint.  */
    8051         8744 :           if (allows_reg)
    8052              :             {
    8053         8240 :               sprintf (buf, "%u", i);
    8054              : 
    8055              :               /* If there are multiple alternatives in the constraint,
    8056              :                  handle each of them individually.  Those that allow register
    8057              :                  will be replaced with operand number, the others will stay
    8058              :                  unchanged.  */
    8059         8240 :               if (strchr (p, ',') != NULL)
    8060              :                 {
    8061          101 :                   size_t len = 0, buflen = strlen (buf);
    8062          101 :                   char *beg, *end, *str, *dst;
    8063              : 
    8064          101 :                   for (beg = p + 1;;)
    8065              :                     {
    8066          202 :                       end = strchr (beg, ',');
    8067          202 :                       if (end == NULL)
    8068          101 :                         end = strchr (beg, '\0');
    8069          202 :                       if ((size_t) (end - beg) < buflen)
    8070           28 :                         len += buflen + 1;
    8071              :                       else
    8072          174 :                         len += end - beg + 1;
    8073          202 :                       if (*end)
    8074          101 :                         beg = end + 1;
    8075              :                       else
    8076              :                         break;
    8077              :                     }
    8078              : 
    8079          101 :                   str = (char *) alloca (len);
    8080          101 :                   for (beg = p + 1, dst = str;;)
    8081              :                     {
    8082          202 :                       const char *tem;
    8083          202 :                       bool mem_p, reg_p, inout_p;
    8084              : 
    8085          202 :                       end = strchr (beg, ',');
    8086          202 :                       if (end)
    8087          101 :                         *end = '\0';
    8088          202 :                       beg[-1] = '=';
    8089          202 :                       tem = beg - 1;
    8090          202 :                       parse_output_constraint (&tem, i, 0, 0, &mem_p, &reg_p,
    8091              :                                                &inout_p, nullptr);
    8092          202 :                       if (dst != str)
    8093          101 :                         *dst++ = ',';
    8094          202 :                       if (reg_p)
    8095              :                         {
    8096          117 :                           memcpy (dst, buf, buflen);
    8097          117 :                           dst += buflen;
    8098              :                         }
    8099              :                       else
    8100              :                         {
    8101           85 :                           if (end)
    8102            0 :                             len = end - beg;
    8103              :                           else
    8104           85 :                             len = strlen (beg);
    8105           85 :                           memcpy (dst, beg, len);
    8106           85 :                           dst += len;
    8107              :                         }
    8108          202 :                       if (end)
    8109          101 :                         beg = end + 1;
    8110              :                       else
    8111              :                         break;
    8112          101 :                     }
    8113          101 :                   *dst = '\0';
    8114          101 :                   input = build_string (dst - str, str);
    8115              :                 }
    8116              :               else
    8117         8139 :                 input = build_string (strlen (buf), buf);
    8118              :             }
    8119              :           else
    8120          504 :             input = build_string (constraint_len - 1, constraint + 1);
    8121              : 
    8122         8744 :           free (p);
    8123              : 
    8124         8744 :           input = build_tree_list (build_tree_list (NULL_TREE, input),
    8125         8744 :                                    unshare_expr (TREE_VALUE (link)));
    8126         8744 :           ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
    8127              :         }
    8128              :     }
    8129              : 
    8130              :   /* After all output operands have been gimplified, verify that each output
    8131              :      operand is used at most once in case of hard register constraints.  Thus,
    8132              :      error out in cases like
    8133              :        asm ("" : "={0}" (x), "={1}" (x));
    8134              :      or even for
    8135              :        asm ("" : "=r" (x), "={1}" (x));
    8136              : 
    8137              :      FIXME: Ideally we would also error out for cases like
    8138              :        int x;
    8139              :        asm ("" : "=r" (x), "=r" (x));
    8140              :      However, since code like that was previously accepted, erroring out now might
    8141              :      break existing code.  On the other hand, we already error out for register
    8142              :      asm like
    8143              :        register int x asm ("0");
    8144              :        asm ("" : "=r" (x), "=r" (x));
    8145              :      Thus, maybe it wouldn't be too bad to also error out in the former
    8146              :      non-register-asm case.
    8147              :   */
    8148       145912 :   for (unsigned i = 0; i < vec_safe_length (outputs); ++i)
    8149              :     {
    8150        62347 :       tree link = (*outputs)[i];
    8151        62347 :       tree op1 = TREE_VALUE (link);
    8152        62347 :       const char *constraint
    8153        62347 :         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
    8154        62347 :       if (strchr (constraint, '{') != nullptr)
    8155        62491 :         for (unsigned j = 0; j < vec_safe_length (outputs); ++j)
    8156              :           {
    8157          146 :             if (i == j)
    8158           99 :               continue;
    8159           47 :             tree link2 = (*outputs)[j];
    8160           47 :             tree op2 = TREE_VALUE (link2);
    8161           47 :             if (op1 == op2)
    8162              :               {
    8163            2 :                 error ("multiple outputs to lvalue %qE", op2);
    8164            2 :                 return GS_ERROR;
    8165              :               }
    8166              :           }
    8167              :     }
    8168              : 
    8169        83565 :   link_next = NULL_TREE;
    8170        83565 :   int input_num = 0;
    8171       126476 :   for (link = ASM_INPUTS (expr); link; ++input_num, ++i, link = link_next)
    8172              :     {
    8173        42969 :       if (error_operand_p (TREE_VALUE (link)))
    8174              :         return GS_ERROR;
    8175        42920 :       link_next = TREE_CHAIN (link);
    8176        42920 :       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
    8177        42920 :       reg_info.operand = TREE_VALUE (link);
    8178        42920 :       bool ok = parse_input_constraint (&constraint, input_num, 0, noutputs, 0,
    8179              :                                         oconstraints, &allows_mem, &allows_reg,
    8180              :                                         &reg_info);
    8181        42920 :       if (!ok)
    8182              :         {
    8183           30 :           ret = GS_ERROR;
    8184           30 :           is_inout = false;
    8185              :         }
    8186              : 
    8187              :       /* If we can't make copies, we can only accept memory.  */
    8188        42920 :       tree intype = TREE_TYPE (TREE_VALUE (link));
    8189        42920 :       if (TREE_ADDRESSABLE (intype)
    8190        42891 :           || !COMPLETE_TYPE_P (intype)
    8191        85756 :           || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (intype)))
    8192              :         {
    8193          300 :           if (allows_mem)
    8194          291 :             allows_reg = 0;
    8195              :           else
    8196              :             {
    8197            9 :               error ("impossible constraint in %<asm%>");
    8198            9 :               error ("non-memory input %d must stay in memory", i);
    8199            9 :               return GS_ERROR;
    8200              :             }
    8201              :         }
    8202              : 
    8203              :       /* If the operand is a memory input, it should be an lvalue.  */
    8204        42911 :       if (!allows_reg && allows_mem)
    8205              :         {
    8206         2436 :           tree inputv = TREE_VALUE (link);
    8207         2436 :           STRIP_NOPS (inputv);
    8208         2436 :           if (TREE_CODE (inputv) == PREDECREMENT_EXPR
    8209              :               || TREE_CODE (inputv) == PREINCREMENT_EXPR
    8210              :               || TREE_CODE (inputv) == POSTDECREMENT_EXPR
    8211         2436 :               || TREE_CODE (inputv) == POSTINCREMENT_EXPR
    8212         2424 :               || TREE_CODE (inputv) == MODIFY_EXPR
    8213         4858 :               || VOID_TYPE_P (TREE_TYPE (inputv)))
    8214           38 :             TREE_VALUE (link) = error_mark_node;
    8215         2436 :           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
    8216              :                                 is_gimple_lvalue, fb_lvalue | fb_mayfail);
    8217         2436 :           if (tret != GS_ERROR)
    8218              :             {
    8219              :               /* Unlike output operands, memory inputs are not guaranteed
    8220              :                  to be lvalues by the FE, and while the expressions are
    8221              :                  marked addressable there, if it is e.g. a statement
    8222              :                  expression, temporaries in it might not end up being
    8223              :                  addressable.  They might be already used in the IL and thus
    8224              :                  it is too late to make them addressable now though.  */
    8225         2390 :               tree x = TREE_VALUE (link);
    8226         2532 :               while (handled_component_p (x))
    8227          142 :                 x = TREE_OPERAND (x, 0);
    8228         2390 :               if (TREE_CODE (x) == MEM_REF
    8229         2390 :                   && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
    8230            0 :                 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
    8231         2390 :               if ((VAR_P (x)
    8232              :                    || TREE_CODE (x) == PARM_DECL
    8233              :                    || TREE_CODE (x) == RESULT_DECL)
    8234         1533 :                   && !TREE_ADDRESSABLE (x)
    8235           23 :                   && is_gimple_reg (x))
    8236              :                 {
    8237           17 :                   warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
    8238           17 :                                                input_location), 0,
    8239              :                               "memory input %d is not directly addressable",
    8240              :                               i);
    8241           17 :                   prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
    8242              :                 }
    8243              :             }
    8244         2436 :           mark_addressable (TREE_VALUE (link));
    8245         2436 :           if (tret == GS_ERROR)
    8246              :             {
    8247           46 :               if (inputv != error_mark_node)
    8248           46 :                 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
    8249              :                           "memory input %d is not directly addressable", i);
    8250              :               ret = tret;
    8251              :             }
    8252              :         }
    8253              :       else
    8254              :         {
    8255        40475 :           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
    8256              :                                 is_gimple_asm_val, fb_rvalue);
    8257        40475 :           if (tret == GS_ERROR)
    8258           46 :             ret = tret;
    8259              :         }
    8260              : 
    8261        42911 :       TREE_CHAIN (link) = NULL_TREE;
    8262        42911 :       vec_safe_push (inputs, link);
    8263              :     }
    8264              : 
    8265        83507 :   link_next = NULL_TREE;
    8266        84466 :   for (link = ASM_LABELS (expr); link; link = link_next)
    8267              :     {
    8268          959 :       link_next = TREE_CHAIN (link);
    8269          959 :       TREE_CHAIN (link) = NULL_TREE;
    8270          959 :       vec_safe_push (labels, link);
    8271              :     }
    8272              : 
    8273              :   /* Do not add ASMs with errors to the gimple IL stream.  */
    8274        83507 :   if (ret != GS_ERROR)
    8275              :     {
    8276        83424 :       stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
    8277              :                                    inputs, outputs, clobbers, labels);
    8278              : 
    8279              :       /* asm is volatile if it was marked by the user as volatile or
    8280              :          there are no outputs or this is an asm goto.  */
    8281        90958 :       gimple_asm_set_volatile (stmt,
    8282        83424 :                                ASM_VOLATILE_P (expr)
    8283         7548 :                                || noutputs == 0
    8284              :                                || labels);
    8285        83424 :       gimple_asm_set_basic (stmt, ASM_BASIC_P (expr));
    8286        83424 :       gimple_asm_set_inline (stmt, ASM_INLINE_P (expr));
    8287              : 
    8288        83424 :       gimplify_seq_add_stmt (pre_p, stmt);
    8289              :     }
    8290              : 
    8291              :   return ret;
    8292        83600 : }
    8293              : 
    8294              : /* Gimplify a CLEANUP_POINT_EXPR.  Currently this works by adding
    8295              :    GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
    8296              :    gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
    8297              :    return to this function.
    8298              : 
    8299              :    FIXME should we complexify the prequeue handling instead?  Or use flags
    8300              :    for all the cleanups and let the optimizer tighten them up?  The current
    8301              :    code seems pretty fragile; it will break on a cleanup within any
    8302              :    non-conditional nesting.  But any such nesting would be broken, anyway;
    8303              :    we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
    8304              :    and continues out of it.  We can do that at the RTL level, though, so
    8305              :    having an optimizer to tighten up try/finally regions would be a Good
    8306              :    Thing.  */
    8307              : 
    8308              : static enum gimplify_status
    8309      5436501 : gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
    8310              : {
    8311      5436501 :   gimple_stmt_iterator iter;
    8312      5436501 :   gimple_seq body_sequence = NULL;
    8313              : 
    8314      5436501 :   tree temp = voidify_wrapper_expr (*expr_p, NULL);
    8315              : 
    8316              :   /* We only care about the number of conditions between the innermost
    8317              :      CLEANUP_POINT_EXPR and the cleanup.  So save and reset the count and
    8318              :      any cleanups collected outside the CLEANUP_POINT_EXPR.  */
    8319      5436501 :   int old_conds = gimplify_ctxp->conditions;
    8320      5436501 :   gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
    8321      5436501 :   bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
    8322      5436501 :   gimplify_ctxp->conditions = 0;
    8323      5436501 :   gimplify_ctxp->conditional_cleanups = NULL;
    8324      5436501 :   gimplify_ctxp->in_cleanup_point_expr = true;
    8325              : 
    8326      5436501 :   gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
    8327              : 
    8328      5436501 :   gimplify_ctxp->conditions = old_conds;
    8329      5436501 :   gimplify_ctxp->conditional_cleanups = old_cleanups;
    8330      5436501 :   gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
    8331              : 
    8332     28179670 :   for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
    8333              :     {
    8334     17353503 :       gimple *wce = gsi_stmt (iter);
    8335              : 
    8336     17353503 :       if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
    8337              :         {
    8338       647777 :           if (gsi_one_before_end_p (iter))
    8339              :             {
    8340              :               /* Note that gsi_insert_seq_before and gsi_remove do not
    8341              :                  scan operands, unlike some other sequence mutators.  */
    8342         3133 :               if (!gimple_wce_cleanup_eh_only (wce))
    8343         3133 :                 gsi_insert_seq_before_without_update (&iter,
    8344              :                                                       gimple_wce_cleanup (wce),
    8345              :                                                       GSI_SAME_STMT);
    8346         3133 :               gsi_remove (&iter, true);
    8347         3133 :               break;
    8348              :             }
    8349              :           else
    8350              :             {
    8351       644644 :               gtry *gtry;
    8352       644644 :               gimple_seq seq;
    8353       644644 :               enum gimple_try_flags kind;
    8354              : 
    8355       644644 :               if (gimple_wce_cleanup_eh_only (wce))
    8356              :                 kind = GIMPLE_TRY_CATCH;
    8357              :               else
    8358       627386 :                 kind = GIMPLE_TRY_FINALLY;
    8359       644644 :               seq = gsi_split_seq_after (iter);
    8360              : 
    8361       644644 :               gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
    8362              :               /* Do not use gsi_replace here, as it may scan operands.
    8363              :                  We want to do a simple structural modification only.  */
    8364       644644 :               gsi_set_stmt (&iter, gtry);
    8365      1289288 :               iter = gsi_start (gtry->eval);
    8366              :             }
    8367              :         }
    8368              :       else
    8369     16705726 :         gsi_next (&iter);
    8370              :     }
    8371              : 
    8372      5436501 :   gimplify_seq_add_seq (pre_p, body_sequence);
    8373      5436501 :   if (temp)
    8374              :     {
    8375              :       *expr_p = temp;
    8376              :       return GS_OK;
    8377              :     }
    8378              :   else
    8379              :     {
    8380      5080795 :       *expr_p = NULL;
    8381      5080795 :       return GS_ALL_DONE;
    8382              :     }
    8383              : }
    8384              : 
    8385              : /* Insert a cleanup marker for gimplify_cleanup_point_expr.  CLEANUP
    8386              :    is the cleanup action required.  EH_ONLY is true if the cleanup should
    8387              :    only be executed if an exception is thrown, not on normal exit.
    8388              :    If FORCE_UNCOND is true perform the cleanup unconditionally;  this is
    8389              :    only valid for clobbers.  */
    8390              : 
    8391              : static void
    8392       652900 : gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
    8393              :                      bool force_uncond = false)
    8394              : {
    8395       652900 :   gimple *wce;
    8396       652900 :   gimple_seq cleanup_stmts = NULL;
    8397              : 
    8398              :   /* Errors can result in improperly nested cleanups.  Which results in
    8399              :      confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR.  */
    8400       652900 :   if (seen_error ())
    8401         5123 :     return;
    8402              : 
    8403       647777 :   if (gimple_conditional_context ())
    8404              :     {
    8405              :       /* If we're in a conditional context, this is more complex.  We only
    8406              :          want to run the cleanup if we actually ran the initialization that
    8407              :          necessitates it, but we want to run it after the end of the
    8408              :          conditional context.  So we wrap the try/finally around the
    8409              :          condition and use a flag to determine whether or not to actually
    8410              :          run the destructor.  Thus
    8411              : 
    8412              :            test ? f(A()) : 0
    8413              : 
    8414              :          becomes (approximately)
    8415              : 
    8416              :            flag = 0;
    8417              :            try {
    8418              :              if (test) { A::A(temp); flag = 1; val = f(temp); }
    8419              :              else { val = 0; }
    8420              :            } finally {
    8421              :              if (flag) A::~A(temp);
    8422              :            }
    8423              :            val
    8424              :       */
    8425        15474 :       if (force_uncond)
    8426              :         {
    8427        14604 :           gimplify_stmt (&cleanup, &cleanup_stmts);
    8428        14604 :           wce = gimple_build_wce (cleanup_stmts);
    8429        14604 :           gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
    8430              :         }
    8431              :       else
    8432              :         {
    8433          870 :           tree flag = create_tmp_var (boolean_type_node, "cleanup");
    8434          870 :           gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
    8435          870 :           gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
    8436              : 
    8437          870 :           cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
    8438          870 :           gimplify_stmt (&cleanup, &cleanup_stmts);
    8439          870 :           wce = gimple_build_wce (cleanup_stmts);
    8440          870 :           gimple_wce_set_cleanup_eh_only (wce, eh_only);
    8441              : 
    8442          870 :           gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
    8443          870 :           gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
    8444          870 :           gimplify_seq_add_stmt (pre_p, ftrue);
    8445              : 
    8446              :           /* Because of this manipulation, and the EH edges that jump
    8447              :              threading cannot redirect, the temporary (VAR) will appear
    8448              :              to be used uninitialized.  Don't warn.  */
    8449          870 :           suppress_warning (var, OPT_Wuninitialized);
    8450              :         }
    8451              :     }
    8452              :   else
    8453              :     {
    8454       632303 :       gimplify_stmt (&cleanup, &cleanup_stmts);
    8455       632303 :       wce = gimple_build_wce (cleanup_stmts);
    8456       632303 :       gimple_wce_set_cleanup_eh_only (wce, eh_only);
    8457       632303 :       gimplify_seq_add_stmt (pre_p, wce);
    8458              :     }
    8459              : }
    8460              : 
    8461              : /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR.  */
    8462              : 
    8463              : static enum gimplify_status
    8464       867101 : gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
    8465              : {
    8466       867101 :   tree targ = *expr_p;
    8467       867101 :   tree temp = TARGET_EXPR_SLOT (targ);
    8468       867101 :   tree init = TARGET_EXPR_INITIAL (targ);
    8469       867101 :   enum gimplify_status ret;
    8470              : 
    8471       867101 :   bool unpoison_empty_seq = false;
    8472       867101 :   gimple_stmt_iterator unpoison_it;
    8473              : 
    8474       867101 :   if (init)
    8475              :     {
    8476       832798 :       gimple_seq init_pre_p = NULL;
    8477       832798 :       bool is_vla = false;
    8478              : 
    8479              :       /* TARGET_EXPR temps aren't part of the enclosing block, so add it
    8480              :          to the temps list.  Handle also variable length TARGET_EXPRs.  */
    8481       832798 :       if (!poly_int_tree_p (DECL_SIZE (temp)))
    8482              :         {
    8483           44 :           if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
    8484            1 :             gimplify_type_sizes (TREE_TYPE (temp), &init_pre_p);
    8485              :           /* FIXME: this is correct only when the size of the type does
    8486              :              not depend on expressions evaluated in init.  */
    8487           44 :           gimplify_vla_decl (temp, &init_pre_p);
    8488           44 :           is_vla = true;
    8489              :         }
    8490              :       else
    8491              :         {
    8492              :           /* Save location where we need to place unpoisoning.  It's possible
    8493              :              that a variable will be converted to needs_to_live_in_memory.  */
    8494       832754 :           unpoison_it = gsi_last (*pre_p);
    8495       832754 :           unpoison_empty_seq = gsi_end_p (unpoison_it);
    8496              : 
    8497       832754 :           gimple_add_tmp_var (temp);
    8498              :         }
    8499              : 
    8500       832798 :       if (var_needs_auto_init_p (temp) && VOID_TYPE_P (TREE_TYPE (init)))
    8501              :         {
    8502        60054 :           gimple_add_init_for_auto_var (temp, flag_auto_var_init, &init_pre_p);
    8503        60054 :           if ((flag_auto_var_init & ~AUTO_INIT_CXX26) == AUTO_INIT_PATTERN
    8504            0 :               && !is_gimple_reg (temp)
    8505        60054 :               && clear_padding_type_may_have_padding_p (TREE_TYPE (temp)))
    8506            0 :             gimple_add_padding_init_for_auto_var (temp, is_vla, &init_pre_p);
    8507              :         }
    8508              : 
    8509              :       /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
    8510              :          expression is supposed to initialize the slot.  */
    8511       832798 :       if (VOID_TYPE_P (TREE_TYPE (init)))
    8512       293266 :         ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
    8513              :                              fb_none);
    8514              :       else
    8515              :         {
    8516       539532 :           tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
    8517       539532 :           init = init_expr;
    8518       539532 :           ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
    8519              :                                fb_none);
    8520       539532 :           init = NULL;
    8521       539532 :           ggc_free (init_expr);
    8522              :         }
    8523       832798 :       if (ret == GS_ERROR)
    8524              :         {
    8525              :           /* PR c++/28266 Make sure this is expanded only once. */
    8526           10 :           TARGET_EXPR_INITIAL (targ) = NULL_TREE;
    8527           10 :           return GS_ERROR;
    8528              :         }
    8529              : 
    8530       832788 :       if (init)
    8531            0 :         gimplify_and_add (init, &init_pre_p);
    8532              : 
    8533              :       /* Add a clobber for the temporary going out of scope, like
    8534              :          gimplify_bind_expr.  But only if we did not promote the
    8535              :          temporary to static storage.  */
    8536       832788 :       if (gimplify_ctxp->in_cleanup_point_expr
    8537       688498 :           && !TREE_STATIC (temp)
    8538      1519136 :           && needs_to_live_in_memory (temp))
    8539              :         {
    8540       500140 :           if (flag_stack_reuse == SR_ALL)
    8541              :             {
    8542       499660 :               tree clobber = build_clobber (TREE_TYPE (temp),
    8543              :                                             CLOBBER_STORAGE_END);
    8544       499660 :               clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
    8545       499660 :               gimple_push_cleanup (temp, clobber, false, pre_p, true);
    8546              :             }
    8547       500140 :           if (asan_poisoned_variables
    8548          480 :               && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
    8549          480 :               && !TREE_STATIC (temp)
    8550          480 :               && dbg_cnt (asan_use_after_scope)
    8551       500620 :               && !gimplify_omp_ctxp)
    8552              :             {
    8553          472 :               tree asan_cleanup = build_asan_poison_call_expr (temp);
    8554          472 :               if (asan_cleanup)
    8555              :                 {
    8556          472 :                   if (unpoison_empty_seq)
    8557          217 :                     unpoison_it = gsi_start (*pre_p);
    8558              : 
    8559          472 :                   asan_poison_variable (temp, false, &unpoison_it,
    8560              :                                         unpoison_empty_seq);
    8561          472 :                   gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
    8562              :                 }
    8563              :             }
    8564              :         }
    8565              : 
    8566       832788 :       gimple_seq_add_seq (pre_p, init_pre_p);
    8567              : 
    8568              :       /* If needed, push the cleanup for the temp.  */
    8569       832788 :       if (TARGET_EXPR_CLEANUP (targ))
    8570       152768 :         gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
    8571       152768 :                              CLEANUP_EH_ONLY (targ), pre_p);
    8572              : 
    8573              :       /* Only expand this once.  */
    8574       832788 :       TREE_OPERAND (targ, 3) = init;
    8575       832788 :       TARGET_EXPR_INITIAL (targ) = NULL_TREE;
    8576              :     }
    8577              :   else
    8578              :     /* We should have expanded this before.  */
    8579        34303 :     gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
    8580              : 
    8581       867091 :   *expr_p = temp;
    8582       867091 :   return GS_OK;
    8583              : }
    8584              : 
    8585              : /* Gimplification of expression trees.  */
    8586              : 
    8587              : /* Gimplify an expression which appears at statement context.  The
    8588              :    corresponding GIMPLE statements are added to *SEQ_P.  If *SEQ_P is
    8589              :    NULL, a new sequence is allocated.
    8590              : 
    8591              :    Return true if we actually added a statement to the queue.  */
    8592              : 
    8593              : bool
    8594    105197100 : gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
    8595              : {
    8596    105197100 :   gimple_seq_node last;
    8597              : 
    8598    105197100 :   last = gimple_seq_last (*seq_p);
    8599    105197100 :   gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
    8600    105197100 :   return last != gimple_seq_last (*seq_p);
    8601              : }
    8602              : 
    8603              : /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
    8604              :    to CTX.  If entries already exist, force them to be some flavor of private.
    8605              :    If there is no enclosing parallel, do nothing.  */
    8606              : 
    8607              : void
    8608       127816 : omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
    8609              : {
    8610       127816 :   splay_tree_node n;
    8611              : 
    8612       127816 :   if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
    8613              :     return;
    8614              : 
    8615        37060 :   do
    8616              :     {
    8617        37060 :       n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
    8618        37060 :       if (n != NULL)
    8619              :         {
    8620         4281 :           if (n->value & GOVD_SHARED)
    8621          157 :             n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
    8622         4124 :           else if (n->value & GOVD_MAP)
    8623           58 :             n->value |= GOVD_MAP_TO_ONLY;
    8624              :           else
    8625              :             return;
    8626              :         }
    8627        32779 :       else if ((ctx->region_type & ORT_TARGET) != 0)
    8628              :         {
    8629         7072 :           if (ctx->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
    8630         4288 :             omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
    8631              :           else
    8632         2784 :             omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
    8633              :         }
    8634        25707 :       else if (ctx->region_type != ORT_WORKSHARE
    8635              :                && ctx->region_type != ORT_TASKGROUP
    8636        23878 :                && ctx->region_type != ORT_SIMD
    8637        23254 :                && ctx->region_type != ORT_ACC
    8638        23137 :                && !(ctx->region_type & ORT_TARGET_DATA))
    8639        14556 :         omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
    8640              : 
    8641        32994 :       ctx = ctx->outer_context;
    8642              :     }
    8643        32994 :   while (ctx);
    8644              : }
    8645              : 
    8646              : /* Similarly for each of the type sizes of TYPE.  */
    8647              : 
    8648              : static void
    8649        40822 : omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
    8650              : {
    8651        40822 :   if (type == NULL || type == error_mark_node)
    8652              :     return;
    8653        40818 :   type = TYPE_MAIN_VARIANT (type);
    8654              : 
    8655        40818 :   if (ctx->privatized_types->add (type))
    8656              :     return;
    8657              : 
    8658        31782 :   switch (TREE_CODE (type))
    8659              :     {
    8660        13775 :     case INTEGER_TYPE:
    8661        13775 :     case ENUMERAL_TYPE:
    8662        13775 :     case BOOLEAN_TYPE:
    8663        13775 :     case REAL_TYPE:
    8664        13775 :     case FIXED_POINT_TYPE:
    8665        13775 :       omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
    8666        13775 :       omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
    8667        13775 :       break;
    8668              : 
    8669         5455 :     case ARRAY_TYPE:
    8670         5455 :       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
    8671         5455 :       omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
    8672         5455 :       break;
    8673              : 
    8674         2524 :     case RECORD_TYPE:
    8675         2524 :     case UNION_TYPE:
    8676         2524 :     case QUAL_UNION_TYPE:
    8677         2524 :       {
    8678         2524 :         tree field;
    8679        26867 :         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    8680        24343 :           if (TREE_CODE (field) == FIELD_DECL)
    8681              :             {
    8682         9069 :               omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
    8683         9069 :               omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
    8684              :             }
    8685              :       }
    8686              :       break;
    8687              : 
    8688         9364 :     case POINTER_TYPE:
    8689         9364 :     case REFERENCE_TYPE:
    8690         9364 :       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
    8691         9364 :       break;
    8692              : 
    8693              :     default:
    8694              :       break;
    8695              :     }
    8696              : 
    8697        31782 :   omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
    8698        31782 :   omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
    8699        31782 :   lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
    8700              : }
    8701              : 
    8702              : /* Add an entry for DECL in the OMP context CTX with FLAGS.  */
    8703              : 
    8704              : static void
    8705       746965 : omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
    8706              : {
    8707       746965 :   splay_tree_node n;
    8708       746965 :   unsigned int nflags;
    8709       746965 :   tree t;
    8710              : 
    8711       746965 :   if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
    8712              :     return;
    8713              : 
    8714              :   /* Never elide decls whose type has TREE_ADDRESSABLE set.  This means
    8715              :      there are constructors involved somewhere.  Exception is a shared clause,
    8716              :      there is nothing privatized in that case.  */
    8717       746817 :   if ((flags & GOVD_SHARED) == 0
    8718       746817 :       && (TREE_ADDRESSABLE (TREE_TYPE (decl))
    8719       683981 :           || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
    8720         7806 :     flags |= GOVD_SEEN;
    8721              : 
    8722       746817 :   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
    8723       746817 :   if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
    8724              :     {
    8725              :       /* We shouldn't be re-adding the decl with the same data
    8726              :          sharing class.  */
    8727          828 :       gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
    8728          828 :       nflags = n->value | flags;
    8729              :       /* The only combination of data sharing classes we should see is
    8730              :          FIRSTPRIVATE and LASTPRIVATE.  However, OpenACC permits
    8731              :          reduction variables to be used in data sharing clauses.  */
    8732          828 :       gcc_assert ((ctx->region_type & ORT_ACC) != 0
    8733              :                   || ((nflags & GOVD_DATA_SHARE_CLASS)
    8734              :                       == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
    8735              :                   || (flags & GOVD_DATA_SHARE_CLASS) == 0);
    8736          828 :       n->value = nflags;
    8737          828 :       return;
    8738              :     }
    8739              : 
    8740              :   /* When adding a variable-sized variable, we have to handle all sorts
    8741              :      of additional bits of data: the pointer replacement variable, and
    8742              :      the parameters of the type.  */
    8743       745989 :   if (DECL_SIZE (decl) && !poly_int_tree_p (DECL_SIZE (decl)))
    8744              :     {
    8745              :       /* Add the pointer replacement variable as PRIVATE if the variable
    8746              :          replacement is private, else FIRSTPRIVATE since we'll need the
    8747              :          address of the original variable either for SHARED, or for the
    8748              :          copy into or out of the context.  */
    8749         1342 :       if (!(flags & GOVD_LOCAL) && ctx->region_type != ORT_TASKGROUP)
    8750              :         {
    8751         1249 :           if (flags & GOVD_MAP)
    8752              :             nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
    8753          540 :           else if (flags & GOVD_PRIVATE)
    8754              :             nflags = GOVD_PRIVATE;
    8755          450 :           else if (((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
    8756           54 :                     && (flags & GOVD_FIRSTPRIVATE))
    8757          436 :                    || (ctx->region_type == ORT_TARGET_DATA
    8758            6 :                        && (flags & GOVD_DATA_SHARE_CLASS) == 0))
    8759              :             nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
    8760              :           else
    8761         1249 :             nflags = GOVD_FIRSTPRIVATE;
    8762         1249 :           nflags |= flags & GOVD_SEEN;
    8763         1249 :           t = DECL_VALUE_EXPR (decl);
    8764         1249 :           gcc_assert (INDIRECT_REF_P (t));
    8765         1249 :           t = TREE_OPERAND (t, 0);
    8766         1249 :           gcc_assert (DECL_P (t));
    8767         1249 :           omp_add_variable (ctx, t, nflags);
    8768              :         }
    8769              : 
    8770              :       /* Add all of the variable and type parameters (which should have
    8771              :          been gimplified to a formal temporary) as FIRSTPRIVATE.  */
    8772         1342 :       omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
    8773         1342 :       omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
    8774         1342 :       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
    8775              : 
    8776              :       /* The variable-sized variable itself is never SHARED, only some form
    8777              :          of PRIVATE.  The sharing would take place via the pointer variable
    8778              :          which we remapped above.  */
    8779         1342 :       if (flags & GOVD_SHARED)
    8780          285 :         flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
    8781          285 :                 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
    8782              : 
    8783              :       /* We're going to make use of the TYPE_SIZE_UNIT at least in the
    8784              :          alloca statement we generate for the variable, so make sure it
    8785              :          is available.  This isn't automatically needed for the SHARED
    8786              :          case, since we won't be allocating local storage then.
    8787              :          For local variables TYPE_SIZE_UNIT might not be gimplified yet,
    8788              :          in this case omp_notice_variable will be called later
    8789              :          on when it is gimplified.  */
    8790         1057 :       else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
    8791         1057 :                && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
    8792          283 :         omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
    8793              :     }
    8794       744647 :   else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
    8795       744647 :            && omp_privatize_by_reference (decl))
    8796              :     {
    8797        10137 :       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
    8798              : 
    8799              :       /* Similar to the direct variable sized case above, we'll need the
    8800              :          size of references being privatized.  */
    8801        10137 :       if ((flags & GOVD_SHARED) == 0)
    8802              :         {
    8803         6346 :           t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
    8804         6346 :           if (t && DECL_P (t))
    8805         1401 :             omp_notice_variable (ctx, t, true);
    8806              :         }
    8807              :     }
    8808              : 
    8809       745989 :   if (n != NULL)
    8810         1874 :     n->value |= flags;
    8811              :   else
    8812       744115 :     splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
    8813              : 
    8814              :   /* For reductions clauses in OpenACC loop directives, by default create a
    8815              :      copy clause on the enclosing parallel construct for carrying back the
    8816              :      results.  */
    8817       745989 :   if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
    8818              :     {
    8819         4716 :       struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
    8820         6454 :       while (outer_ctx)
    8821              :         {
    8822         5788 :           n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
    8823         5788 :           if (n != NULL)
    8824              :             {
    8825              :               /* Ignore local variables and explicitly declared clauses.  */
    8826         3990 :               if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
    8827              :                 break;
    8828          443 :               else if (outer_ctx->region_type == ORT_ACC_KERNELS)
    8829              :                 {
    8830              :                   /* According to the OpenACC spec, such a reduction variable
    8831              :                      should already have a copy map on a kernels construct,
    8832              :                      verify that here.  */
    8833          143 :                   gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
    8834              :                               && (n->value & GOVD_MAP));
    8835              :                 }
    8836          300 :               else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
    8837              :                 {
    8838              :                   /* Remove firstprivate and make it a copy map.  */
    8839          150 :                   n->value &= ~GOVD_FIRSTPRIVATE;
    8840          150 :                   n->value |= GOVD_MAP;
    8841              :                 }
    8842              :             }
    8843         1798 :           else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
    8844              :             {
    8845          503 :               splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
    8846              :                                  GOVD_MAP | GOVD_SEEN);
    8847          503 :               break;
    8848              :             }
    8849         1738 :           outer_ctx = outer_ctx->outer_context;
    8850              :         }
    8851              :     }
    8852              : }
    8853              : 
    8854              : /* Notice a threadprivate variable DECL used in OMP context CTX.
    8855              :    This just prints out diagnostics about threadprivate variable uses
    8856              :    in untied tasks.  If DECL2 is non-NULL, prevent this warning
    8857              :    on that variable.  */
    8858              : 
    8859              : static bool
    8860        14183 : omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
    8861              :                                    tree decl2)
    8862              : {
    8863        14183 :   splay_tree_node n;
    8864        14183 :   struct gimplify_omp_ctx *octx;
    8865              : 
    8866        28798 :   for (octx = ctx; octx; octx = octx->outer_context)
    8867        14615 :     if ((octx->region_type & ORT_TARGET) != 0
    8868        14615 :         || octx->order_concurrent)
    8869              :       {
    8870          112 :         n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
    8871          112 :         if (n == NULL)
    8872              :           {
    8873           20 :             if (octx->order_concurrent)
    8874              :               {
    8875           20 :                 error ("threadprivate variable %qE used in a region with"
    8876           20 :                        " %<order(concurrent)%> clause", DECL_NAME (decl));
    8877           20 :                 inform (octx->location, "enclosing region");
    8878              :               }
    8879              :             else
    8880              :               {
    8881            0 :                 error ("threadprivate variable %qE used in target region",
    8882            0 :                        DECL_NAME (decl));
    8883            0 :                 inform (octx->location, "enclosing target region");
    8884              :               }
    8885           20 :             splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
    8886              :           }
    8887          112 :         if (decl2)
    8888            0 :           splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
    8889              :       }
    8890              : 
    8891        14183 :   if (ctx->region_type != ORT_UNTIED_TASK)
    8892              :     return false;
    8893           37 :   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
    8894           37 :   if (n == NULL)
    8895              :     {
    8896            6 :       error ("threadprivate variable %qE used in untied task",
    8897            6 :              DECL_NAME (decl));
    8898            6 :       inform (ctx->location, "enclosing task");
    8899            6 :       splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
    8900              :     }
    8901           37 :   if (decl2)
    8902            4 :     splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
    8903              :   return false;
    8904              : }
    8905              : 
    8906              : /* Return true if global var DECL is device resident.  */
    8907              : 
    8908              : static bool
    8909          642 : device_resident_p (tree decl)
    8910              : {
    8911          642 :   tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
    8912              : 
    8913          642 :   if (!attr)
    8914              :     return false;
    8915              : 
    8916            0 :   for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
    8917              :     {
    8918            0 :       tree c = TREE_VALUE (t);
    8919            0 :       if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
    8920              :         return true;
    8921              :     }
    8922              : 
    8923              :   return false;
    8924              : }
    8925              : 
    8926              : /* Return true if DECL has an ACC DECLARE attribute.  */
    8927              : 
    8928              : static bool
    8929         9101 : is_oacc_declared (tree decl)
    8930              : {
    8931         9101 :   tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
    8932         9101 :   tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
    8933         9101 :   return declared != NULL_TREE;
    8934              : }
    8935              : 
    8936              : /* Determine outer default flags for DECL mentioned in an OMP region
    8937              :    but not declared in an enclosing clause.
    8938              : 
    8939              :    ??? Some compiler-generated variables (like SAVE_EXPRs) could be
    8940              :    remapped firstprivate instead of shared.  To some extent this is
    8941              :    addressed in omp_firstprivatize_type_sizes, but not
    8942              :    effectively.  */
    8943              : 
    8944              : static unsigned
    8945        49875 : omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
    8946              :                     bool in_code, unsigned flags)
    8947              : {
    8948        49875 :   enum omp_clause_default_kind default_kind = ctx->default_kind;
    8949        49875 :   enum omp_clause_default_kind kind;
    8950              : 
    8951        49875 :   kind = lang_hooks.decls.omp_predetermined_sharing (decl);
    8952        49875 :   if (ctx->region_type & ORT_TASK)
    8953              :     {
    8954         3998 :       tree detach_clause = omp_find_clause (ctx->clauses, OMP_CLAUSE_DETACH);
    8955              : 
    8956              :       /* The event-handle specified by a detach clause should always be firstprivate,
    8957              :          regardless of the current default.  */
    8958         4210 :       if (detach_clause && OMP_CLAUSE_DECL (detach_clause) == decl)
    8959              :         kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
    8960              :     }
    8961        49875 :   if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
    8962              :     default_kind = kind;
    8963        46864 :   else if (VAR_P (decl) && TREE_STATIC (decl) && DECL_IN_CONSTANT_POOL (decl))
    8964              :     default_kind = OMP_CLAUSE_DEFAULT_SHARED;
    8965              :   /* For C/C++ default({,first}private), variables with static storage duration
    8966              :      declared in a namespace or global scope and referenced in construct
    8967              :      must be explicitly specified, i.e. acts as default(none).  */
    8968        46863 :   else if ((default_kind == OMP_CLAUSE_DEFAULT_PRIVATE
    8969        46863 :             || default_kind == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
    8970          379 :            && VAR_P (decl)
    8971          336 :            && is_global_var (decl)
    8972          184 :            && (DECL_FILE_SCOPE_P (decl)
    8973          104 :                || (DECL_CONTEXT (decl)
    8974          104 :                    && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL))
    8975        46991 :            && !lang_GNU_Fortran ())
    8976              :     default_kind = OMP_CLAUSE_DEFAULT_NONE;
    8977              : 
    8978        49746 :   switch (default_kind)
    8979              :     {
    8980          267 :     case OMP_CLAUSE_DEFAULT_NONE:
    8981          267 :       {
    8982          267 :         const char *rtype;
    8983              : 
    8984          267 :         if (ctx->region_type & ORT_PARALLEL)
    8985              :           rtype = "parallel";
    8986          108 :         else if ((ctx->region_type & ORT_TASKLOOP) == ORT_TASKLOOP)
    8987              :           rtype = "taskloop";
    8988           72 :         else if (ctx->region_type & ORT_TASK)
    8989              :           rtype = "task";
    8990           36 :         else if (ctx->region_type & ORT_TEAMS)
    8991              :           rtype = "teams";
    8992              :         else
    8993            0 :           gcc_unreachable ();
    8994              : 
    8995          267 :         error ("%qE not specified in enclosing %qs",
    8996          267 :                DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
    8997          267 :         inform (ctx->location, "enclosing %qs", rtype);
    8998              :       }
    8999              :       /* FALLTHRU */
    9000        46206 :     case OMP_CLAUSE_DEFAULT_SHARED:
    9001        46206 :       flags |= GOVD_SHARED;
    9002        46206 :       break;
    9003          155 :     case OMP_CLAUSE_DEFAULT_PRIVATE:
    9004          155 :       flags |= GOVD_PRIVATE;
    9005          155 :       break;
    9006          288 :     case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
    9007          288 :       flags |= GOVD_FIRSTPRIVATE;
    9008          288 :       break;
    9009         3226 :     case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
    9010              :       /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED.  */
    9011         3226 :       gcc_assert ((ctx->region_type & ORT_TASK) != 0);
    9012         3226 :       if (struct gimplify_omp_ctx *octx = ctx->outer_context)
    9013              :         {
    9014         2166 :           omp_notice_variable (octx, decl, in_code);
    9015         5164 :           for (; octx; octx = octx->outer_context)
    9016              :             {
    9017         2942 :               splay_tree_node n2;
    9018              : 
    9019         2942 :               n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
    9020         2942 :               if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
    9021            5 :                   && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
    9022            5 :                 continue;
    9023         2937 :               if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
    9024              :                 {
    9025          898 :                   flags |= GOVD_FIRSTPRIVATE;
    9026          898 :                   goto found_outer;
    9027              :                 }
    9028         2039 :               if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
    9029              :                 {
    9030         1212 :                   flags |= GOVD_SHARED;
    9031         1212 :                   goto found_outer;
    9032              :                 }
    9033              :             }
    9034              :         }
    9035              : 
    9036         1116 :       if (TREE_CODE (decl) == PARM_DECL
    9037         1116 :           || (!is_global_var (decl)
    9038          295 :               && DECL_CONTEXT (decl) == current_function_decl))
    9039          736 :         flags |= GOVD_FIRSTPRIVATE;
    9040              :       else
    9041          380 :         flags |= GOVD_SHARED;
    9042        49875 :     found_outer:
    9043              :       break;
    9044              : 
    9045            0 :     default:
    9046            0 :       gcc_unreachable ();
    9047              :     }
    9048              : 
    9049        49875 :   return flags;
    9050              : }
    9051              : 
    9052              : /* Return string name for types of OpenACC constructs from ORT_* values.  */
    9053              : 
    9054              : static const char *
    9055          570 : oacc_region_type_name (enum omp_region_type region_type)
    9056              : {
    9057          570 :   switch (region_type)
    9058              :     {
    9059              :     case ORT_ACC_DATA:
    9060              :       return "data";
    9061          140 :     case ORT_ACC_PARALLEL:
    9062          140 :       return "parallel";
    9063          140 :     case ORT_ACC_KERNELS:
    9064          140 :       return "kernels";
    9065          140 :     case ORT_ACC_SERIAL:
    9066          140 :       return "serial";
    9067            0 :     default:
    9068            0 :       gcc_unreachable ();
    9069              :     }
    9070              : }
    9071              : 
    9072              : /* Determine outer default flags for DECL mentioned in an OACC region
    9073              :    but not declared in an enclosing clause.  */
    9074              : 
    9075              : static unsigned
    9076         8839 : oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
    9077              : {
    9078         8839 :   struct gimplify_omp_ctx *ctx_default = ctx;
    9079              :   /* If no 'default' clause appears on this compute construct...  */
    9080         8839 :   if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED)
    9081              :     {
    9082              :       /* ..., see if one appears on a lexically containing 'data'
    9083              :          construct.  */
    9084         9618 :       while ((ctx_default = ctx_default->outer_context))
    9085              :         {
    9086         1534 :           if (ctx_default->region_type == ORT_ACC_DATA
    9087         1534 :               && ctx_default->default_kind != OMP_CLAUSE_DEFAULT_SHARED)
    9088              :             break;
    9089              :         }
    9090              :       /* If not, reset.  */
    9091         8260 :       if (!ctx_default)
    9092         8663 :         ctx_default = ctx;
    9093              :     }
    9094              : 
    9095         8839 :   bool on_device = false;
    9096         8839 :   bool is_private = false;
    9097         8839 :   bool declared = is_oacc_declared (decl);
    9098         8839 :   tree type = TREE_TYPE (decl);
    9099              : 
    9100         8839 :   if (omp_privatize_by_reference (decl))
    9101          338 :     type = TREE_TYPE (type);
    9102              : 
    9103              :   /* For Fortran COMMON blocks, only used variables in those blocks are
    9104              :      transferred and remapped.  The block itself will have a private clause to
    9105              :      avoid transferring the data twice.
    9106              :      The hook evaluates to false by default.  For a variable in Fortran's COMMON
    9107              :      or EQUIVALENCE block, returns 'true' (as we have shared=false) - as only
    9108              :      the variables in such a COMMON/EQUIVALENCE block shall be privatized not
    9109              :      the whole block.  For C++ and Fortran, it can also be true under certain
    9110              :      other conditions, if DECL_HAS_VALUE_EXPR.  */
    9111         8839 :   if (RECORD_OR_UNION_TYPE_P (type))
    9112          983 :     is_private = lang_hooks.decls.omp_disregard_value_expr (decl, false);
    9113              : 
    9114         8839 :   if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
    9115         8839 :       && is_global_var (decl)
    9116          642 :       && device_resident_p (decl)
    9117         8839 :       && !is_private)
    9118              :     {
    9119            0 :       on_device = true;
    9120            0 :       flags |= GOVD_MAP_TO_ONLY;
    9121              :     }
    9122              : 
    9123         8839 :   switch (ctx->region_type)
    9124              :     {
    9125         1460 :     case ORT_ACC_KERNELS:
    9126         1460 :       if (is_private)
    9127            0 :         flags |= GOVD_FIRSTPRIVATE;
    9128         1460 :       else if (AGGREGATE_TYPE_P (type))
    9129              :         {
    9130              :           /* Aggregates default to 'present_or_copy', or 'present'.  */
    9131          415 :           if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
    9132          397 :             flags |= GOVD_MAP;
    9133              :           else
    9134           18 :             flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
    9135              :         }
    9136              :       else
    9137              :         /* Scalars default to 'copy'.  */
    9138         1045 :         flags |= GOVD_MAP | GOVD_MAP_FORCE;
    9139              : 
    9140              :       break;
    9141              : 
    9142         7379 :     case ORT_ACC_PARALLEL:
    9143         7379 :     case ORT_ACC_SERIAL:
    9144         7379 :       if (is_private)
    9145            0 :         flags |= GOVD_FIRSTPRIVATE;
    9146         7379 :       else if (on_device || declared)
    9147           17 :         flags |= GOVD_MAP;
    9148         7362 :       else if (AGGREGATE_TYPE_P (type))
    9149              :         {
    9150              :           /* Aggregates default to 'present_or_copy', or 'present'.  */
    9151         3697 :           if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
    9152         3431 :             flags |= GOVD_MAP;
    9153              :           else
    9154          266 :             flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
    9155              :         }
    9156              :       else
    9157              :         /* Scalars default to 'firstprivate'.  */
    9158         3665 :         flags |= GOVD_FIRSTPRIVATE;
    9159              : 
    9160              :       break;
    9161              : 
    9162            0 :     default:
    9163            0 :       gcc_unreachable ();
    9164              :     }
    9165              : 
    9166         8839 :   if (DECL_ARTIFICIAL (decl))
    9167              :     ; /* We can get compiler-generated decls, and should not complain
    9168              :          about them.  */
    9169         8528 :   else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_NONE)
    9170              :     {
    9171          420 :       error ("%qE not specified in enclosing OpenACC %qs construct",
    9172          210 :              DECL_NAME (lang_hooks.decls.omp_report_decl (decl)),
    9173              :              oacc_region_type_name (ctx->region_type));
    9174          210 :       if (ctx_default != ctx)
    9175          150 :         inform (ctx->location, "enclosing OpenACC %qs construct and",
    9176              :                 oacc_region_type_name (ctx->region_type));
    9177          210 :       inform (ctx_default->location,
    9178              :               "enclosing OpenACC %qs construct with %qs clause",
    9179              :               oacc_region_type_name (ctx_default->region_type),
    9180              :               "default(none)");
    9181              :     }
    9182         8318 :   else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
    9183              :     ; /* Handled above.  */
    9184              :   else
    9185         7785 :     gcc_checking_assert (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
    9186              : 
    9187         8839 :   return flags;
    9188              : }
    9189              : 
    9190              : /* Record the fact that DECL was used within the OMP context CTX.
    9191              :    IN_CODE is true when real code uses DECL, and false when we should
    9192              :    merely emit default(none) errors.  Return true if DECL is going to
    9193              :    be remapped and thus DECL shouldn't be gimplified into its
    9194              :    DECL_VALUE_EXPR (if any).  */
    9195              : 
    9196              : static bool
    9197      4055667 : omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
    9198              : {
    9199      4055667 :   splay_tree_node n;
    9200      4055667 :   unsigned flags = in_code ? GOVD_SEEN : 0;
    9201      4055667 :   bool ret = false, shared;
    9202              : 
    9203      4055667 :   if (error_operand_p (decl))
    9204              :     return false;
    9205              : 
    9206      4055667 :   if (DECL_ARTIFICIAL (decl))
    9207              :     {
    9208      2330157 :       tree attr = lookup_attribute ("omp allocate var", DECL_ATTRIBUTES (decl));
    9209      2330157 :       if (attr)
    9210         1053 :         decl = TREE_VALUE (TREE_VALUE (attr));
    9211              :     }
    9212              : 
    9213      4055667 :   if (ctx->region_type == ORT_NONE)
    9214          248 :     return lang_hooks.decls.omp_disregard_value_expr (decl, false);
    9215              : 
    9216      4055419 :   if (is_global_var (decl))
    9217              :     {
    9218              :       /* Threadprivate variables are predetermined.  */
    9219       517232 :       if (DECL_THREAD_LOCAL_P (decl))
    9220        13565 :         return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
    9221              : 
    9222       503667 :       if (DECL_HAS_VALUE_EXPR_P (decl))
    9223              :         {
    9224         4308 :           if (ctx->region_type & ORT_ACC)
    9225              :             /* For OpenACC, defer expansion of value to avoid transferring
    9226              :                privatized common block data instead of im-/explicitly
    9227              :                transferred variables which are in common blocks.  */
    9228              :             ;
    9229              :           else
    9230              :             {
    9231         2104 :               tree value = get_base_address (DECL_VALUE_EXPR (decl));
    9232              : 
    9233         2104 :               if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
    9234          618 :                 return omp_notice_threadprivate_variable (ctx, decl, value);
    9235              :             }
    9236              :         }
    9237              : 
    9238       503049 :       if (gimplify_omp_ctxp->outer_context == NULL
    9239       114908 :           && VAR_P (decl)
    9240       617957 :           && oacc_get_fn_attrib (current_function_decl))
    9241              :         {
    9242          765 :           location_t loc = DECL_SOURCE_LOCATION (decl);
    9243              : 
    9244          765 :           if (lookup_attribute ("omp declare target link",
    9245          765 :                                 DECL_ATTRIBUTES (decl)))
    9246              :             {
    9247           48 :               error_at (loc,
    9248              :                         "%qE with %<link%> clause used in %<routine%> function",
    9249           24 :                         DECL_NAME (decl));
    9250           24 :               return false;
    9251              :             }
    9252          741 :           else if (!lookup_attribute ("omp declare target",
    9253          741 :                                       DECL_ATTRIBUTES (decl)))
    9254              :             {
    9255          132 :               error_at (loc,
    9256              :                         "%qE requires a %<declare%> directive for use "
    9257           66 :                         "in a %<routine%> function", DECL_NAME (decl));
    9258           66 :               return false;
    9259              :             }
    9260              :         }
    9261              :     }
    9262              : 
    9263      4041146 :   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
    9264      4041146 :   if ((ctx->region_type & ORT_TARGET) != 0)
    9265              :     {
    9266       866235 :       if (n == NULL)
    9267              :         {
    9268        46121 :           unsigned nflags = flags;
    9269        46121 :           if ((ctx->region_type & ORT_ACC) == 0)
    9270              :             {
    9271        35167 :               bool is_declare_target = false;
    9272        35167 :               if (is_global_var (decl)
    9273        35167 :                   && varpool_node::get_create (decl)->offloadable)
    9274              :                 {
    9275         7970 :                   struct gimplify_omp_ctx *octx;
    9276         7970 :                   for (octx = ctx->outer_context;
    9277         7978 :                        octx; octx = octx->outer_context)
    9278              :                     {
    9279            9 :                       n = splay_tree_lookup (octx->variables,
    9280              :                                              (splay_tree_key)decl);
    9281            9 :                       if (n
    9282            9 :                           && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
    9283            9 :                           && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
    9284              :                         break;
    9285              :                     }
    9286         7970 :                   is_declare_target = octx == NULL;
    9287              :                 }
    9288         7970 :               if (!is_declare_target)
    9289              :                 {
    9290        27198 :                   int gdmk;
    9291        27198 :                   enum omp_clause_defaultmap_kind kind;
    9292        27198 :                   if (lang_hooks.decls.omp_allocatable_p (decl))
    9293              :                     gdmk = GDMK_ALLOCATABLE;
    9294        26839 :                   else if (lang_hooks.decls.omp_scalar_target_p (decl))
    9295              :                     gdmk = GDMK_SCALAR_TARGET;
    9296        26740 :                   else if (lang_hooks.decls.omp_scalar_p (decl, false))
    9297              :                     gdmk = GDMK_SCALAR;
    9298        11195 :                   else if (lang_hooks.decls.omp_pointer_p (decl))
    9299              :                     gdmk = GDMK_POINTER;
    9300              :                   else
    9301         5047 :                     gdmk = GDMK_AGGREGATE;
    9302        27198 :                   kind = lang_hooks.decls.omp_predetermined_mapping (decl);
    9303        27198 :                   if (kind != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
    9304              :                     {
    9305         1014 :                       if (kind == OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE)
    9306          101 :                         nflags |= GOVD_FIRSTPRIVATE;
    9307          913 :                       else if (kind == OMP_CLAUSE_DEFAULTMAP_TO)
    9308          913 :                         nflags |= GOVD_MAP | GOVD_MAP_TO_ONLY;
    9309              :                       else
    9310            0 :                         gcc_unreachable ();
    9311              :                     }
    9312        26184 :                   else if (ctx->defaultmap[gdmk] == 0)
    9313              :                     {
    9314           75 :                       tree d = lang_hooks.decls.omp_report_decl (decl);
    9315           75 :                       error ("%qE not specified in enclosing %<target%>",
    9316           75 :                              DECL_NAME (d));
    9317           75 :                       inform (ctx->location, "enclosing %<target%>");
    9318              :                     }
    9319        26109 :                   else if (ctx->defaultmap[gdmk]
    9320        26109 :                            & (GOVD_MAP_0LEN_ARRAY | GOVD_FIRSTPRIVATE))
    9321         9258 :                     nflags |= ctx->defaultmap[gdmk];
    9322        16851 :                   else if (ctx->defaultmap[gdmk] & GOVD_MAP_FORCE_PRESENT)
    9323              :                     {
    9324           42 :                       gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
    9325           42 :                       nflags |= ctx->defaultmap[gdmk] | GOVD_MAP_ALLOC_ONLY;
    9326              :                     }
    9327              :                   else
    9328              :                     {
    9329        16809 :                       gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
    9330        16809 :                       nflags |= ctx->defaultmap[gdmk] & ~GOVD_MAP;
    9331              :                     }
    9332              :                 }
    9333              :             }
    9334              : 
    9335        46121 :           struct gimplify_omp_ctx *octx = ctx->outer_context;
    9336        46121 :           if ((ctx->region_type & ORT_ACC) && octx)
    9337              :             {
    9338              :               /* Look in outer OpenACC contexts, to see if there's a
    9339              :                  data attribute for this variable.  */
    9340         3535 :               omp_notice_variable (octx, decl, in_code);
    9341              : 
    9342         8842 :               for (; octx; octx = octx->outer_context)
    9343              :                 {
    9344         3883 :                   if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
    9345              :                     break;
    9346         3883 :                   splay_tree_node n2
    9347         3883 :                     = splay_tree_lookup (octx->variables,
    9348              :                                          (splay_tree_key) decl);
    9349         3883 :                   if (n2)
    9350              :                     {
    9351         2111 :                       if (octx->region_type == ORT_ACC_HOST_DATA)
    9352            4 :                         error ("variable %qE declared in enclosing "
    9353            4 :                                "%<host_data%> region", DECL_NAME (decl));
    9354         2111 :                       nflags |= GOVD_MAP;
    9355         2111 :                       if (octx->region_type == ORT_ACC_DATA
    9356         2107 :                           && (n2->value & GOVD_MAP_0LEN_ARRAY))
    9357          288 :                         nflags |= GOVD_MAP_0LEN_ARRAY;
    9358         2111 :                       goto found_outer;
    9359              :                     }
    9360              :                 }
    9361              :             }
    9362              : 
    9363        44010 :           if ((nflags & ~(GOVD_MAP_TO_ONLY | GOVD_MAP_FROM_ONLY
    9364              :                           | GOVD_MAP_ALLOC_ONLY)) == flags)
    9365              :             {
    9366        33696 :               tree type = TREE_TYPE (decl);
    9367        33696 :               location_t loc = DECL_SOURCE_LOCATION (decl);
    9368              : 
    9369        33696 :               if (gimplify_omp_ctxp->target_firstprivatize_array_bases
    9370        33696 :                   && omp_privatize_by_reference (decl))
    9371           28 :                 type = TREE_TYPE (type);
    9372              : 
    9373        33696 :               if (!verify_type_context (loc, TCTX_OMP_MAP_IMP_REF, type))
    9374              :                 /* Check if TYPE can appear in a target region.
    9375              :                    verify_type_context has already issued an error if it
    9376              :                    can't.  */
    9377            0 :                 nflags |= GOVD_MAP | GOVD_EXPLICIT;
    9378        33696 :               else if (!omp_mappable_type (type))
    9379              :                 {
    9380            8 :                   error ("%qD referenced in target region does not have "
    9381              :                          "a mappable type", decl);
    9382            8 :                   nflags |= GOVD_MAP | GOVD_EXPLICIT;
    9383              :                 }
    9384              :               else
    9385              :                 {
    9386        33688 :                   if ((ctx->region_type & ORT_ACC) != 0)
    9387         8839 :                     nflags = oacc_default_clause (ctx, decl, flags);
    9388              :                   else
    9389        24849 :                     nflags |= GOVD_MAP;
    9390              :                 }
    9391              :             }
    9392        10314 :         found_outer:
    9393        46121 :           omp_add_variable (ctx, decl, nflags);
    9394        46121 :           if (ctx->region_type & ORT_ACC)
    9395              :             /* For OpenACC, as remarked above, defer expansion.  */
    9396              :             shared = false;
    9397              :           else
    9398        35167 :             shared = (nflags & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
    9399        46121 :           ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
    9400              :         }
    9401              :       else
    9402              :         {
    9403       820114 :           if (ctx->region_type & ORT_ACC)
    9404              :             /* For OpenACC, as remarked above, defer expansion.  */
    9405              :             shared = false;
    9406              :           else
    9407       523257 :             shared = ((n->value | flags)
    9408       523257 :                       & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
    9409       820114 :           ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
    9410              :           /* If nothing changed, there's nothing left to do.  */
    9411       820114 :           if ((n->value & flags) == flags)
    9412              :             return ret;
    9413        18157 :           flags |= n->value;
    9414        18157 :           n->value = flags;
    9415              :         }
    9416        64278 :       goto do_outer;
    9417              :     }
    9418              : 
    9419      3174911 :   if (n == NULL)
    9420              :     {
    9421      1252437 :       if (ctx->region_type == ORT_WORKSHARE
    9422              :           || ctx->region_type == ORT_TASKGROUP
    9423       600195 :           || ctx->region_type == ORT_SIMD
    9424       290459 :           || ctx->region_type == ORT_ACC
    9425        94590 :           || (ctx->region_type & ORT_TARGET_DATA) != 0)
    9426      1202562 :         goto do_outer;
    9427              : 
    9428        49875 :       flags = omp_default_clause (ctx, decl, in_code, flags);
    9429              : 
    9430        49875 :       if ((flags & GOVD_PRIVATE)
    9431        49875 :           && lang_hooks.decls.omp_private_outer_ref (decl))
    9432            6 :         flags |= GOVD_PRIVATE_OUTER_REF;
    9433              : 
    9434        49875 :       omp_add_variable (ctx, decl, flags);
    9435              : 
    9436        49875 :       shared = (flags & GOVD_SHARED) != 0;
    9437        49875 :       ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
    9438        49875 :       goto do_outer;
    9439              :     }
    9440              : 
    9441              :   /* Don't mark as GOVD_SEEN addressable temporaries seen only in simd
    9442              :      lb, b or incr expressions, those shouldn't be turned into simd arrays.  */
    9443      1922474 :   if (ctx->region_type == ORT_SIMD
    9444       172462 :       && ctx->in_for_exprs
    9445           72 :       && ((n->value & (GOVD_PRIVATE | GOVD_SEEN | GOVD_EXPLICIT))
    9446              :           == GOVD_PRIVATE))
    9447      1922474 :     flags &= ~GOVD_SEEN;
    9448              : 
    9449      1922474 :   if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
    9450        32382 :       && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
    9451      1954817 :       && DECL_SIZE (decl))
    9452              :     {
    9453        32342 :       tree size;
    9454        32342 :       if (!poly_int_tree_p (DECL_SIZE (decl)))
    9455              :         {
    9456          150 :           splay_tree_node n2;
    9457          150 :           tree t = DECL_VALUE_EXPR (decl);
    9458          150 :           gcc_assert (INDIRECT_REF_P (t));
    9459          150 :           t = TREE_OPERAND (t, 0);
    9460          150 :           gcc_assert (DECL_P (t));
    9461          150 :           n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
    9462          150 :           n2->value |= GOVD_SEEN;
    9463              :         }
    9464        32192 :       else if (omp_privatize_by_reference (decl)
    9465         4194 :                && (size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
    9466        36349 :                && !poly_int_tree_p (size))
    9467              :         {
    9468         1337 :           splay_tree_node n2;
    9469         1337 :           gcc_assert (DECL_P (size));
    9470         1337 :           n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) size);
    9471         1337 :           if (n2)
    9472          629 :             omp_notice_variable (ctx, size, true);
    9473              :         }
    9474              :     }
    9475              : 
    9476      1922474 :   if (ctx->region_type & ORT_ACC)
    9477              :     /* For OpenACC, as remarked above, defer expansion.  */
    9478              :     shared = false;
    9479              :   else
    9480      1739963 :     shared = ((flags | n->value) & GOVD_SHARED) != 0;
    9481      1922474 :   ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
    9482              : 
    9483              :   /* If nothing changed, there's nothing left to do.  */
    9484      1922474 :   if ((n->value & flags) == flags)
    9485              :     return ret;
    9486        32343 :   flags |= n->value;
    9487        32343 :   n->value = flags;
    9488              : 
    9489      1349058 :  do_outer:
    9490              :   /* If the variable is private in the current context, then we don't
    9491              :      need to propagate anything to an outer context.  */
    9492      1349058 :   if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
    9493              :     return ret;
    9494      1338524 :   if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
    9495              :       == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
    9496              :     return ret;
    9497      1338462 :   if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
    9498              :                 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
    9499              :       == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
    9500              :     return ret;
    9501      1338462 :   if (ctx->outer_context
    9502      1338462 :       && omp_notice_variable (ctx->outer_context, decl, in_code))
    9503         2151 :     return true;
    9504              :   return ret;
    9505              : }
    9506              : 
    9507              : /* Verify that DECL is private within CTX.  If there's specific information
    9508              :    to the contrary in the innermost scope, generate an error.  */
    9509              : 
    9510              : static bool
    9511        52517 : omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
    9512              : {
    9513       101457 :   splay_tree_node n;
    9514              : 
    9515       101457 :   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
    9516       101457 :   if (n != NULL)
    9517              :     {
    9518        18718 :       if (n->value & GOVD_SHARED)
    9519              :         {
    9520          407 :           if (ctx == gimplify_omp_ctxp)
    9521              :             {
    9522            0 :               if (simd)
    9523            0 :                 error ("iteration variable %qE is predetermined linear",
    9524            0 :                        DECL_NAME (decl));
    9525              :               else
    9526            0 :                 error ("iteration variable %qE should be private",
    9527            0 :                        DECL_NAME (decl));
    9528            0 :               n->value = GOVD_PRIVATE;
    9529            0 :               return true;
    9530              :             }
    9531              :           else
    9532              :             return false;
    9533              :         }
    9534        18311 :       else if ((n->value & GOVD_EXPLICIT) != 0
    9535        10102 :                && (ctx == gimplify_omp_ctxp
    9536          416 :                    || (ctx->region_type == ORT_COMBINED_PARALLEL
    9537          220 :                        && gimplify_omp_ctxp->outer_context == ctx)))
    9538              :         {
    9539         9906 :           if ((n->value & GOVD_FIRSTPRIVATE) != 0)
    9540            4 :             error ("iteration variable %qE should not be firstprivate",
    9541            4 :                    DECL_NAME (decl));
    9542         9902 :           else if ((n->value & GOVD_REDUCTION) != 0)
    9543            8 :             error ("iteration variable %qE should not be reduction",
    9544            8 :                    DECL_NAME (decl));
    9545         9894 :           else if (simd != 1 && (n->value & GOVD_LINEAR) != 0)
    9546           58 :             error ("iteration variable %qE should not be linear",
    9547           58 :                    DECL_NAME (decl));
    9548              :         }
    9549        18311 :       return (ctx == gimplify_omp_ctxp
    9550        18311 :               || (ctx->region_type == ORT_COMBINED_PARALLEL
    9551         2646 :                   && gimplify_omp_ctxp->outer_context == ctx));
    9552              :     }
    9553              : 
    9554        82739 :   if (ctx->region_type != ORT_WORKSHARE
    9555              :       && ctx->region_type != ORT_TASKGROUP
    9556        52867 :       && ctx->region_type != ORT_SIMD
    9557        37663 :       && ctx->region_type != ORT_ACC)
    9558              :     return false;
    9559        57249 :   else if (ctx->outer_context)
    9560              :     return omp_is_private (ctx->outer_context, decl, simd);
    9561              :   return false;
    9562              : }
    9563              : 
    9564              : /* Return true if DECL is private within a parallel region
    9565              :    that binds to the current construct's context or in parallel
    9566              :    region's REDUCTION clause.  */
    9567              : 
    9568              : static bool
    9569        12152 : omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
    9570              : {
    9571        12224 :   splay_tree_node n;
    9572              : 
    9573        12224 :   do
    9574              :     {
    9575        12224 :       ctx = ctx->outer_context;
    9576        12224 :       if (ctx == NULL)
    9577              :         {
    9578         2786 :           if (is_global_var (decl))
    9579              :             return false;
    9580              : 
    9581              :           /* References might be private, but might be shared too,
    9582              :              when checking for copyprivate, assume they might be
    9583              :              private, otherwise assume they might be shared.  */
    9584         1292 :           if (copyprivate)
    9585              :             return true;
    9586              : 
    9587         1246 :           if (omp_privatize_by_reference (decl))
    9588              :             return false;
    9589              : 
    9590              :           /* Treat C++ privatized non-static data members outside
    9591              :              of the privatization the same.  */
    9592         1200 :           if (omp_member_access_dummy_var (decl))
    9593              :             return false;
    9594              : 
    9595         1144 :           return true;
    9596              :         }
    9597              : 
    9598         9438 :       n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
    9599              : 
    9600         9438 :       if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
    9601         1291 :           && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
    9602              :         {
    9603          242 :           if ((ctx->region_type & ORT_TARGET_DATA) != 0
    9604          238 :               || n == NULL
    9605          230 :               || (n->value & GOVD_MAP) == 0)
    9606           12 :             continue;
    9607              :           return false;
    9608              :         }
    9609              : 
    9610         8147 :       if (n != NULL)
    9611              :         {
    9612         5589 :           if ((n->value & GOVD_LOCAL) != 0
    9613         5589 :               && omp_member_access_dummy_var (decl))
    9614              :             return false;
    9615         5550 :           return (n->value & GOVD_SHARED) == 0;
    9616              :         }
    9617              : 
    9618         3607 :       if (ctx->region_type == ORT_WORKSHARE
    9619              :           || ctx->region_type == ORT_TASKGROUP
    9620         3551 :           || ctx->region_type == ORT_SIMD
    9621         3547 :           || ctx->region_type == ORT_ACC)
    9622           60 :         continue;
    9623              : 
    9624              :       break;
    9625              :     }
    9626              :   while (1);
    9627              :   return false;
    9628              : }
    9629              : 
    9630              : /* Callback for walk_tree to find a DECL_EXPR for the given DECL.  */
    9631              : 
    9632              : static tree
    9633         3148 : find_decl_expr (tree *tp, int *walk_subtrees, void *data)
    9634              : {
    9635         3148 :   tree t = *tp;
    9636              : 
    9637              :   /* If this node has been visited, unmark it and keep looking.  */
    9638         3148 :   if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
    9639              :     return t;
    9640              : 
    9641         2724 :   if (IS_TYPE_OR_DECL_P (t))
    9642          452 :     *walk_subtrees = 0;
    9643              :   return NULL_TREE;
    9644              : }
    9645              : 
    9646              : 
    9647              : /* Gimplify the affinity clause but effectively ignore it.
    9648              :    Generate:
    9649              :      var = begin;
    9650              :      if ((step > 1) ? var <= end : var > end)
    9651              :        locatator_var_expr;  */
    9652              : 
    9653              : static void
    9654          510 : gimplify_omp_affinity (tree *list_p, gimple_seq *pre_p)
    9655              : {
    9656          510 :   tree last_iter = NULL_TREE;
    9657          510 :   tree last_bind = NULL_TREE;
    9658          510 :   tree label = NULL_TREE;
    9659          510 :   tree *last_body = NULL;
    9660         1287 :   for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
    9661          777 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
    9662              :       {
    9663          777 :         tree t = OMP_CLAUSE_DECL (c);
    9664          777 :         if (OMP_ITERATOR_DECL_P (t))
    9665              :           {
    9666          389 :             if (TREE_VALUE (t) == null_pointer_node)
    9667          201 :               continue;
    9668          188 :             if (TREE_PURPOSE (t) != last_iter)
    9669              :               {
    9670          127 :                 if (last_bind)
    9671              :                   {
    9672            9 :                     append_to_statement_list (label, last_body);
    9673            9 :                     gimplify_and_add (last_bind, pre_p);
    9674            9 :                     last_bind = NULL_TREE;
    9675              :                   }
    9676          274 :                 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
    9677              :                   {
    9678          147 :                     if (gimplify_expr (&OMP_ITERATOR_BEGIN (it), pre_p, NULL,
    9679              :                                        is_gimple_val, fb_rvalue) == GS_ERROR
    9680          147 :                         || gimplify_expr (&OMP_ITERATOR_END (it), pre_p, NULL,
    9681              :                                           is_gimple_val, fb_rvalue) == GS_ERROR
    9682          147 :                         || gimplify_expr (&OMP_ITERATOR_STEP (it), pre_p, NULL,
    9683              :                                           is_gimple_val, fb_rvalue) == GS_ERROR
    9684          294 :                         || (gimplify_expr (&OMP_ITERATOR_ORIG_STEP (it), pre_p,
    9685              :                                            NULL, is_gimple_val, fb_rvalue)
    9686              :                             == GS_ERROR))
    9687            0 :                       return;
    9688              :                   }
    9689          127 :             last_iter = TREE_PURPOSE (t);
    9690          127 :             tree block = OMP_ITERATOR_BLOCK (TREE_PURPOSE (t));
    9691          127 :             last_bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
    9692              :                                 NULL, block);
    9693          127 :             last_body = &BIND_EXPR_BODY (last_bind);
    9694          127 :             tree cond = NULL_TREE;
    9695          127 :             location_t loc = OMP_CLAUSE_LOCATION (c);
    9696          274 :             for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
    9697              :               {
    9698          147 :                 tree var = OMP_ITERATOR_VAR (it);
    9699          147 :                 tree begin = OMP_ITERATOR_BEGIN (it);
    9700          147 :                 tree end = OMP_ITERATOR_END (it);
    9701          147 :                 tree step = OMP_ITERATOR_STEP (it);
    9702          147 :                 loc = DECL_SOURCE_LOCATION (var);
    9703          147 :                 tree tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
    9704              :                                        var, begin);
    9705          147 :                 append_to_statement_list_force (tem, last_body);
    9706              : 
    9707          147 :                 tree cond1 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
    9708          147 :                                step, build_zero_cst (TREE_TYPE (step)));
    9709          147 :                 tree cond2 = fold_build2_loc (loc, LE_EXPR, boolean_type_node,
    9710              :                                               var, end);
    9711          147 :                 tree cond3 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
    9712              :                                               var, end);
    9713          147 :                 cond1 = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
    9714              :                                          cond1, cond2, cond3);
    9715          147 :                 if (cond)
    9716           20 :                   cond = fold_build2_loc (loc, TRUTH_AND_EXPR,
    9717              :                                           boolean_type_node, cond, cond1);
    9718              :                 else
    9719              :                   cond = cond1;
    9720              :               }
    9721          127 :             tree cont_label = create_artificial_label (loc);
    9722          127 :             label = build1 (LABEL_EXPR, void_type_node, cont_label);
    9723          127 :             tree tem = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
    9724              :                                         void_node,
    9725              :                                         build_and_jump (&cont_label));
    9726          127 :             append_to_statement_list_force (tem, last_body);
    9727              :               }
    9728          188 :             if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
    9729              :               {
    9730            0 :                 append_to_statement_list (TREE_OPERAND (TREE_VALUE (t), 0),
    9731              :                                           last_body);
    9732            0 :                 TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
    9733              :               }
    9734          188 :             if (error_operand_p (TREE_VALUE (t)))
    9735              :               return;
    9736          188 :             append_to_statement_list_force (TREE_VALUE (t), last_body);
    9737          188 :             TREE_VALUE (t) = null_pointer_node;
    9738              :           }
    9739              :         else
    9740              :           {
    9741          388 :             if (last_bind)
    9742              :               {
    9743            9 :                 append_to_statement_list (label, last_body);
    9744            9 :                 gimplify_and_add (last_bind, pre_p);
    9745            9 :                 last_bind = NULL_TREE;
    9746              :               }
    9747          388 :             if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
    9748              :               {
    9749            0 :                 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
    9750              :                                NULL, is_gimple_val, fb_rvalue);
    9751            0 :                 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
    9752              :               }
    9753          388 :             if (error_operand_p (OMP_CLAUSE_DECL (c)))
    9754              :               return;
    9755          388 :             if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
    9756              :                                is_gimple_lvalue, fb_lvalue) == GS_ERROR)
    9757              :               return;
    9758          388 :             gimplify_and_add (OMP_CLAUSE_DECL (c), pre_p);
    9759              :           }
    9760              :       }
    9761          510 :   if (last_bind)
    9762              :     {
    9763          109 :       append_to_statement_list (label, last_body);
    9764          109 :       gimplify_and_add (last_bind, pre_p);
    9765              :     }
    9766              :   return;
    9767              : }
    9768              : 
    9769              : /* Returns a tree expression containing the total iteration count of the
    9770              :    OpenMP iterator IT.  */
    9771              : 
    9772              : static tree
    9773          391 : compute_omp_iterator_count (tree it, gimple_seq *pre_p)
    9774              : {
    9775          391 :   tree tcnt = size_one_node;
    9776          861 :   for (; it; it = TREE_CHAIN (it))
    9777              :     {
    9778          470 :       if (gimplify_expr (&OMP_ITERATOR_BEGIN (it), pre_p, NULL,
    9779              :                          is_gimple_val, fb_rvalue) == GS_ERROR
    9780          470 :           || gimplify_expr (&OMP_ITERATOR_END (it), pre_p, NULL,
    9781              :                             is_gimple_val, fb_rvalue) == GS_ERROR
    9782          470 :           || gimplify_expr (&OMP_ITERATOR_STEP (it), pre_p, NULL,
    9783              :                             is_gimple_val, fb_rvalue) == GS_ERROR
    9784          940 :           || (gimplify_expr (&OMP_ITERATOR_ORIG_STEP (it), pre_p, NULL,
    9785              :                              is_gimple_val, fb_rvalue) == GS_ERROR))
    9786            0 :         return NULL_TREE;
    9787          470 :       tree var = OMP_ITERATOR_VAR (it);
    9788          470 :       tree begin = OMP_ITERATOR_BEGIN (it);
    9789          470 :       tree end = OMP_ITERATOR_END (it);
    9790          470 :       tree step = OMP_ITERATOR_STEP (it);
    9791          470 :       tree orig_step = OMP_ITERATOR_ORIG_STEP (it);
    9792          470 :       tree type = TREE_TYPE (var);
    9793          470 :       tree stype = TREE_TYPE (step);
    9794          470 :       location_t loc = DECL_SOURCE_LOCATION (var);
    9795          470 :       tree endmbegin;
    9796              :       /* Compute count for this iterator as
    9797              :          orig_step > 0
    9798              :          ? (begin < end ? (end - begin + (step - 1)) / step : 0)
    9799              :          : (begin > end ? (end - begin + (step + 1)) / step : 0)
    9800              :          and compute product of those for the entire clause.  */
    9801          470 :       if (POINTER_TYPE_P (type))
    9802           42 :         endmbegin = fold_build2_loc (loc, POINTER_DIFF_EXPR, stype, end, begin);
    9803              :       else
    9804          428 :         endmbegin = fold_build2_loc (loc, MINUS_EXPR, type, end, begin);
    9805              :       /* Account for iteration stopping on the end value in Fortran rather
    9806              :          than before it.  */
    9807          470 :       tree stepm1 = step;
    9808          470 :       tree stepp1 = step;
    9809          470 :       if (!lang_GNU_Fortran ())
    9810              :         {
    9811          372 :           stepm1 = fold_build2_loc (loc, MINUS_EXPR, stype, step,
    9812              :                                     build_int_cst (stype, 1));
    9813          372 :           stepp1 = fold_build2_loc (loc, PLUS_EXPR, stype, step,
    9814              :                                     build_int_cst (stype, 1));
    9815              :         }
    9816          470 :       tree pos = fold_build2_loc (loc, PLUS_EXPR, stype,
    9817              :                                   unshare_expr (endmbegin), stepm1);
    9818          470 :       pos = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype, pos, step);
    9819          470 :       tree neg = fold_build2_loc (loc, PLUS_EXPR, stype, endmbegin, stepp1);
    9820          470 :       if (TYPE_UNSIGNED (stype))
    9821              :         {
    9822           31 :           neg = fold_build1_loc (loc, NEGATE_EXPR, stype, neg);
    9823           31 :           step = fold_build1_loc (loc, NEGATE_EXPR, stype, step);
    9824              :         }
    9825          470 :       neg = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype, neg, step);
    9826          470 :       step = NULL_TREE;
    9827          470 :       tree_code cmp_op = lang_GNU_Fortran () ? LE_EXPR : LT_EXPR;
    9828          470 :       tree cond = fold_build2_loc (loc, cmp_op, boolean_type_node, begin, end);
    9829          470 :       pos = fold_build3_loc (loc, COND_EXPR, stype, cond, pos,
    9830              :                              build_int_cst (stype, 0));
    9831          470 :       cond = fold_build2_loc (loc, cmp_op, boolean_type_node, end, begin);
    9832          470 :       neg = fold_build3_loc (loc, COND_EXPR, stype, cond, neg,
    9833              :                              build_int_cst (stype, 0));
    9834          470 :       tree osteptype = TREE_TYPE (orig_step);
    9835          470 :       cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node, orig_step,
    9836              :                               build_int_cst (osteptype, 0));
    9837          470 :       tree cnt = fold_build3_loc (loc, COND_EXPR, stype, cond, pos, neg);
    9838          470 :       cnt = fold_convert_loc (loc, sizetype, cnt);
    9839          470 :       if (gimplify_expr (&cnt, pre_p, NULL, is_gimple_val,
    9840              :                          fb_rvalue) == GS_ERROR)
    9841              :         return NULL_TREE;
    9842          470 :       tcnt = size_binop_loc (loc, MULT_EXPR, tcnt, cnt);
    9843              :     }
    9844          391 :   if (gimplify_expr (&tcnt, pre_p, NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
    9845              :     return NULL_TREE;
    9846              : 
    9847          391 :   return tcnt;
    9848              : }
    9849              : 
    9850              : /* Build loops iterating over the space defined by the OpenMP iterator IT.
    9851              :    Returns a pointer to the BIND_EXPR_BODY in the innermost loop body.
    9852              :    LAST_BIND is set to point to the BIND_EXPR containing the whole loop.  */
    9853              : 
    9854              : static tree *
    9855          391 : build_omp_iterator_loop (tree it, gimple_seq *pre_p, tree *last_bind)
    9856              : {
    9857          391 :   if (*last_bind)
    9858           31 :     gimplify_and_add (*last_bind, pre_p);
    9859          391 :   tree block = OMP_ITERATOR_BLOCK (it);
    9860          476 :   tree block_stmts = lang_GNU_Fortran () ? BLOCK_SUBBLOCKS (block) : NULL_TREE;
    9861          391 :   *last_bind = build3 (BIND_EXPR, void_type_node,
    9862          391 :                        BLOCK_VARS (block), NULL, block);
    9863          391 :   TREE_SIDE_EFFECTS (*last_bind) = 1;
    9864          391 :   tree *p = &BIND_EXPR_BODY (*last_bind);
    9865         1252 :   for (; it; it = TREE_CHAIN (it))
    9866              :     {
    9867          470 :       tree var = OMP_ITERATOR_VAR (it);
    9868          470 :       tree begin = OMP_ITERATOR_BEGIN (it);
    9869          470 :       tree end = OMP_ITERATOR_END (it);
    9870          470 :       tree step = OMP_ITERATOR_STEP (it);
    9871          470 :       tree orig_step = OMP_ITERATOR_ORIG_STEP (it);
    9872          470 :       block = OMP_ITERATOR_BLOCK (it);
    9873          470 :       tree type = TREE_TYPE (var);
    9874          470 :       location_t loc = DECL_SOURCE_LOCATION (var);
    9875              :       /* Emit:
    9876              :          var = begin;
    9877              :          goto cond_label;
    9878              :          beg_label:
    9879              :          ...
    9880              :          var = var + step;
    9881              :          cond_label:
    9882              :          if (orig_step > 0) {
    9883              :            if (var < end) goto beg_label;  // <= for Fortran
    9884              :          } else {
    9885              :            if (var > end) goto beg_label;  // >= for Fortran
    9886              :          }
    9887              :          for each iterator, with inner iterators added to
    9888              :          the ... above.  */
    9889          470 :       tree beg_label = create_artificial_label (loc);
    9890          470 :       tree cond_label = NULL_TREE;
    9891          470 :       tree tem = build2_loc (loc, MODIFY_EXPR, void_type_node, var, begin);
    9892          470 :       append_to_statement_list_force (tem, p);
    9893          470 :       tem = build_and_jump (&cond_label);
    9894          470 :       append_to_statement_list_force (tem, p);
    9895          470 :       tem = build1 (LABEL_EXPR, void_type_node, beg_label);
    9896          470 :       append_to_statement_list (tem, p);
    9897          470 :       tree bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
    9898              :                           NULL_TREE, NULL_TREE);
    9899          470 :       TREE_SIDE_EFFECTS (bind) = 1;
    9900          470 :       SET_EXPR_LOCATION (bind, loc);
    9901          470 :       append_to_statement_list_force (bind, p);
    9902          470 :       if (POINTER_TYPE_P (type))
    9903           42 :         tem = build2_loc (loc, POINTER_PLUS_EXPR, type,
    9904              :                           var, fold_convert_loc (loc, sizetype, step));
    9905              :       else
    9906          428 :         tem = build2_loc (loc, PLUS_EXPR, type, var, step);
    9907          470 :       tem = build2_loc (loc, MODIFY_EXPR, void_type_node, var, tem);
    9908          470 :       append_to_statement_list_force (tem, p);
    9909          470 :       tem = build1 (LABEL_EXPR, void_type_node, cond_label);
    9910          470 :       append_to_statement_list (tem, p);
    9911          842 :       tree cond = fold_build2_loc (loc, lang_GNU_Fortran () ? LE_EXPR : LT_EXPR,
    9912              :                                    boolean_type_node, var, end);
    9913          470 :       tree pos = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
    9914              :                                   build_and_jump (&beg_label), void_node);
    9915          842 :       cond = fold_build2_loc (loc, lang_GNU_Fortran () ? GE_EXPR : GT_EXPR,
    9916              :                               boolean_type_node, var, end);
    9917          470 :       tree neg = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
    9918              :                                   build_and_jump (&beg_label), void_node);
    9919          470 :       tree osteptype = TREE_TYPE (orig_step);
    9920          470 :       cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node, orig_step,
    9921              :                               build_int_cst (osteptype, 0));
    9922          470 :       tem = fold_build3_loc (loc, COND_EXPR, void_type_node, cond, pos, neg);
    9923          470 :       append_to_statement_list_force (tem, p);
    9924          470 :       p = &BIND_EXPR_BODY (bind);
    9925              :       /* The Fortran front-end stashes statements into the BLOCK_SUBBLOCKS
    9926              :          of the last element of the first iterator.  These should go into the
    9927              :          body of the innermost loop.  */
    9928          470 :       if (!TREE_CHAIN (it))
    9929          391 :         append_to_statement_list_force (block_stmts, p);
    9930              :     }
    9931              : 
    9932          391 :   return p;
    9933              : }
    9934              : 
    9935              : 
    9936              : /* Callback for walk_tree to find a VAR_DECL (stored in DATA) in the
    9937              :    tree TP.  */
    9938              : 
    9939              : static tree
    9940         3900 : find_var_decl (tree *tp, int *, void *data)
    9941              : {
    9942         3900 :   if (*tp == (tree) data)
    9943          369 :     return *tp;
    9944              : 
    9945              :   return NULL_TREE;
    9946              : }
    9947              : 
    9948              : /* Returns an element-by-element copy of OMP iterator tree IT.  If
    9949              :    EXPAND is true, force the result to be an expanded iterator; otherwise
    9950              :    it makes the copy the same size as IT.  */
    9951              : static tree
    9952          337 : copy_omp_iterator (tree it, bool expand = false)
    9953              : {
    9954          337 :   int elem_count = TREE_VEC_LENGTH (it);
    9955          337 :   tree new_it =
    9956          337 :     (expand ? make_expanded_omp_iterator () : make_tree_vec (elem_count));
    9957         2359 :   for (int i = 0; i < TREE_VEC_LENGTH (it); i++)
    9958         2022 :     TREE_VEC_ELT (new_it, i) = TREE_VEC_ELT (it, i);
    9959          337 :   return new_it;
    9960              : }
    9961              : 
    9962              : /* Helper function for walk_tree in remap_omp_iterator_var.  */
    9963              : 
    9964              : static tree
    9965         1063 : remap_omp_iterator_var_1 (tree *tp, int *, void *data)
    9966              : {
    9967         1063 :   tree old_var = ((tree *) data)[0];
    9968         1063 :   tree new_var = ((tree *) data)[1];
    9969              : 
    9970         1063 :   if (*tp == old_var)
    9971          106 :     *tp = new_var;
    9972         1063 :   return NULL_TREE;
    9973              : }
    9974              : 
    9975              : /* Replace instances of OLD_VAR in TP with NEW_VAR.  */
    9976              : 
    9977              : static void
    9978          212 : remap_omp_iterator_var (tree *tp, tree old_var, tree new_var)
    9979              : {
    9980          212 :   tree vars[2] = { old_var, new_var };
    9981          212 :   walk_tree (tp, remap_omp_iterator_var_1, vars, NULL);
    9982          212 : }
    9983              : 
    9984              : /* Scan through all clauses using OpenMP iterators in LIST_P.  If any
    9985              :    clauses have iterators with variables that are not used by the clause
    9986              :    decl or size, issue a warning and replace the iterator with a copy with
    9987              :    the unused variables removed.  */
    9988              : 
    9989              : static void
    9990        29229 : remove_unused_omp_iterator_vars (tree *list_p)
    9991              : {
    9992        29229 :   auto_vec< vec<tree> > iter_vars;
    9993        29229 :   auto_vec<tree> new_iterators;
    9994              : 
    9995        85918 :   for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
    9996              :     {
    9997        56689 :       if (!OMP_CLAUSE_HAS_ITERATORS (c))
    9998        56625 :         continue;
    9999          288 :       auto_vec<tree> vars;
   10000          288 :       bool need_new_iterators = false;
   10001          734 :       for (tree it = OMP_CLAUSE_ITERATORS (c); it; it = TREE_CHAIN (it))
   10002              :         {
   10003          446 :           tree var = OMP_ITERATOR_VAR (it);
   10004          446 :           tree t = walk_tree (&OMP_CLAUSE_DECL (c), find_var_decl, var, NULL);
   10005          446 :           if (t == NULL_TREE)
   10006          143 :             t = walk_tree (&OMP_CLAUSE_SIZE (c), find_var_decl, var, NULL);
   10007          143 :           if (t == NULL_TREE)
   10008              :             need_new_iterators = true;
   10009              :           else
   10010          369 :             vars.safe_push (var);
   10011              :         }
   10012          288 :       if (!need_new_iterators)
   10013          211 :         continue;
   10014           77 :       if (vars.is_empty ())
   10015              :         {
   10016              :           /* No iteration variables are used in the clause - remove the
   10017              :              iterator from the clause.  */
   10018           13 :           OMP_CLAUSE_ITERATORS (c) = NULL_TREE;
   10019           13 :           continue;
   10020              :         }
   10021              : 
   10022              :       /* If a new iterator has been created for the current set of used
   10023              :          iterator variables, then use that as the iterator.  Otherwise,
   10024              :          create a new iterator for the current iterator variable set. */
   10025              :       unsigned i;
   10026          106 :       for (i = 0; i < iter_vars.length (); i++)
   10027              :         {
   10028          124 :           if (vars.length () != iter_vars[i].length ())
   10029            0 :             continue;
   10030              :           bool identical_p = true;
   10031          150 :           for (unsigned j = 0; j < vars.length () && identical_p; j++)
   10032           88 :             identical_p = vars[j] == iter_vars[i][j];
   10033              : 
   10034           62 :           if (identical_p)
   10035              :             break;
   10036              :         }
   10037           64 :       if (i < iter_vars.length ())
   10038           20 :         OMP_CLAUSE_ITERATORS (c) = new_iterators[i];
   10039              :       else
   10040              :         {
   10041           44 :           tree new_iters = NULL_TREE;
   10042           44 :           tree *new_iters_p = &new_iters;
   10043           44 :           tree new_vars = NULL_TREE;
   10044           44 :           tree *new_vars_p = &new_vars;
   10045           44 :           i = 0;
   10046          140 :           for (tree it = OMP_CLAUSE_ITERATORS (c); it && i < vars.length();
   10047           96 :                it = TREE_CHAIN (it))
   10048              :             {
   10049           96 :               tree var = OMP_ITERATOR_VAR (it);
   10050           96 :               if (var == vars[i])
   10051              :                 {
   10052           74 :                   *new_iters_p = copy_omp_iterator (it);
   10053           74 :                   *new_vars_p = build_decl (OMP_CLAUSE_LOCATION (c), VAR_DECL,
   10054           74 :                                             DECL_NAME (var), TREE_TYPE (var));
   10055           74 :                   DECL_ARTIFICIAL (*new_vars_p) = 1;
   10056           74 :                   DECL_CONTEXT (*new_vars_p) = DECL_CONTEXT (var);
   10057           74 :                   OMP_ITERATOR_VAR (*new_iters_p) = *new_vars_p;
   10058           74 :                   new_iters_p = &TREE_CHAIN (*new_iters_p);
   10059           74 :                   new_vars_p = &DECL_CHAIN (*new_vars_p);
   10060           74 :                   i++;
   10061              :                 }
   10062              :             }
   10063           44 :           tree old_block = OMP_ITERATOR_BLOCK (OMP_CLAUSE_ITERATORS (c));
   10064           44 :           tree new_block = make_node (BLOCK);
   10065           44 :           BLOCK_VARS (new_block) = new_vars;
   10066           44 :           if (BLOCK_SUBBLOCKS (old_block))
   10067              :             {
   10068            4 :               BLOCK_SUBBLOCKS (new_block) = BLOCK_SUBBLOCKS (old_block);
   10069            4 :               BLOCK_SUBBLOCKS (old_block) = NULL_TREE;
   10070              :             }
   10071           44 :           OMP_ITERATOR_BLOCK (new_iters) = new_block;
   10072           44 :           new_iterators.safe_push (new_iters);
   10073           44 :           iter_vars.safe_push (vars.copy ());
   10074           44 :           OMP_CLAUSE_ITERATORS (c) = new_iters;
   10075              :         }
   10076              : 
   10077              :       /* Remap clause to use the new variables.  */
   10078           64 :       i = 0;
   10079          170 :       for (tree it = OMP_CLAUSE_ITERATORS (c); it; it = TREE_CHAIN (it))
   10080              :         {
   10081          106 :           tree old_var = vars[i++];
   10082          106 :           tree new_var = OMP_ITERATOR_VAR (it);
   10083          106 :           remap_omp_iterator_var (&OMP_CLAUSE_DECL (c), old_var, new_var);
   10084          106 :           remap_omp_iterator_var (&OMP_CLAUSE_SIZE (c), old_var, new_var);
   10085              :         }
   10086          288 :     }
   10087              : 
   10088        29273 :   for (unsigned i = 0; i < iter_vars.length (); i++)
   10089           44 :     iter_vars[i].release ();
   10090        29229 : }
   10091              : 
   10092          272 : struct iterator_loop_info_t
   10093              : {
   10094              :   tree bind;
   10095              :   tree count;
   10096              :   tree index;
   10097              :   tree body_label;
   10098              :   auto_vec<tree> clauses;
   10099              : };
   10100              : 
   10101              : typedef hash_map<tree, iterator_loop_info_t> iterator_loop_info_map_t;
   10102              : 
   10103              : /* Builds a loop to expand any OpenMP iterators in the clauses in LIST_P,
   10104              :    reusing any previously built loops if they use the same set of iterators.
   10105              :    Generated Gimple statements are placed into LOOPS_SEQ_P.  The clause
   10106              :    iterators are updated with information on how and where to insert code into
   10107              :    the loop body.  */
   10108              : 
   10109              : static void
   10110        29229 : build_omp_iterators_loops (tree *list_p, gimple_seq *loops_seq_p)
   10111              : {
   10112        29229 :   iterator_loop_info_map_t loops;
   10113              : 
   10114        85918 :   for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
   10115              :     {
   10116        56689 :       if (!OMP_CLAUSE_HAS_ITERATORS (c))
   10117        56426 :         continue;
   10118              : 
   10119          275 :       bool built_p;
   10120          275 :       iterator_loop_info_t &loop
   10121          275 :         = loops.get_or_insert (OMP_CLAUSE_ITERATORS (c), &built_p);
   10122              : 
   10123          275 :       if (!built_p)
   10124              :         {
   10125          136 :           loop.count = compute_omp_iterator_count (OMP_CLAUSE_ITERATORS (c),
   10126              :                                                    loops_seq_p);
   10127          136 :           if (!loop.count)
   10128            0 :             continue;
   10129          136 :           if (integer_zerop (loop.count))
   10130            8 :             warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
   10131              :                         "iteration count is zero");
   10132              : 
   10133          136 :           loop.bind = NULL_TREE;
   10134          136 :           tree *body = build_omp_iterator_loop (OMP_CLAUSE_ITERATORS (c),
   10135              :                                                 loops_seq_p, &loop.bind);
   10136              : 
   10137          136 :           loop.index = create_tmp_var (sizetype);
   10138          136 :           SET_EXPR_LOCATION (loop.bind, OMP_CLAUSE_LOCATION (c));
   10139              : 
   10140              :           /* BEFORE LOOP:  */
   10141              :           /* idx = -1;  */
   10142              :           /* This should be initialized to before the individual elements,
   10143              :              as idx is pre-incremented in the loop body.  */
   10144          136 :           gimple *assign = gimple_build_assign (loop.index, size_int (-1));
   10145          136 :           gimple_seq_add_stmt (loops_seq_p, assign);
   10146              : 
   10147              :           /* IN LOOP BODY:  */
   10148              :           /* Create a label so we can find this point later.  */
   10149          136 :           loop.body_label = create_artificial_label (OMP_CLAUSE_LOCATION (c));
   10150          136 :           tree tem = build1 (LABEL_EXPR, void_type_node, loop.body_label);
   10151          136 :           append_to_statement_list_force (tem, body);
   10152              : 
   10153              :           /* idx += 2;  */
   10154          136 :           tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
   10155              :                             void_type_node, loop.index,
   10156              :                             size_binop (PLUS_EXPR, loop.index, size_int (2)));
   10157          136 :           append_to_statement_list_force (tem, body);
   10158              :         }
   10159              : 
   10160              :       /* Create array to hold expanded values.  */
   10161          275 :       tree last_count_2 = size_binop (MULT_EXPR, loop.count, size_int (2));
   10162          275 :       tree arr_length = size_binop (PLUS_EXPR, last_count_2, size_int (1));
   10163          275 :       tree elems = NULL_TREE;
   10164          275 :       if (TREE_CONSTANT (arr_length))
   10165              :         {
   10166          263 :           tree type = build_array_type (ptr_type_node,
   10167              :                                         build_index_type (arr_length));
   10168          263 :           elems = create_tmp_var_raw (type, "omp_iter_data");
   10169          263 :           TREE_ADDRESSABLE (elems) = 1;
   10170          263 :           gimple_add_tmp_var (elems);
   10171              :         }
   10172              :       else
   10173              :         {
   10174              :           /* Handle dynamic sizes.  */
   10175           12 :           sorry ("dynamic iterator sizes not implemented yet");
   10176           12 :           OMP_CLAUSE_ITERATORS (c) = NULL_TREE;
   10177           12 :           continue;
   10178              :         }
   10179              : 
   10180              :       /* BEFORE LOOP:  */
   10181              :       /* elems[0] = count;  */
   10182          263 :       tree lhs = build4 (ARRAY_REF, ptr_type_node, elems, size_int (0),
   10183              :                          NULL_TREE, NULL_TREE);
   10184          263 :       tree tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
   10185              :                              void_type_node, lhs, loop.count);
   10186          263 :       gimplify_and_add (tem, loops_seq_p);
   10187              : 
   10188              :       /* Make a copy of the iterator with extra info at the end.  */
   10189          263 :       tree new_iterator = copy_omp_iterator (OMP_CLAUSE_ITERATORS (c), true);
   10190          263 :       OMP_ITERATOR_LABEL (new_iterator) = loop.body_label;
   10191          263 :       OMP_ITERATOR_INDEX (new_iterator) = loop.index;
   10192          263 :       OMP_ITERATOR_ELEMS (new_iterator) = elems;
   10193          263 :       TREE_CHAIN (new_iterator) = TREE_CHAIN (OMP_CLAUSE_ITERATORS (c));
   10194          263 :       OMP_CLAUSE_ITERATORS (c) = new_iterator;
   10195              : 
   10196          263 :       loop.clauses.safe_push (c);
   10197              :     }
   10198              : 
   10199              :   /* Now gimplify and add all the loops that were built.  */
   10200        29365 :   for (hash_map<tree, iterator_loop_info_t>::iterator it = loops.begin ();
   10201        58594 :        it != loops.end (); ++it)
   10202          136 :     gimplify_and_add ((*it).second.bind, loops_seq_p);
   10203        29229 : }
   10204              : 
   10205              : /* Helper function for enter_omp_iterator_loop_context.  */
   10206              : 
   10207              : static gimple_seq *
   10208         2954 : enter_omp_iterator_loop_context_1 (tree iterator, gimple_seq *loops_seq_p)
   10209              : {
   10210              :   /* Drill into the nested bind expressions to get to the loop body.  */
   10211         2954 :   for (gimple_stmt_iterator gsi = gsi_start (*loops_seq_p);
   10212        19654 :        !gsi_end_p (gsi); gsi_next (&gsi))
   10213              :     {
   10214        19046 :       gimple *stmt = gsi_stmt (gsi);
   10215              : 
   10216        19046 :       switch (gimple_code (stmt))
   10217              :         {
   10218         2256 :         case GIMPLE_BIND:
   10219         2256 :           {
   10220         2256 :             gbind *bind_stmt = as_a<gbind *> (stmt);
   10221         2256 :             gimple_push_bind_expr (bind_stmt);
   10222         2256 :             gimple_seq *bind_body_p = gimple_bind_body_ptr (bind_stmt);
   10223         2256 :             gimple_seq *seq =
   10224         2256 :               enter_omp_iterator_loop_context_1 (iterator, bind_body_p);
   10225         2256 :             if (seq)
   10226              :               return seq;
   10227          608 :             gimple_pop_bind_expr ();
   10228              :           }
   10229          608 :           break;
   10230            3 :         case GIMPLE_TRY:
   10231            3 :           {
   10232            3 :             gimple_seq *try_eval_p = gimple_try_eval_ptr (stmt);
   10233            3 :             gimple_seq *seq =
   10234            3 :               enter_omp_iterator_loop_context_1 (iterator, try_eval_p);
   10235            3 :             if (seq)
   10236              :               return seq;
   10237              :           }
   10238              :           break;
   10239         3018 :         case GIMPLE_LABEL:
   10240         3018 :           {
   10241         3018 :             glabel *label_stmt = as_a<glabel *> (stmt);
   10242         3018 :             tree label = gimple_label_label (label_stmt);
   10243         3018 :             if (label == OMP_ITERATOR_LABEL (iterator))
   10244              :               return loops_seq_p;
   10245              :           }
   10246              :           break;
   10247              :         default:
   10248              :           break;
   10249              :         }
   10250              :     }
   10251              : 
   10252              :   return NULL;
   10253              : }
   10254              : 
   10255              : /* Enter the Gimplification context in LOOPS_SEQ_P for the iterator loop
   10256              :    associated with OpenMP clause C.  Returns the gimple_seq for the loop body
   10257              :    if C has OpenMP iterators, or ALT_SEQ_P if not.  */
   10258              : 
   10259              : static gimple_seq *
   10260        67985 : enter_omp_iterator_loop_context (tree c, gimple_seq *loops_seq_p,
   10261              :                                  gimple_seq *alt_seq_p)
   10262              : {
   10263        67985 :   if (!OMP_CLAUSE_HAS_ITERATORS (c))
   10264              :     return alt_seq_p;
   10265              : 
   10266          695 :   push_gimplify_context ();
   10267              : 
   10268          695 :   gimple_seq *seq = enter_omp_iterator_loop_context_1 (OMP_CLAUSE_ITERATORS (c),
   10269              :                                                        loops_seq_p);
   10270          695 :   gcc_assert (seq);
   10271              :   return seq;
   10272              : }
   10273              : 
   10274              : /* Enter the Gimplification context in STMT for the iterator loop associated
   10275              :    with OpenMP clause C.  Returns the gimple_seq for the loop body if C has
   10276              :    OpenMP iterators, or ALT_SEQ_P if not.  */
   10277              : 
   10278              : gimple_seq *
   10279          434 : enter_omp_iterator_loop_context (tree c, gomp_target *stmt,
   10280              :                                  gimple_seq *alt_seq_p)
   10281              : {
   10282          434 :   gimple_seq *loops_seq_p = gimple_omp_target_iterator_loops_ptr (stmt);
   10283          434 :   return enter_omp_iterator_loop_context (c, loops_seq_p, alt_seq_p);
   10284              : }
   10285              : 
   10286              : /* Exit the Gimplification context for the OpenMP clause C.  */
   10287              : 
   10288              : void
   10289        68075 : exit_omp_iterator_loop_context (tree c)
   10290              : {
   10291        68075 :   if (!OMP_CLAUSE_HAS_ITERATORS (c))
   10292              :     return;
   10293         2343 :   while (!gimplify_ctxp->bind_expr_stack.is_empty ())
   10294         1648 :     gimple_pop_bind_expr ();
   10295          695 :   pop_gimplify_context (NULL);
   10296              : }
   10297              : 
   10298              : /* If *LIST_P contains any OpenMP depend clauses with iterators,
   10299              :    lower all the depend clauses by populating corresponding depend
   10300              :    array.  Returns 0 if there are no such depend clauses, or
   10301              :    2 if all depend clauses should be removed, 1 otherwise.  */
   10302              : 
   10303              : static int
   10304         1905 : gimplify_omp_depend (tree *list_p, gimple_seq *pre_p)
   10305              : {
   10306         1905 :   tree c;
   10307         1905 :   gimple *g;
   10308         1905 :   size_t n[5] = { 0, 0, 0, 0, 0 };
   10309         1905 :   bool unused[5];
   10310         1905 :   tree counts[5] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
   10311         1905 :   tree last_iter = NULL_TREE, last_count = NULL_TREE;
   10312         1905 :   size_t i, j;
   10313         1905 :   location_t first_loc = UNKNOWN_LOCATION;
   10314              : 
   10315         6254 :   for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
   10316         4349 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
   10317              :       {
   10318         2207 :         switch (OMP_CLAUSE_DEPEND_KIND (c))
   10319              :           {
   10320              :           case OMP_CLAUSE_DEPEND_IN:
   10321              :             i = 2;
   10322              :             break;
   10323              :           case OMP_CLAUSE_DEPEND_OUT:
   10324              :           case OMP_CLAUSE_DEPEND_INOUT:
   10325              :             i = 0;
   10326              :             break;
   10327              :           case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
   10328              :             i = 1;
   10329              :             break;
   10330              :           case OMP_CLAUSE_DEPEND_DEPOBJ:
   10331              :             i = 3;
   10332              :             break;
   10333              :           case OMP_CLAUSE_DEPEND_INOUTSET:
   10334              :             i = 4;
   10335              :             break;
   10336            0 :           default:
   10337            0 :             gcc_unreachable ();
   10338              :           }
   10339         2207 :         tree t = OMP_CLAUSE_DECL (c);
   10340         2207 :         if (first_loc == UNKNOWN_LOCATION)
   10341         1905 :           first_loc = OMP_CLAUSE_LOCATION (c);
   10342         2207 :         if (OMP_ITERATOR_DECL_P (t))
   10343              :           {
   10344          302 :             if (TREE_PURPOSE (t) != last_iter)
   10345              :               {
   10346          255 :                 tree tcnt = compute_omp_iterator_count (TREE_PURPOSE (t),
   10347              :                                                         pre_p);
   10348          255 :                 if (!tcnt)
   10349              :                   return 2;
   10350          255 :                 last_iter = TREE_PURPOSE (t);
   10351          255 :                 last_count = tcnt;
   10352              :               }
   10353          302 :             if (counts[i] == NULL_TREE)
   10354          248 :               counts[i] = last_count;
   10355              :             else
   10356           54 :               counts[i] = size_binop_loc (OMP_CLAUSE_LOCATION (c),
   10357              :                                           PLUS_EXPR, counts[i], last_count);
   10358              :           }
   10359              :         else
   10360         1905 :           n[i]++;
   10361              :       }
   10362        10620 :   for (i = 0; i < 5; i++)
   10363         8925 :     if (counts[i])
   10364              :       break;
   10365         1905 :   if (i == 5)
   10366              :     return 0;
   10367              : 
   10368          210 :   tree total = size_zero_node;
   10369         1260 :   for (i = 0; i < 5; i++)
   10370              :     {
   10371         1050 :       unused[i] = counts[i] == NULL_TREE && n[i] == 0;
   10372         1050 :       if (counts[i] == NULL_TREE)
   10373          802 :         counts[i] = size_zero_node;
   10374         1050 :       if (n[i])
   10375           44 :         counts[i] = size_binop (PLUS_EXPR, counts[i], size_int (n[i]));
   10376         1050 :       if (gimplify_expr (&counts[i], pre_p, NULL, is_gimple_val,
   10377              :                          fb_rvalue) == GS_ERROR)
   10378              :         return 2;
   10379         1050 :       total = size_binop (PLUS_EXPR, total, counts[i]);
   10380              :     }
   10381              : 
   10382          210 :   if (gimplify_expr (&total, pre_p, NULL, is_gimple_val, fb_rvalue)
   10383              :       == GS_ERROR)
   10384              :     return 2;
   10385          210 :   bool is_old = unused[1] && unused[3] && unused[4];
   10386          210 :   tree totalpx = size_binop (PLUS_EXPR, unshare_expr (total),
   10387              :                              size_int (is_old ? 1 : 4));
   10388          210 :   if (!unused[4])
   10389            3 :     totalpx = size_binop (PLUS_EXPR, totalpx,
   10390              :                           size_binop (MULT_EXPR, counts[4], size_int (2)));
   10391          210 :   tree type = build_array_type (ptr_type_node, build_index_type (totalpx));
   10392          210 :   tree array = create_tmp_var_raw (type);
   10393          210 :   TREE_ADDRESSABLE (array) = 1;
   10394          210 :   if (!poly_int_tree_p (totalpx))
   10395              :     {
   10396           83 :       if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (array)))
   10397           83 :         gimplify_type_sizes (TREE_TYPE (array), pre_p);
   10398           83 :       if (gimplify_omp_ctxp)
   10399              :         {
   10400              :           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   10401              :           while (ctx
   10402           22 :                  && (ctx->region_type == ORT_WORKSHARE
   10403              :                      || ctx->region_type == ORT_TASKGROUP
   10404           22 :                      || ctx->region_type == ORT_SIMD
   10405           22 :                      || ctx->region_type == ORT_ACC))
   10406            0 :             ctx = ctx->outer_context;
   10407           22 :           if (ctx)
   10408           22 :             omp_add_variable (ctx, array, GOVD_LOCAL | GOVD_SEEN);
   10409              :         }
   10410           83 :       gimplify_vla_decl (array, pre_p);
   10411              :     }
   10412              :   else
   10413          127 :     gimple_add_tmp_var (array);
   10414          210 :   tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
   10415              :                    NULL_TREE);
   10416          210 :   tree tem;
   10417          210 :   if (!is_old)
   10418              :     {
   10419           31 :       tem = build2 (MODIFY_EXPR, void_type_node, r,
   10420              :                     build_int_cst (ptr_type_node, 0));
   10421           31 :       gimplify_and_add (tem, pre_p);
   10422           31 :       r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
   10423              :                   NULL_TREE);
   10424              :     }
   10425          210 :   tem = build2 (MODIFY_EXPR, void_type_node, r,
   10426              :                 fold_convert (ptr_type_node, total));
   10427          210 :   gimplify_and_add (tem, pre_p);
   10428          816 :   for (i = 1; i < (is_old ? 2 : 4); i++)
   10429              :     {
   10430          272 :       r = build4 (ARRAY_REF, ptr_type_node, array, size_int (i + !is_old),
   10431              :                   NULL_TREE, NULL_TREE);
   10432          272 :       tem = build2 (MODIFY_EXPR, void_type_node, r, counts[i - 1]);
   10433          272 :       gimplify_and_add (tem, pre_p);
   10434              :     }
   10435              : 
   10436              :   tree cnts[6];
   10437          740 :   for (j = 5; j; j--)
   10438          740 :     if (!unused[j - 1])
   10439              :       break;
   10440         1260 :   for (i = 0; i < 5; i++)
   10441              :     {
   10442         1050 :       if (i && (i >= j || unused[i - 1]))
   10443              :         {
   10444          790 :           cnts[i] = cnts[i - 1];
   10445          790 :           continue;
   10446              :         }
   10447          260 :       cnts[i] = create_tmp_var (sizetype);
   10448          260 :       if (i == 0)
   10449          241 :         g = gimple_build_assign (cnts[i], size_int (is_old ? 2 : 5));
   10450              :       else
   10451              :         {
   10452           50 :           tree t;
   10453           50 :           if (is_old)
   10454           38 :             t = size_binop (PLUS_EXPR, counts[0], size_int (2));
   10455              :           else
   10456           12 :             t = size_binop (PLUS_EXPR, cnts[i - 1], counts[i - 1]);
   10457           50 :           if (gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue)
   10458              :               == GS_ERROR)
   10459            0 :             return 2;
   10460           50 :           g = gimple_build_assign (cnts[i], t);
   10461              :         }
   10462          260 :       gimple_seq_add_stmt (pre_p, g);
   10463              :     }
   10464          210 :   if (unused[4])
   10465          207 :     cnts[5] = NULL_TREE;
   10466              :   else
   10467              :     {
   10468            3 :       tree t = size_binop (PLUS_EXPR, total, size_int (5));
   10469            3 :       cnts[5] = create_tmp_var (sizetype);
   10470            3 :       g = gimple_build_assign (cnts[i], t);
   10471            3 :       gimple_seq_add_stmt (pre_p, g);
   10472              :     }
   10473              : 
   10474          210 :   last_iter = NULL_TREE;
   10475          210 :   tree last_bind = NULL_TREE;
   10476          210 :   tree *last_body = NULL;
   10477          573 :   for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
   10478          363 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
   10479              :       {
   10480          348 :         switch (OMP_CLAUSE_DEPEND_KIND (c))
   10481              :           {
   10482              :           case OMP_CLAUSE_DEPEND_IN:
   10483              :             i = 2;
   10484              :             break;
   10485              :           case OMP_CLAUSE_DEPEND_OUT:
   10486              :           case OMP_CLAUSE_DEPEND_INOUT:
   10487              :             i = 0;
   10488              :             break;
   10489              :           case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
   10490              :             i = 1;
   10491              :             break;
   10492              :           case OMP_CLAUSE_DEPEND_DEPOBJ:
   10493              :             i = 3;
   10494              :             break;
   10495              :           case OMP_CLAUSE_DEPEND_INOUTSET:
   10496              :             i = 4;
   10497              :             break;
   10498            0 :           default:
   10499            0 :             gcc_unreachable ();
   10500              :           }
   10501          348 :         tree t = OMP_CLAUSE_DECL (c);
   10502          348 :         if (OMP_ITERATOR_DECL_P (t))
   10503              :           {
   10504          302 :             if (TREE_PURPOSE (t) != last_iter)
   10505              :               {
   10506          255 :                 last_body = build_omp_iterator_loop (TREE_PURPOSE (t), pre_p,
   10507              :                                                      &last_bind);
   10508          255 :                 SET_EXPR_LOCATION (last_bind, OMP_CLAUSE_LOCATION (c));
   10509              :               }
   10510          302 :             last_iter = TREE_PURPOSE (t);
   10511          302 :             if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
   10512              :               {
   10513            0 :                 append_to_statement_list (TREE_OPERAND (TREE_VALUE (t),
   10514              :                                           0), last_body);
   10515            0 :                 TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
   10516              :               }
   10517          302 :             if (error_operand_p (TREE_VALUE (t)))
   10518              :               return 2;
   10519          302 :             if (TREE_VALUE (t) != null_pointer_node)
   10520          296 :               TREE_VALUE (t) = build_fold_addr_expr (TREE_VALUE (t));
   10521          302 :             if (i == 4)
   10522              :               {
   10523            3 :                 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
   10524              :                             NULL_TREE, NULL_TREE);
   10525            3 :                 tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
   10526              :                                   NULL_TREE, NULL_TREE);
   10527            3 :                 r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
   10528            3 :                 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
   10529              :                                   void_type_node, r, r2);
   10530            3 :                 append_to_statement_list_force (tem, last_body);
   10531            3 :                 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
   10532              :                                   void_type_node, cnts[i],
   10533              :                                   size_binop (PLUS_EXPR, cnts[i],
   10534              :                                               size_int (1)));
   10535            3 :                 append_to_statement_list_force (tem, last_body);
   10536            3 :                 i = 5;
   10537              :               }
   10538          302 :             r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
   10539              :                         NULL_TREE, NULL_TREE);
   10540          604 :             tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
   10541          302 :                               void_type_node, r, TREE_VALUE (t));
   10542          302 :             append_to_statement_list_force (tem, last_body);
   10543          302 :             if (i == 5)
   10544              :               {
   10545            3 :                 r = build4 (ARRAY_REF, ptr_type_node, array,
   10546              :                             size_binop (PLUS_EXPR, cnts[i], size_int (1)),
   10547              :                             NULL_TREE, NULL_TREE);
   10548            3 :                 tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
   10549            3 :                 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
   10550              :                                   void_type_node, r, tem);
   10551            3 :                 append_to_statement_list_force (tem, last_body);
   10552              :               }
   10553          302 :             tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
   10554              :                               void_type_node, cnts[i],
   10555          302 :                               size_binop (PLUS_EXPR, cnts[i],
   10556              :                                           size_int (1 + (i == 5))));
   10557          302 :             append_to_statement_list_force (tem, last_body);
   10558          302 :             TREE_VALUE (t) = null_pointer_node;
   10559              :           }
   10560              :         else
   10561              :           {
   10562           46 :             if (last_bind)
   10563              :               {
   10564           18 :                 gimplify_and_add (last_bind, pre_p);
   10565           18 :                 last_bind = NULL_TREE;
   10566              :               }
   10567           46 :             if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
   10568              :               {
   10569            0 :                 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
   10570              :                                NULL, is_gimple_val, fb_rvalue);
   10571            0 :                 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
   10572              :               }
   10573           46 :             if (error_operand_p (OMP_CLAUSE_DECL (c)))
   10574              :               return 2;
   10575           46 :             if (OMP_CLAUSE_DECL (c) != null_pointer_node)
   10576           46 :               OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
   10577           46 :             if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
   10578              :                                is_gimple_val, fb_rvalue) == GS_ERROR)
   10579              :               return 2;
   10580           46 :             if (i == 4)
   10581              :               {
   10582            0 :                 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
   10583              :                             NULL_TREE, NULL_TREE);
   10584            0 :                 tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
   10585              :                                   NULL_TREE, NULL_TREE);
   10586            0 :                 r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
   10587            0 :                 tem = build2 (MODIFY_EXPR, void_type_node, r, r2);
   10588            0 :                 gimplify_and_add (tem, pre_p);
   10589            0 :                 g = gimple_build_assign (cnts[i], size_binop (PLUS_EXPR,
   10590              :                                                               cnts[i],
   10591              :                                                               size_int (1)));
   10592            0 :                 gimple_seq_add_stmt (pre_p, g);
   10593            0 :                 i = 5;
   10594              :               }
   10595           46 :             r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
   10596              :                         NULL_TREE, NULL_TREE);
   10597           46 :             tem = build2 (MODIFY_EXPR, void_type_node, r, OMP_CLAUSE_DECL (c));
   10598           46 :             gimplify_and_add (tem, pre_p);
   10599           46 :             if (i == 5)
   10600              :               {
   10601            0 :                 r = build4 (ARRAY_REF, ptr_type_node, array,
   10602              :                             size_binop (PLUS_EXPR, cnts[i], size_int (1)),
   10603              :                             NULL_TREE, NULL_TREE);
   10604            0 :                 tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
   10605            0 :                 tem = build2 (MODIFY_EXPR, void_type_node, r, tem);
   10606            0 :                 append_to_statement_list_force (tem, last_body);
   10607            0 :                 gimplify_and_add (tem, pre_p);
   10608              :               }
   10609           46 :             g = gimple_build_assign (cnts[i],
   10610           46 :                                      size_binop (PLUS_EXPR, cnts[i],
   10611              :                                                  size_int (1 + (i == 5))));
   10612           46 :             gimple_seq_add_stmt (pre_p, g);
   10613              :           }
   10614              :       }
   10615          210 :   if (last_bind)
   10616          206 :     gimplify_and_add (last_bind, pre_p);
   10617          210 :   tree cond = boolean_false_node;
   10618          210 :   if (is_old)
   10619              :     {
   10620          179 :       if (!unused[0])
   10621           92 :         cond = build2_loc (first_loc, NE_EXPR, boolean_type_node, cnts[0],
   10622              :                            size_binop_loc (first_loc, PLUS_EXPR, counts[0],
   10623              :                                            size_int (2)));
   10624          179 :       if (!unused[2])
   10625          125 :         cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
   10626              :                            build2_loc (first_loc, NE_EXPR, boolean_type_node,
   10627              :                                        cnts[2],
   10628              :                                        size_binop_loc (first_loc, PLUS_EXPR,
   10629              :                                                        totalpx,
   10630              :                                                        size_int (1))));
   10631              :     }
   10632              :   else
   10633              :     {
   10634           31 :       tree prev = size_int (5);
   10635          186 :       for (i = 0; i < 5; i++)
   10636              :         {
   10637          155 :           if (unused[i])
   10638          112 :             continue;
   10639           43 :           prev = size_binop_loc (first_loc, PLUS_EXPR, counts[i], prev);
   10640           43 :           cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
   10641              :                              build2_loc (first_loc, NE_EXPR, boolean_type_node,
   10642              :                                          cnts[i], unshare_expr (prev)));
   10643              :         }
   10644              :     }
   10645          210 :   tem = build3_loc (first_loc, COND_EXPR, void_type_node, cond,
   10646              :                     build_call_expr_loc (first_loc,
   10647              :                                          builtin_decl_explicit (BUILT_IN_TRAP),
   10648              :                                          0), void_node);
   10649          210 :   gimplify_and_add (tem, pre_p);
   10650          210 :   c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
   10651          210 :   OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
   10652          210 :   OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
   10653          210 :   OMP_CLAUSE_CHAIN (c) = *list_p;
   10654          210 :   *list_p = c;
   10655          210 :   return 1;
   10656              : }
   10657              : 
   10658              : /* True if mapping node C maps, or unmaps, a (Fortran) array descriptor.  */
   10659              : 
   10660              : static bool
   10661       127734 : omp_map_clause_descriptor_p (tree c)
   10662              : {
   10663       127734 :   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
   10664              :     return false;
   10665              : 
   10666       127730 :   if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TO_PSET)
   10667              :     return true;
   10668              : 
   10669        84320 :   if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_RELEASE
   10670        76282 :        || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DELETE)
   10671        84802 :       && OMP_CLAUSE_RELEASE_DESCRIPTOR (c))
   10672         6135 :     return true;
   10673              : 
   10674              :   return false;
   10675              : }
   10676              : 
   10677              : /* For a set of mappings describing an array section pointed to by a struct
   10678              :    (or derived type, etc.) component, create an "alloc" or "release" node to
   10679              :    insert into a list following a GOMP_MAP_STRUCT node.  For some types of
   10680              :    mapping (e.g. Fortran arrays with descriptors), an additional mapping may
   10681              :    be created that is inserted into the list of mapping nodes attached to the
   10682              :    directive being processed -- not part of the sorted list of nodes after
   10683              :    GOMP_MAP_STRUCT.
   10684              : 
   10685              :    CODE is the code of the directive being processed.  GRP_START and GRP_END
   10686              :    are the first and last of two or three nodes representing this array section
   10687              :    mapping (e.g. a data movement node like GOMP_MAP_{TO,FROM}, optionally a
   10688              :    GOMP_MAP_TO_PSET, and finally a GOMP_MAP_ALWAYS_POINTER).  EXTRA_NODE is
   10689              :    filled with the additional node described above, if needed.
   10690              : 
   10691              :    This function does not add the new nodes to any lists itself.  It is the
   10692              :    responsibility of the caller to do that.  */
   10693              : 
   10694              : static tree
   10695         1729 : build_omp_struct_comp_nodes (enum tree_code code, tree grp_start, tree grp_end,
   10696              :                              tree *extra_node)
   10697              : {
   10698         1524 :   enum gomp_map_kind mkind
   10699         1729 :     = (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
   10700         1729 :       ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
   10701              : 
   10702         1729 :   gcc_assert (grp_start != grp_end);
   10703              : 
   10704         1729 :   tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
   10705         1729 :   OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
   10706         1729 :   OMP_CLAUSE_DECL (c2) = unshare_expr (OMP_CLAUSE_DECL (grp_end));
   10707         1729 :   OMP_CLAUSE_CHAIN (c2) = NULL_TREE;
   10708         1729 :   tree grp_mid = NULL_TREE;
   10709         1729 :   if (OMP_CLAUSE_CHAIN (grp_start) != grp_end)
   10710          197 :     grp_mid = OMP_CLAUSE_CHAIN (grp_start);
   10711              : 
   10712          197 :   if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
   10713            0 :     OMP_CLAUSE_SIZE (c2) = OMP_CLAUSE_SIZE (grp_mid);
   10714              :   else
   10715         1729 :     OMP_CLAUSE_SIZE (c2) = TYPE_SIZE_UNIT (ptr_type_node);
   10716              : 
   10717         1729 :   if (grp_mid
   10718          197 :       && OMP_CLAUSE_CODE (grp_mid) == OMP_CLAUSE_MAP
   10719         1926 :       && OMP_CLAUSE_MAP_KIND (grp_mid) == GOMP_MAP_ALWAYS_POINTER)
   10720              :     {
   10721            0 :       tree c3
   10722            0 :         = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
   10723            0 :       OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
   10724            0 :       OMP_CLAUSE_DECL (c3) = unshare_expr (OMP_CLAUSE_DECL (grp_mid));
   10725            0 :       OMP_CLAUSE_SIZE (c3) = TYPE_SIZE_UNIT (ptr_type_node);
   10726            0 :       OMP_CLAUSE_CHAIN (c3) = NULL_TREE;
   10727              : 
   10728            0 :       *extra_node = c3;
   10729              :     }
   10730              :   else
   10731         1729 :     *extra_node = NULL_TREE;
   10732              : 
   10733         1729 :   return c2;
   10734              : }
   10735              : 
   10736              : /* Strip ARRAY_REFS or an indirect ref off BASE, find the containing object,
   10737              :    and set *BITPOSP and *POFFSETP to the bit offset of the access.
   10738              :    If BASE_REF is non-NULL and the containing object is a reference, set
   10739              :    *BASE_REF to that reference before dereferencing the object.
   10740              :    If BASE_REF is NULL, check that the containing object is a COMPONENT_REF or
   10741              :    has array type, else return NULL.  */
   10742              : 
   10743              : static tree
   10744         7758 : extract_base_bit_offset (tree base, poly_int64 *bitposp,
   10745              :                          poly_offset_int *poffsetp,
   10746              :                          bool *variable_offset,
   10747              :                          tree iterator)
   10748              : {
   10749         7758 :   tree offset;
   10750         7758 :   poly_int64 bitsize, bitpos;
   10751         7758 :   machine_mode mode;
   10752         7758 :   int unsignedp, reversep, volatilep = 0;
   10753         7758 :   poly_offset_int poffset;
   10754              : 
   10755         7758 :   STRIP_NOPS (base);
   10756              : 
   10757         7758 :   if (iterator)
   10758              :     {
   10759              :       /* Replace any iterator variables with the lower bound of the iterator.
   10760              :          This will give us the nominal offset and bit position of the first
   10761              :          element, which is all we should need to lay out the mappings.  The
   10762              :          actual locations of the iterated mappings are elsewhere.  */
   10763              :       tree it;
   10764           72 :       for (it = iterator; it; it = TREE_CHAIN (it))
   10765           39 :         base = simplify_replace_tree (base, OMP_ITERATOR_VAR (it),
   10766           39 :                                       OMP_ITERATOR_BEGIN (it));
   10767              :     }
   10768              : 
   10769         7758 :   base = get_inner_reference (base, &bitsize, &bitpos, &offset, &mode,
   10770              :                               &unsignedp, &reversep, &volatilep);
   10771              : 
   10772         7758 :   STRIP_NOPS (base);
   10773              : 
   10774         7758 :   if (offset && poly_int_tree_p (offset))
   10775              :     {
   10776            0 :       poffset = wi::to_poly_offset (offset);
   10777            0 :       *variable_offset = false;
   10778              :     }
   10779              :   else
   10780              :     {
   10781         7758 :       poffset = 0;
   10782         7758 :       *variable_offset = (offset != NULL_TREE);
   10783              :     }
   10784              : 
   10785         7758 :   if (maybe_ne (bitpos, 0))
   10786         5633 :     poffset += bits_to_bytes_round_down (bitpos);
   10787              : 
   10788         7758 :   *bitposp = bitpos;
   10789         7758 :   *poffsetp = poffset;
   10790              : 
   10791         7758 :   return base;
   10792              : }
   10793              : 
   10794              : /* Used for topological sorting of mapping groups.  UNVISITED means we haven't
   10795              :    started processing the group yet.  The TEMPORARY mark is used when we first
   10796              :    encounter a group on a depth-first traversal, and the PERMANENT mark is used
   10797              :    when we have processed all the group's children (i.e. all the base pointers
   10798              :    referred to by the group's mapping nodes, recursively).  */
   10799              : 
   10800              : enum omp_tsort_mark {
   10801              :   UNVISITED,
   10802              :   TEMPORARY,
   10803              :   PERMANENT
   10804              : };
   10805              : 
   10806              : /* Hash for trees based on operand_equal_p.  Like tree_operand_hash
   10807              :    but ignores side effects in the equality comparisons.  */
   10808              : 
   10809              : struct tree_operand_hash_no_se : tree_operand_hash
   10810              : {
   10811              :   static inline bool equal (const value_type &,
   10812              :                             const compare_type &);
   10813              : };
   10814              : 
   10815              : inline bool
   10816       419212 : tree_operand_hash_no_se::equal (const value_type &t1,
   10817              :                                 const compare_type &t2)
   10818              : {
   10819       419212 :   return operand_equal_p (t1, t2, OEP_MATCH_SIDE_EFFECTS);
   10820              : }
   10821              : 
   10822              : /* A group of OMP_CLAUSE_MAP nodes that correspond to a single "map"
   10823              :    clause.  */
   10824              : 
   10825              : struct omp_mapping_group {
   10826              :   tree *grp_start;
   10827              :   tree grp_end;
   10828              :   omp_tsort_mark mark;
   10829              :   /* If we've removed the group but need to reindex, mark the group as
   10830              :      deleted.  */
   10831              :   bool deleted;
   10832              :   /* The group points to an already-created "GOMP_MAP_STRUCT
   10833              :      GOMP_MAP_ATTACH_DETACH" pair.  */
   10834              :   bool reprocess_struct;
   10835              :   /* The group should use "zero-length" allocations for pointers that are not
   10836              :      mapped "to" on the same directive.  */
   10837              :   bool fragile;
   10838              :   struct omp_mapping_group *sibling;
   10839              :   struct omp_mapping_group *next;
   10840              : };
   10841              : 
   10842              : DEBUG_FUNCTION void
   10843            0 : debug_mapping_group (omp_mapping_group *grp)
   10844              : {
   10845            0 :   tree tmp = OMP_CLAUSE_CHAIN (grp->grp_end);
   10846            0 :   OMP_CLAUSE_CHAIN (grp->grp_end) = NULL;
   10847            0 :   debug_generic_expr (*grp->grp_start);
   10848            0 :   OMP_CLAUSE_CHAIN (grp->grp_end) = tmp;
   10849            0 : }
   10850              : 
   10851              : /* Return the OpenMP "base pointer" of an expression EXPR, or NULL if there
   10852              :    isn't one.  */
   10853              : 
   10854              : static tree
   10855        38831 : omp_get_base_pointer (tree expr)
   10856              : {
   10857        38831 :   while (TREE_CODE (expr) == ARRAY_REF
   10858        46250 :          || TREE_CODE (expr) == COMPONENT_REF)
   10859         7419 :     expr = TREE_OPERAND (expr, 0);
   10860              : 
   10861        38831 :   if (INDIRECT_REF_P (expr)
   10862        38831 :       || (TREE_CODE (expr) == MEM_REF
   10863            1 :           && integer_zerop (TREE_OPERAND (expr, 1))))
   10864              :     {
   10865        10678 :       expr = TREE_OPERAND (expr, 0);
   10866        10715 :       while (TREE_CODE (expr) == COMPOUND_EXPR)
   10867           37 :         expr = TREE_OPERAND (expr, 1);
   10868        10678 :       if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
   10869          841 :         expr = TREE_OPERAND (expr, 0);
   10870        10678 :       if (TREE_CODE (expr) == SAVE_EXPR)
   10871           31 :         expr = TREE_OPERAND (expr, 0);
   10872        10678 :       STRIP_NOPS (expr);
   10873        10678 :       return expr;
   10874              :     }
   10875              : 
   10876              :   return NULL_TREE;
   10877              : }
   10878              : 
   10879              : /* An attach or detach operation depends directly on the address being
   10880              :    attached/detached.  Return that address, or none if there are no
   10881              :    attachments/detachments.  */
   10882              : 
   10883              : static tree
   10884        18069 : omp_get_attachment (omp_mapping_group *grp)
   10885              : {
   10886        18069 :   tree node = *grp->grp_start;
   10887              : 
   10888        18069 :   switch (OMP_CLAUSE_MAP_KIND (node))
   10889              :     {
   10890        15098 :     case GOMP_MAP_TO:
   10891        15098 :     case GOMP_MAP_FROM:
   10892        15098 :     case GOMP_MAP_TOFROM:
   10893        15098 :     case GOMP_MAP_ALWAYS_FROM:
   10894        15098 :     case GOMP_MAP_ALWAYS_TO:
   10895        15098 :     case GOMP_MAP_ALWAYS_TOFROM:
   10896        15098 :     case GOMP_MAP_FORCE_FROM:
   10897        15098 :     case GOMP_MAP_FORCE_TO:
   10898        15098 :     case GOMP_MAP_FORCE_TOFROM:
   10899        15098 :     case GOMP_MAP_FORCE_PRESENT:
   10900        15098 :     case GOMP_MAP_PRESENT_ALLOC:
   10901        15098 :     case GOMP_MAP_PRESENT_FROM:
   10902        15098 :     case GOMP_MAP_PRESENT_TO:
   10903        15098 :     case GOMP_MAP_PRESENT_TOFROM:
   10904        15098 :     case GOMP_MAP_ALWAYS_PRESENT_FROM:
   10905        15098 :     case GOMP_MAP_ALWAYS_PRESENT_TO:
   10906        15098 :     case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
   10907        15098 :     case GOMP_MAP_ALLOC:
   10908        15098 :     case GOMP_MAP_RELEASE:
   10909        15098 :     case GOMP_MAP_DELETE:
   10910        15098 :     case GOMP_MAP_FORCE_ALLOC:
   10911        15098 :       if (node == grp->grp_end)
   10912              :         return NULL_TREE;
   10913              : 
   10914         6729 :       node = OMP_CLAUSE_CHAIN (node);
   10915         6729 :       if (node && omp_map_clause_descriptor_p (node))
   10916              :         {
   10917         1054 :           gcc_assert (node != grp->grp_end);
   10918         1054 :           node = OMP_CLAUSE_CHAIN (node);
   10919              :         }
   10920         6729 :       if (node)
   10921         6729 :         switch (OMP_CLAUSE_MAP_KIND (node))
   10922              :           {
   10923              :           case GOMP_MAP_POINTER:
   10924              :           case GOMP_MAP_ALWAYS_POINTER:
   10925              :           case GOMP_MAP_FIRSTPRIVATE_POINTER:
   10926              :           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
   10927              :           case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
   10928              :           case GOMP_MAP_USES_ALLOCATORS:
   10929              :             return NULL_TREE;
   10930              : 
   10931         2559 :           case GOMP_MAP_ATTACH_DETACH:
   10932         2559 :           case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
   10933         2559 :           case GOMP_MAP_DETACH:
   10934         2559 :             return OMP_CLAUSE_DECL (node);
   10935              : 
   10936            0 :           default:
   10937            0 :             internal_error ("unexpected mapping node");
   10938              :           }
   10939            0 :       return error_mark_node;
   10940              : 
   10941            0 :     case GOMP_MAP_TO_PSET:
   10942            0 :       gcc_assert (node != grp->grp_end);
   10943            0 :       node = OMP_CLAUSE_CHAIN (node);
   10944            0 :       if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
   10945            0 :           || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
   10946            0 :         return OMP_CLAUSE_DECL (node);
   10947              :       else
   10948            0 :         internal_error ("unexpected mapping node");
   10949              :       return error_mark_node;
   10950              : 
   10951          541 :     case GOMP_MAP_ATTACH:
   10952          541 :     case GOMP_MAP_DETACH:
   10953          541 :       node = OMP_CLAUSE_CHAIN (node);
   10954          541 :       if (!node || *grp->grp_start == grp->grp_end)
   10955          541 :         return OMP_CLAUSE_DECL (*grp->grp_start);
   10956            0 :       if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
   10957            0 :           || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
   10958            0 :         return OMP_CLAUSE_DECL (*grp->grp_start);
   10959              :       else
   10960            0 :         internal_error ("unexpected mapping node");
   10961              :       return error_mark_node;
   10962              : 
   10963              :     case GOMP_MAP_STRUCT:
   10964              :     case GOMP_MAP_STRUCT_UNORD:
   10965              :     case GOMP_MAP_FORCE_DEVICEPTR:
   10966              :     case GOMP_MAP_DEVICE_RESIDENT:
   10967              :     case GOMP_MAP_LINK:
   10968              :     case GOMP_MAP_IF_PRESENT:
   10969              :     case GOMP_MAP_FIRSTPRIVATE:
   10970              :     case GOMP_MAP_FIRSTPRIVATE_INT:
   10971              :     case GOMP_MAP_USE_DEVICE_PTR:
   10972              :     case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
   10973              :     case GOMP_MAP_USES_ALLOCATORS:
   10974              :       return NULL_TREE;
   10975              : 
   10976            0 :     default:
   10977            0 :       internal_error ("unexpected mapping node");
   10978              :     }
   10979              : 
   10980              :   return error_mark_node;
   10981              : }
   10982              : 
   10983              : /* Given a pointer START_P to the start of a group of related (e.g. pointer)
   10984              :    mappings, return the chain pointer to the end of that group in the list.  */
   10985              : 
   10986              : static tree *
   10987       116170 : omp_group_last (tree *start_p)
   10988              : {
   10989       116170 :   tree c = *start_p, nc, *grp_last_p = start_p;
   10990              : 
   10991       116170 :   gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
   10992              : 
   10993       116170 :   nc = OMP_CLAUSE_CHAIN (c);
   10994              : 
   10995       200805 :   if (!nc || OMP_CLAUSE_CODE (nc) != OMP_CLAUSE_MAP)
   10996              :     return grp_last_p;
   10997              : 
   10998        72098 :   switch (OMP_CLAUSE_MAP_KIND (c))
   10999              :     {
   11000              :     default:
   11001              :       while (nc
   11002       115376 :              && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
   11003       242275 :              && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
   11004       108198 :                  || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
   11005        97297 :                  || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH
   11006        84470 :                  || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_POINTER
   11007        58310 :                  || (OMP_CLAUSE_MAP_KIND (nc)
   11008              :                      == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
   11009        58294 :                  || (OMP_CLAUSE_MAP_KIND (nc)
   11010              :                      == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)
   11011        57927 :                  || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH
   11012        57872 :                  || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ALWAYS_POINTER
   11013        55904 :                  || omp_map_clause_descriptor_p (nc)))
   11014              :         {
   11015        67459 :           tree nc2 = OMP_CLAUSE_CHAIN (nc);
   11016        67459 :           if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH)
   11017              :             {
   11018              :               /* In the specific case we're doing "exit data" on an array
   11019              :                  slice of a reference-to-pointer struct component, we will see
   11020              :                  DETACH followed by ATTACH_DETACH here.  We want to treat that
   11021              :                  as a single group. In other cases DETACH might represent a
   11022              :                  stand-alone "detach" clause, so we don't want to consider
   11023              :                  that part of the group.  */
   11024           55 :               if (nc2
   11025           16 :                   && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
   11026           71 :                   && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH_DETACH)
   11027            0 :                 goto consume_two_nodes;
   11028              :               else
   11029              :                 break;
   11030              :             }
   11031        67404 :           if (nc2
   11032        49372 :               && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
   11033        42859 :               && (OMP_CLAUSE_MAP_KIND (nc)
   11034              :                   == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
   11035        67420 :               && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH)
   11036              :             {
   11037           16 :             consume_two_nodes:
   11038           16 :               grp_last_p = &OMP_CLAUSE_CHAIN (nc);
   11039           16 :               c = nc2;
   11040           16 :               nc = OMP_CLAUSE_CHAIN (nc2);
   11041              :             }
   11042              :           else
   11043              :             {
   11044        67388 :               grp_last_p = &OMP_CLAUSE_CHAIN (c);
   11045        67388 :               c = nc;
   11046        67388 :               nc = nc2;
   11047              :             }
   11048              :         }
   11049              :       break;
   11050              : 
   11051          302 :     case GOMP_MAP_ATTACH:
   11052          302 :     case GOMP_MAP_DETACH:
   11053              :       /* This is a weird artifact of how directives are parsed: bare attach or
   11054              :          detach clauses get a subsequent (meaningless) FIRSTPRIVATE_POINTER or
   11055              :          FIRSTPRIVATE_REFERENCE node.  FIXME.  */
   11056          302 :       if (nc
   11057          302 :           && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
   11058          302 :           && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
   11059          302 :               || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER))
   11060            0 :         grp_last_p = &OMP_CLAUSE_CHAIN (c);
   11061              :       break;
   11062              : 
   11063           39 :     case GOMP_MAP_TO_PSET:
   11064           39 :       if (OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
   11065           39 :           && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH
   11066           12 :               || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH))
   11067           39 :         grp_last_p = &OMP_CLAUSE_CHAIN (c);
   11068              :       break;
   11069              : 
   11070         5749 :     case GOMP_MAP_STRUCT:
   11071         5749 :     case GOMP_MAP_STRUCT_UNORD:
   11072         5749 :       {
   11073         5749 :         unsigned HOST_WIDE_INT num_mappings
   11074         5749 :           = tree_to_uhwi (OMP_CLAUSE_SIZE (c));
   11075         5749 :         if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
   11076         5411 :             || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
   11077        10678 :             || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH)
   11078         1134 :           grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
   11079        14447 :         for (unsigned i = 0; i < num_mappings; i++)
   11080         8698 :           grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
   11081              :       }
   11082              :       break;
   11083              :     }
   11084              : 
   11085              :   return grp_last_p;
   11086              : }
   11087              : 
   11088              : /* Walk through LIST_P, and return a list of groups of mappings found (e.g.
   11089              :    OMP_CLAUSE_MAP with GOMP_MAP_{TO/FROM/TOFROM} followed by one or two
   11090              :    associated GOMP_MAP_POINTER mappings).  Return a vector of omp_mapping_group
   11091              :    if we have more than one such group, else return NULL.  */
   11092              : 
   11093              : static void
   11094        92950 : omp_gather_mapping_groups_1 (tree *list_p, vec<omp_mapping_group> *groups,
   11095              :                              tree gather_sentinel)
   11096              : {
   11097        92950 :   for (tree *cp = list_p;
   11098       280620 :        *cp && *cp != gather_sentinel;
   11099       187670 :        cp = &OMP_CLAUSE_CHAIN (*cp))
   11100              :     {
   11101       187670 :       if (OMP_CLAUSE_CODE (*cp) != OMP_CLAUSE_MAP)
   11102       104442 :         continue;
   11103              : 
   11104        83228 :       tree *grp_last_p = omp_group_last (cp);
   11105        83228 :       omp_mapping_group grp;
   11106              : 
   11107        83228 :       grp.grp_start = cp;
   11108        83228 :       grp.grp_end = *grp_last_p;
   11109        83228 :       grp.mark = UNVISITED;
   11110        83228 :       grp.sibling = NULL;
   11111        83228 :       grp.deleted = false;
   11112        83228 :       grp.reprocess_struct = false;
   11113        83228 :       grp.fragile = false;
   11114        83228 :       grp.next = NULL;
   11115        83228 :       groups->safe_push (grp);
   11116              : 
   11117        83228 :       cp = grp_last_p;
   11118              :     }
   11119        92950 : }
   11120              : 
   11121              : static vec<omp_mapping_group> *
   11122        92632 : omp_gather_mapping_groups (tree *list_p)
   11123              : {
   11124        92632 :   vec<omp_mapping_group> *groups = new vec<omp_mapping_group> ();
   11125              : 
   11126        92632 :   omp_gather_mapping_groups_1 (list_p, groups, NULL_TREE);
   11127              : 
   11128        92632 :   if (groups->length () > 0)
   11129              :     return groups;
   11130              :   else
   11131              :     {
   11132        45190 :       delete groups;
   11133        45190 :       return NULL;
   11134              :     }
   11135              : }
   11136              : 
   11137              : /* A pointer mapping group GRP may define a block of memory starting at some
   11138              :    base address, and maybe also define a firstprivate pointer or firstprivate
   11139              :    reference that points to that block.  The return value is a node containing
   11140              :    the former, and the *FIRSTPRIVATE pointer is set if we have the latter.
   11141              :    If we define several base pointers, i.e. for a GOMP_MAP_STRUCT mapping,
   11142              :    return the number of consecutive chained nodes in CHAINED.  */
   11143              : 
   11144              : static tree
   11145        83622 : omp_group_base (omp_mapping_group *grp, unsigned int *chained,
   11146              :                 tree *firstprivate)
   11147              : {
   11148        83622 :   tree node = *grp->grp_start;
   11149              : 
   11150        83622 :   *firstprivate = NULL_TREE;
   11151        83622 :   *chained = 1;
   11152              : 
   11153        83622 :   switch (OMP_CLAUSE_MAP_KIND (node))
   11154              :     {
   11155        79413 :     case GOMP_MAP_TO:
   11156        79413 :     case GOMP_MAP_FROM:
   11157        79413 :     case GOMP_MAP_TOFROM:
   11158        79413 :     case GOMP_MAP_ALWAYS_FROM:
   11159        79413 :     case GOMP_MAP_ALWAYS_TO:
   11160        79413 :     case GOMP_MAP_ALWAYS_TOFROM:
   11161        79413 :     case GOMP_MAP_FORCE_FROM:
   11162        79413 :     case GOMP_MAP_FORCE_TO:
   11163        79413 :     case GOMP_MAP_FORCE_TOFROM:
   11164        79413 :     case GOMP_MAP_FORCE_PRESENT:
   11165        79413 :     case GOMP_MAP_PRESENT_ALLOC:
   11166        79413 :     case GOMP_MAP_PRESENT_FROM:
   11167        79413 :     case GOMP_MAP_PRESENT_TO:
   11168        79413 :     case GOMP_MAP_PRESENT_TOFROM:
   11169        79413 :     case GOMP_MAP_ALWAYS_PRESENT_FROM:
   11170        79413 :     case GOMP_MAP_ALWAYS_PRESENT_TO:
   11171        79413 :     case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
   11172        79413 :     case GOMP_MAP_ALLOC:
   11173        79413 :     case GOMP_MAP_RELEASE:
   11174        79413 :     case GOMP_MAP_DELETE:
   11175        79413 :     case GOMP_MAP_FORCE_ALLOC:
   11176        79413 :     case GOMP_MAP_IF_PRESENT:
   11177        79413 :       if (node == grp->grp_end)
   11178              :         return node;
   11179              : 
   11180        35104 :       node = OMP_CLAUSE_CHAIN (node);
   11181        35104 :       if (!node)
   11182            0 :         internal_error ("unexpected mapping node");
   11183        35104 :       if (omp_map_clause_descriptor_p (node))
   11184              :         {
   11185        10802 :           if (node == grp->grp_end)
   11186            0 :             return *grp->grp_start;
   11187        10802 :           node = OMP_CLAUSE_CHAIN (node);
   11188              :         }
   11189        35104 :       switch (OMP_CLAUSE_MAP_KIND (node))
   11190              :         {
   11191        23749 :         case GOMP_MAP_POINTER:
   11192        23749 :         case GOMP_MAP_FIRSTPRIVATE_POINTER:
   11193        23749 :         case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
   11194        23749 :         case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
   11195        23749 :           *firstprivate = OMP_CLAUSE_DECL (node);
   11196        23749 :           return *grp->grp_start;
   11197              : 
   11198        11355 :         case GOMP_MAP_ALWAYS_POINTER:
   11199        11355 :         case GOMP_MAP_ATTACH_DETACH:
   11200        11355 :         case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
   11201        11355 :         case GOMP_MAP_DETACH:
   11202        11355 :           return *grp->grp_start;
   11203              : 
   11204            0 :         default:
   11205            0 :           internal_error ("unexpected mapping node");
   11206              :         }
   11207              :       return error_mark_node;
   11208              : 
   11209           26 :     case GOMP_MAP_TO_PSET:
   11210           26 :       gcc_assert (node != grp->grp_end);
   11211           26 :       node = OMP_CLAUSE_CHAIN (node);
   11212           26 :       if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
   11213           26 :           || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
   11214              :         return NULL_TREE;
   11215              :       else
   11216            0 :         internal_error ("unexpected mapping node");
   11217              :       return error_mark_node;
   11218              : 
   11219         1109 :     case GOMP_MAP_ATTACH:
   11220         1109 :     case GOMP_MAP_DETACH:
   11221         1109 :       node = OMP_CLAUSE_CHAIN (node);
   11222         1109 :       if (!node || *grp->grp_start == grp->grp_end)
   11223              :         return NULL_TREE;
   11224            0 :       if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
   11225            0 :           || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
   11226              :         {
   11227              :           /* We're mapping the base pointer itself in a bare attach or detach
   11228              :              node.  This is a side effect of how parsing works, and the mapping
   11229              :              will be removed anyway (at least for enter/exit data directives).
   11230              :              We should ignore the mapping here.  FIXME.  */
   11231              :           return NULL_TREE;
   11232              :         }
   11233              :       else
   11234            0 :         internal_error ("unexpected mapping node");
   11235              :       return error_mark_node;
   11236              : 
   11237         2790 :     case GOMP_MAP_STRUCT:
   11238         2790 :     case GOMP_MAP_STRUCT_UNORD:
   11239         2790 :       {
   11240         2790 :         unsigned HOST_WIDE_INT num_mappings
   11241         2790 :           = tree_to_uhwi (OMP_CLAUSE_SIZE (node));
   11242         2790 :         node = OMP_CLAUSE_CHAIN (node);
   11243         2790 :         if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
   11244         2790 :             || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
   11245              :           {
   11246          393 :             *firstprivate = OMP_CLAUSE_DECL (node);
   11247          393 :             node = OMP_CLAUSE_CHAIN (node);
   11248              :           }
   11249         2397 :         else if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH_DETACH)
   11250          314 :           node = OMP_CLAUSE_CHAIN (node);
   11251         2790 :         *chained = num_mappings;
   11252         2790 :         return node;
   11253              :       }
   11254              : 
   11255              :     case GOMP_MAP_FORCE_DEVICEPTR:
   11256              :     case GOMP_MAP_DEVICE_RESIDENT:
   11257              :     case GOMP_MAP_LINK:
   11258              :     case GOMP_MAP_FIRSTPRIVATE:
   11259              :     case GOMP_MAP_FIRSTPRIVATE_INT:
   11260              :     case GOMP_MAP_USE_DEVICE_PTR:
   11261              :     case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
   11262              :     case GOMP_MAP_USES_ALLOCATORS:
   11263              :       return NULL_TREE;
   11264              : 
   11265            0 :     case GOMP_MAP_FIRSTPRIVATE_POINTER:
   11266            0 :     case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
   11267            0 :     case GOMP_MAP_POINTER:
   11268            0 :     case GOMP_MAP_ALWAYS_POINTER:
   11269            0 :     case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
   11270              :       /* These shouldn't appear by themselves.  */
   11271            0 :       if (!seen_error ())
   11272            0 :         internal_error ("unexpected pointer mapping node");
   11273            0 :       return error_mark_node;
   11274              : 
   11275            0 :     default:
   11276            0 :       gcc_unreachable ();
   11277              :     }
   11278              : 
   11279              :   return error_mark_node;
   11280              : }
   11281              : 
   11282              : /* Given a vector of omp_mapping_groups, build a hash table so we can look up
   11283              :    nodes by tree_operand_hash_no_se.  */
   11284              : 
   11285              : static void
   11286        47926 : omp_index_mapping_groups_1 (hash_map<tree_operand_hash_no_se,
   11287              :                                      omp_mapping_group *> *grpmap,
   11288              :                             vec<omp_mapping_group> *groups,
   11289              :                             tree reindex_sentinel)
   11290              : {
   11291        47926 :   omp_mapping_group *grp;
   11292        47926 :   unsigned int i;
   11293        47926 :   bool reindexing = reindex_sentinel != NULL_TREE, above_hwm = false;
   11294              : 
   11295       132002 :   FOR_EACH_VEC_ELT (*groups, i, grp)
   11296              :     {
   11297        84076 :       if (reindexing && *grp->grp_start == reindex_sentinel)
   11298        84076 :         above_hwm = true;
   11299              : 
   11300        84076 :       if (reindexing && !above_hwm)
   11301        59934 :         continue;
   11302              : 
   11303        83776 :       if (grp->reprocess_struct)
   11304          154 :         continue;
   11305              : 
   11306        83622 :       tree fpp;
   11307        83622 :       unsigned int chained;
   11308        83622 :       tree node = omp_group_base (grp, &chained, &fpp);
   11309              : 
   11310        83622 :       if (node == error_mark_node || (!node && !fpp))
   11311         1419 :         continue;
   11312              : 
   11313              :       for (unsigned j = 0;
   11314       165853 :            node && j < chained;
   11315        83650 :            node = OMP_CLAUSE_CHAIN (node), j++)
   11316              :         {
   11317        83650 :           tree decl = OMP_CLAUSE_DECL (node);
   11318              :           /* Sometimes we see zero-offset MEM_REF instead of INDIRECT_REF,
   11319              :              meaning node-hash lookups don't work.  This is a workaround for
   11320              :              that, but ideally we should just create the INDIRECT_REF at
   11321              :              source instead.  FIXME.  */
   11322        83650 :           if (TREE_CODE (decl) == MEM_REF
   11323        83650 :               && integer_zerop (TREE_OPERAND (decl, 1)))
   11324            0 :             decl = build_fold_indirect_ref (TREE_OPERAND (decl, 0));
   11325              : 
   11326        83650 :           omp_mapping_group **prev = grpmap->get (decl);
   11327              : 
   11328        83650 :           if (prev && *prev == grp)
   11329              :             /* Empty.  */;
   11330        83650 :           else if (prev)
   11331              :             {
   11332              :               /* Mapping the same thing twice is normally diagnosed as an error,
   11333              :                  but can happen under some circumstances, e.g. in pr99928-16.c,
   11334              :                  the directive:
   11335              : 
   11336              :                  #pragma omp target simd reduction(+:a[:3]) \
   11337              :                                          map(always, tofrom: a[:6])
   11338              :                  ...
   11339              : 
   11340              :                  will result in two "a[0]" mappings (of different sizes).  */
   11341              : 
   11342          326 :               grp->sibling = (*prev)->sibling;
   11343          326 :               (*prev)->sibling = grp;
   11344              :             }
   11345              :           else
   11346        83324 :             grpmap->put (decl, grp);
   11347              :         }
   11348              : 
   11349        82203 :       if (!fpp)
   11350        58061 :         continue;
   11351              : 
   11352        24142 :       omp_mapping_group **prev = grpmap->get (fpp);
   11353        24142 :       if (prev && *prev != grp)
   11354              :         {
   11355           12 :           grp->sibling = (*prev)->sibling;
   11356           12 :           (*prev)->sibling = grp;
   11357              :         }
   11358              :       else
   11359        24130 :         grpmap->put (fpp, grp);
   11360              :     }
   11361        47926 : }
   11362              : 
   11363              : static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
   11364        47442 : omp_index_mapping_groups (vec<omp_mapping_group> *groups)
   11365              : {
   11366        47442 :   hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
   11367        47442 :     = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
   11368              : 
   11369        47442 :   omp_index_mapping_groups_1 (grpmap, groups, NULL_TREE);
   11370              : 
   11371        47442 :   return grpmap;
   11372              : }
   11373              : 
   11374              : /* Rebuild group map from partially-processed clause list (during
   11375              :    omp_build_struct_sibling_lists).  We have already processed nodes up until
   11376              :    a high-water mark (HWM).  This is a bit tricky because the list is being
   11377              :    reordered as it is scanned, but we know:
   11378              : 
   11379              :    1. The list after HWM has not been touched yet, so we can reindex it safely.
   11380              : 
   11381              :    2. The list before and including HWM has been altered, but remains
   11382              :       well-formed throughout the sibling-list building operation.
   11383              : 
   11384              :    so, we can do the reindex operation in two parts, on the processed and
   11385              :    then the unprocessed halves of the list.  */
   11386              : 
   11387              : static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
   11388          318 : omp_reindex_mapping_groups (tree *list_p,
   11389              :                             vec<omp_mapping_group> *groups,
   11390              :                             vec<omp_mapping_group> *processed_groups,
   11391              :                             tree sentinel)
   11392              : {
   11393          318 :   hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
   11394          318 :     = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
   11395              : 
   11396          318 :   processed_groups->truncate (0);
   11397              : 
   11398          318 :   omp_gather_mapping_groups_1 (list_p, processed_groups, sentinel);
   11399          318 :   omp_index_mapping_groups_1 (grpmap, processed_groups, NULL_TREE);
   11400          318 :   if (sentinel)
   11401          166 :     omp_index_mapping_groups_1 (grpmap, groups, sentinel);
   11402              : 
   11403          318 :   return grpmap;
   11404              : }
   11405              : 
   11406              : /* Find the immediately-containing struct for a component ref (etc.)
   11407              :    expression EXPR.  */
   11408              : 
   11409              : static tree
   11410        49361 : omp_containing_struct (tree expr)
   11411              : {
   11412        49361 :   tree expr0 = expr;
   11413              : 
   11414        49361 :   STRIP_NOPS (expr);
   11415              : 
   11416              :   /* Note: don't strip NOPs unless we're also stripping off array refs or a
   11417              :      component ref.  */
   11418        49361 :   if (TREE_CODE (expr) != ARRAY_REF && TREE_CODE (expr) != COMPONENT_REF)
   11419              :     return expr0;
   11420              : 
   11421        27458 :   while (TREE_CODE (expr) == ARRAY_REF)
   11422         3727 :     expr = TREE_OPERAND (expr, 0);
   11423              : 
   11424        23731 :   if (TREE_CODE (expr) == COMPONENT_REF)
   11425        21229 :     expr = TREE_OPERAND (expr, 0);
   11426              : 
   11427              :   return expr;
   11428              : }
   11429              : 
   11430              : /* Return TRUE if DECL describes a component that is part of a whole structure
   11431              :    that is mapped elsewhere in GRPMAP.  *MAPPED_BY_GROUP is set to the group
   11432              :    that maps that structure, if present.  */
   11433              : 
   11434              : static bool
   11435        26928 : omp_mapped_by_containing_struct (hash_map<tree_operand_hash_no_se,
   11436              :                                           omp_mapping_group *> *grpmap,
   11437              :                                  tree decl,
   11438              :                                  omp_mapping_group **mapped_by_group)
   11439              : {
   11440        26928 :   tree wsdecl = NULL_TREE;
   11441              : 
   11442        26928 :   *mapped_by_group = NULL;
   11443              : 
   11444        71794 :   while (true)
   11445              :     {
   11446        49361 :       wsdecl = omp_containing_struct (decl);
   11447        49361 :       if (wsdecl == decl)
   11448              :         break;
   11449        23731 :       omp_mapping_group **wholestruct = grpmap->get (wsdecl);
   11450        23731 :       if (!wholestruct
   11451        21103 :           && TREE_CODE (wsdecl) == MEM_REF
   11452        23732 :           && integer_zerop (TREE_OPERAND (wsdecl, 1)))
   11453              :         {
   11454            1 :           tree deref = TREE_OPERAND (wsdecl, 0);
   11455            1 :           deref = build_fold_indirect_ref (deref);
   11456            1 :           wholestruct = grpmap->get (deref);
   11457              :         }
   11458        23731 :       if (wholestruct)
   11459              :         {
   11460              :           /* An intermediate descriptor should not match here because the
   11461              :              pointee is actually not mapped by this group -- it is just a
   11462              :              zero-length alloc.  */
   11463         2628 :           tree desc = OMP_CLAUSE_CHAIN (*(*wholestruct)->grp_start);
   11464         2628 :           if (desc != NULL_TREE && omp_map_clause_descriptor_p (desc))
   11465         1330 :             goto next;
   11466         1298 :           *mapped_by_group = *wholestruct;
   11467         1298 :           return true;
   11468              :         }
   11469        21103 :     next:
   11470        22433 :       decl = wsdecl;
   11471        22433 :     }
   11472              : 
   11473              :   return false;
   11474              : }
   11475              : 
   11476              : /* Helper function for omp_tsort_mapping_groups.  Returns TRUE on success, or
   11477              :    FALSE on error.  */
   11478              : 
   11479              : static bool
   11480        21421 : omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
   11481              :                             vec<omp_mapping_group> *groups,
   11482              :                             hash_map<tree_operand_hash_no_se,
   11483              :                                      omp_mapping_group *> *grpmap,
   11484              :                             omp_mapping_group *grp)
   11485              : {
   11486        21421 :   if (grp->mark == PERMANENT)
   11487              :     return true;
   11488        18069 :   if (grp->mark == TEMPORARY)
   11489              :     {
   11490            0 :       fprintf (stderr, "when processing group:\n");
   11491            0 :       debug_mapping_group (grp);
   11492            0 :       internal_error ("base pointer cycle detected");
   11493              :       return false;
   11494              :     }
   11495        18069 :   grp->mark = TEMPORARY;
   11496              : 
   11497        18069 :   tree attaches_to = omp_get_attachment (grp);
   11498              : 
   11499        18069 :   if (attaches_to)
   11500              :     {
   11501         3100 :       omp_mapping_group **basep = grpmap->get (attaches_to);
   11502              : 
   11503         3100 :       if (basep && *basep != grp)
   11504              :         {
   11505         2924 :           for (omp_mapping_group *w = *basep; w; w = w->sibling)
   11506         1462 :             if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
   11507              :               return false;
   11508              :         }
   11509              :     }
   11510              : 
   11511        18069 :   tree decl = OMP_CLAUSE_DECL (*grp->grp_start);
   11512              : 
   11513        24358 :   while (decl)
   11514              :     {
   11515        24358 :       tree base = omp_get_base_pointer (decl);
   11516              : 
   11517        24358 :       if (!base)
   11518              :         break;
   11519              : 
   11520         9066 :       omp_mapping_group **innerp = grpmap->get (base);
   11521         9066 :       omp_mapping_group *wholestruct;
   11522              : 
   11523              :       /* We should treat whole-structure mappings as if all (pointer, in this
   11524              :          case) members are mapped as individual list items.  Check if we have
   11525              :          such a whole-structure mapping, if we don't have an explicit reference
   11526              :          to the pointer member itself.  */
   11527         9066 :       if (!innerp
   11528         4186 :           && TREE_CODE (base) == COMPONENT_REF
   11529        11312 :           && omp_mapped_by_containing_struct (grpmap, base, &wholestruct))
   11530              :         innerp = &wholestruct;
   11531              : 
   11532         9066 :       if (innerp && *innerp != grp)
   11533              :         {
   11534         5566 :           for (omp_mapping_group *w = *innerp; w; w = w->sibling)
   11535         2789 :             if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
   11536            0 :               return false;
   11537              :           break;
   11538              :         }
   11539              : 
   11540         6289 :       decl = base;
   11541              :     }
   11542              : 
   11543        18069 :   grp->mark = PERMANENT;
   11544              : 
   11545              :   /* Emit grp to output list.  */
   11546              : 
   11547        18069 :   **outlist = grp;
   11548        18069 :   *outlist = &grp->next;
   11549              : 
   11550        18069 :   return true;
   11551              : }
   11552              : 
   11553              : /* Topologically sort GROUPS, so that OMP 5.0-defined base pointers come
   11554              :    before mappings that use those pointers.  This is an implementation of the
   11555              :    depth-first search algorithm, described e.g. at:
   11556              : 
   11557              :      https://en.wikipedia.org/wiki/Topological_sorting
   11558              : */
   11559              : 
   11560              : static omp_mapping_group *
   11561         9439 : omp_tsort_mapping_groups (vec<omp_mapping_group> *groups,
   11562              :                           hash_map<tree_operand_hash_no_se, omp_mapping_group *>
   11563              :                             *grpmap,
   11564              :                           bool enter_exit_data)
   11565              : {
   11566         9439 :   omp_mapping_group *grp, *outlist = NULL, **cursor;
   11567         9439 :   unsigned int i;
   11568         9439 :   bool saw_runtime_implicit = false;
   11569              : 
   11570         9439 :   cursor = &outlist;
   11571              : 
   11572        27508 :   FOR_EACH_VEC_ELT (*groups, i, grp)
   11573              :     {
   11574        18069 :       if (grp->mark != PERMANENT)
   11575              :         {
   11576        17194 :           if (OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
   11577              :             {
   11578          683 :               saw_runtime_implicit = true;
   11579          683 :               continue;
   11580              :             }
   11581        16511 :           if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
   11582              :             return NULL;
   11583              :         }
   11584              :     }
   11585              : 
   11586         9439 :   if (!saw_runtime_implicit)
   11587         9157 :     return outlist;
   11588              : 
   11589         1634 :   FOR_EACH_VEC_ELT (*groups, i, grp)
   11590              :     {
   11591         1352 :       if (grp->mark != PERMANENT
   11592         1352 :           && OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
   11593              :         {
   11594              :           /* Clear the flag for enter/exit data because it is currently
   11595              :              meaningless for those operations in libgomp.  */
   11596          659 :           if (enter_exit_data)
   11597          446 :             OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start) = 0;
   11598              : 
   11599          659 :           if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
   11600              :             return NULL;
   11601              :         }
   11602              :     }
   11603              : 
   11604          282 :   return outlist;
   11605              : }
   11606              : 
   11607              : /* Split INLIST into three parts:
   11608              : 
   11609              :      - "present" alloc/to/from groups
   11610              :      - other to/from groups
   11611              :      - other alloc/release/delete groups
   11612              : 
   11613              :    These sub-lists are then concatenated together to form the final list.
   11614              :    Each sub-list retains the order of the original list.
   11615              :    Note that ATTACH nodes are later moved to the end of the list in
   11616              :    gimplify_adjust_omp_clauses, for target regions.  */
   11617              : 
   11618              : static omp_mapping_group *
   11619         9439 : omp_segregate_mapping_groups (omp_mapping_group *inlist)
   11620              : {
   11621         9439 :   omp_mapping_group *ard_groups = NULL, *tf_groups = NULL;
   11622         9439 :   omp_mapping_group *p_groups = NULL;
   11623         9439 :   omp_mapping_group **ard_tail = &ard_groups, **tf_tail = &tf_groups;
   11624         9439 :   omp_mapping_group **p_tail = &p_groups;
   11625              : 
   11626        27508 :   for (omp_mapping_group *w = inlist; w;)
   11627              :     {
   11628        18069 :       tree c = *w->grp_start;
   11629        18069 :       omp_mapping_group *next = w->next;
   11630              : 
   11631        18069 :       gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
   11632              : 
   11633        18069 :       switch (OMP_CLAUSE_MAP_KIND (c))
   11634              :         {
   11635         1939 :         case GOMP_MAP_ALLOC:
   11636         1939 :         case GOMP_MAP_RELEASE:
   11637         1939 :         case GOMP_MAP_DELETE:
   11638         1939 :           *ard_tail = w;
   11639         1939 :           w->next = NULL;
   11640         1939 :           ard_tail = &w->next;
   11641         1939 :           break;
   11642              : 
   11643              :         /* These map types are all semantically identical, so are moved into a
   11644              :            single group.  They will each be changed into GOMP_MAP_FORCE_PRESENT
   11645              :            in gimplify_adjust_omp_clauses.  */
   11646          151 :         case GOMP_MAP_PRESENT_ALLOC:
   11647          151 :         case GOMP_MAP_PRESENT_FROM:
   11648          151 :         case GOMP_MAP_PRESENT_TO:
   11649          151 :         case GOMP_MAP_PRESENT_TOFROM:
   11650          151 :           *p_tail = w;
   11651          151 :           w->next = NULL;
   11652          151 :           p_tail = &w->next;
   11653          151 :           break;
   11654              : 
   11655        15979 :         default:
   11656        15979 :           *tf_tail = w;
   11657        15979 :           w->next = NULL;
   11658        15979 :           tf_tail = &w->next;
   11659              :         }
   11660              : 
   11661        18069 :       w = next;
   11662              :     }
   11663              : 
   11664              :   /* Now splice the lists together...  */
   11665         9439 :   *tf_tail = ard_groups;
   11666         9439 :   *p_tail = tf_groups;
   11667              : 
   11668         9439 :   return p_groups;
   11669              : }
   11670              : 
   11671              : /* Given a list LIST_P containing groups of mappings given by GROUPS, reorder
   11672              :    those groups based on the output list of omp_tsort_mapping_groups --
   11673              :    singly-linked, threaded through each element's NEXT pointer starting at
   11674              :    HEAD.  Each list element appears exactly once in that linked list.
   11675              : 
   11676              :    Each element of GROUPS may correspond to one or several mapping nodes.
   11677              :    Node groups are kept together, and in the reordered list, the positions of
   11678              :    the original groups are reused for the positions of the reordered list.
   11679              :    Hence if we have e.g.
   11680              : 
   11681              :      {to ptr ptr} firstprivate {tofrom ptr} ...
   11682              :       ^             ^           ^
   11683              :       first group  non-"map"    second group
   11684              : 
   11685              :    and say the second group contains a base pointer for the first so must be
   11686              :    moved before it, the resulting list will contain:
   11687              : 
   11688              :      {tofrom ptr} firstprivate {to ptr ptr} ...
   11689              :       ^ prev. second group      ^ prev. first group
   11690              : */
   11691              : 
   11692              : static tree *
   11693         9439 : omp_reorder_mapping_groups (vec<omp_mapping_group> *groups,
   11694              :                             omp_mapping_group *head,
   11695              :                             tree *list_p)
   11696              : {
   11697         9439 :   omp_mapping_group *grp;
   11698         9439 :   unsigned int i;
   11699         9439 :   unsigned numgroups = groups->length ();
   11700         9439 :   auto_vec<tree> old_heads (numgroups);
   11701         9439 :   auto_vec<tree *> old_headps (numgroups);
   11702         9439 :   auto_vec<tree> new_heads (numgroups);
   11703         9439 :   auto_vec<tree> old_succs (numgroups);
   11704         9439 :   bool map_at_start = (list_p == (*groups)[0].grp_start);
   11705              : 
   11706         9439 :   tree *new_grp_tail = NULL;
   11707              : 
   11708              :   /* Stash the start & end nodes of each mapping group before we start
   11709              :      modifying the list.  */
   11710        27508 :   FOR_EACH_VEC_ELT (*groups, i, grp)
   11711              :     {
   11712        18069 :       old_headps.quick_push (grp->grp_start);
   11713        18069 :       old_heads.quick_push (*grp->grp_start);
   11714        18069 :       old_succs.quick_push (OMP_CLAUSE_CHAIN (grp->grp_end));
   11715              :     }
   11716              : 
   11717              :   /* And similarly, the heads of the groups in the order we want to rearrange
   11718              :      the list to.  */
   11719        27508 :   for (omp_mapping_group *w = head; w; w = w->next)
   11720        18069 :     new_heads.quick_push (*w->grp_start);
   11721              : 
   11722        27508 :   FOR_EACH_VEC_ELT (*groups, i, grp)
   11723              :     {
   11724        18069 :       gcc_assert (head);
   11725              : 
   11726        18069 :       if (new_grp_tail && old_succs[i - 1] == old_heads[i])
   11727              :         {
   11728              :           /* a {b c d} {e f g} h i j   (original)
   11729              :              -->
   11730              :              a {k l m} {e f g} h i j   (inserted new group on last iter)
   11731              :              -->
   11732              :              a {k l m} {n o p} h i j   (this time, chain last group to new one)
   11733              :                       ^new_grp_tail
   11734              :           */
   11735         8008 :           *new_grp_tail = new_heads[i];
   11736              :         }
   11737        10061 :       else if (new_grp_tail)
   11738              :         {
   11739              :           /* a {b c d} e {f g h} i j k   (original)
   11740              :              -->
   11741              :              a {l m n} e {f g h} i j k   (gap after last iter's group)
   11742              :              -->
   11743              :              a {l m n} e {o p q} h i j   (chain last group to old successor)
   11744              :                       ^new_grp_tail
   11745              :            */
   11746          622 :           *new_grp_tail = old_succs[i - 1];
   11747          622 :           *old_headps[i] = new_heads[i];
   11748              :         }
   11749              :       else
   11750              :         {
   11751              :           /* The first inserted group -- point to new group, and leave end
   11752              :              open.
   11753              :              a {b c d} e f
   11754              :              -->
   11755              :              a {g h i...
   11756              :           */
   11757         9439 :           *grp->grp_start = new_heads[i];
   11758              :         }
   11759              : 
   11760        18069 :       new_grp_tail = &OMP_CLAUSE_CHAIN (head->grp_end);
   11761              : 
   11762        18069 :       head = head->next;
   11763              :     }
   11764              : 
   11765         9439 :   if (new_grp_tail)
   11766         9439 :     *new_grp_tail = old_succs[numgroups - 1];
   11767              : 
   11768         9439 :   gcc_assert (!head);
   11769              : 
   11770        12947 :   return map_at_start ? (*groups)[0].grp_start : list_p;
   11771         9439 : }
   11772              : 
   11773              : /* DECL is supposed to have lastprivate semantics in the outer contexts
   11774              :    of combined/composite constructs, starting with OCTX.
   11775              :    Add needed lastprivate, shared or map clause if no data sharing or
   11776              :    mapping clause are present.  IMPLICIT_P is true if it is an implicit
   11777              :    clause (IV on simd), in which case the lastprivate will not be
   11778              :    copied to some constructs.  */
   11779              : 
   11780              : static void
   11781        15601 : omp_lastprivate_for_combined_outer_constructs (struct gimplify_omp_ctx *octx,
   11782              :                                                tree decl, bool implicit_p)
   11783              : {
   11784        15601 :   struct gimplify_omp_ctx *orig_octx = octx;
   11785        28888 :   for (; octx; octx = octx->outer_context)
   11786              :     {
   11787        28059 :       if ((octx->region_type == ORT_COMBINED_PARALLEL
   11788        22563 :            || (octx->region_type & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS)
   11789        27400 :           && splay_tree_lookup (octx->variables,
   11790              :                                 (splay_tree_key) decl) == NULL)
   11791              :         {
   11792         4257 :           omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
   11793         4257 :           continue;
   11794              :         }
   11795        19793 :       if ((octx->region_type & ORT_TASK) != 0
   11796          575 :           && octx->combined_loop
   11797        20074 :           && splay_tree_lookup (octx->variables,
   11798              :                                 (splay_tree_key) decl) == NULL)
   11799              :         {
   11800          248 :           omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
   11801          248 :           continue;
   11802              :         }
   11803        24381 :       if (implicit_p
   11804        14610 :           && octx->region_type == ORT_WORKSHARE
   11805        10903 :           && octx->combined_loop
   11806        10798 :           && splay_tree_lookup (octx->variables,
   11807              :                                 (splay_tree_key) decl) == NULL
   11808        10798 :           && octx->outer_context
   11809         9405 :           && octx->outer_context->region_type == ORT_COMBINED_PARALLEL
   11810        24388 :           && splay_tree_lookup (octx->outer_context->variables,
   11811              :                                 (splay_tree_key) decl) == NULL)
   11812              :         {
   11813         5084 :           octx = octx->outer_context;
   11814         5084 :           omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
   11815         5084 :           continue;
   11816              :         }
   11817         6202 :       if ((octx->region_type == ORT_WORKSHARE || octx->region_type == ORT_ACC)
   11818         8011 :           && octx->combined_loop
   11819         7824 :           && splay_tree_lookup (octx->variables,
   11820              :                                 (splay_tree_key) decl) == NULL
   11821        20693 :           && !omp_check_private (octx, decl, false))
   11822              :         {
   11823         3698 :           omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
   11824         3698 :           continue;
   11825              :         }
   11826        10515 :       if (octx->region_type == ORT_COMBINED_TARGET)
   11827              :         {
   11828         2066 :           splay_tree_node n = splay_tree_lookup (octx->variables,
   11829              :                                                  (splay_tree_key) decl);
   11830         2066 :           if (n == NULL)
   11831              :             {
   11832         2032 :               omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
   11833         2032 :               octx = octx->outer_context;
   11834              :             }
   11835           34 :           else if (!implicit_p
   11836           22 :                    && (n->value & GOVD_FIRSTPRIVATE_IMPLICIT))
   11837              :             {
   11838           12 :               n->value &= ~(GOVD_FIRSTPRIVATE
   11839              :                             | GOVD_FIRSTPRIVATE_IMPLICIT
   11840              :                             | GOVD_EXPLICIT);
   11841           12 :               omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
   11842           12 :               octx = octx->outer_context;
   11843              :             }
   11844              :         }
   11845              :       break;
   11846              :     }
   11847        15601 :   if (octx && (implicit_p || octx != orig_octx))
   11848         4610 :     omp_notice_variable (octx, decl, true);
   11849        15601 : }
   11850              : 
   11851              : /* We might have indexed several groups for DECL, e.g. a "TO" mapping and also
   11852              :    a "FIRSTPRIVATE" mapping.  Return the one that isn't firstprivate, etc.  */
   11853              : 
   11854              : static omp_mapping_group *
   11855         6060 : omp_get_nonfirstprivate_group (hash_map<tree_operand_hash_no_se,
   11856              :                                         omp_mapping_group *> *grpmap,
   11857              :                                tree decl, bool allow_deleted = false)
   11858              : {
   11859         6060 :   omp_mapping_group **to_group_p = grpmap->get (decl);
   11860              : 
   11861         6060 :   if (!to_group_p)
   11862              :     return NULL;
   11863              : 
   11864         2527 :   omp_mapping_group *to_group = *to_group_p;
   11865              : 
   11866         3932 :   for (; to_group; to_group = to_group->sibling)
   11867              :     {
   11868         2564 :       tree grp_end = to_group->grp_end;
   11869         2564 :       switch (OMP_CLAUSE_MAP_KIND (grp_end))
   11870              :         {
   11871              :         case GOMP_MAP_FIRSTPRIVATE_POINTER:
   11872              :         case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
   11873              :           break;
   11874              : 
   11875         1161 :         default:
   11876         1161 :           if (allow_deleted || !to_group->deleted)
   11877              :             return to_group;
   11878              :         }
   11879              :     }
   11880              : 
   11881              :   return NULL;
   11882              : }
   11883              : 
   11884              : /* Return TRUE if the directive (whose clauses are described by the hash table
   11885              :    of mapping groups, GRPMAP) maps DECL explicitly.  If TO_SPECIFICALLY is
   11886              :    true, only count TO mappings.  If ALLOW_DELETED is true, ignore the
   11887              :    "deleted" flag for groups.  If CONTAINED_IN_STRUCT is true, also return
   11888              :    TRUE if DECL is mapped as a member of a whole-struct mapping.  */
   11889              : 
   11890              : static bool
   11891         4450 : omp_directive_maps_explicitly (hash_map<tree_operand_hash_no_se,
   11892              :                                         omp_mapping_group *> *grpmap,
   11893              :                                tree decl, omp_mapping_group **base_group,
   11894              :                                bool to_specifically, bool allow_deleted,
   11895              :                                bool contained_in_struct)
   11896              : {
   11897         4450 :   omp_mapping_group *decl_group
   11898         4450 :     = omp_get_nonfirstprivate_group (grpmap, decl, allow_deleted);
   11899              : 
   11900         4450 :   *base_group = NULL;
   11901              : 
   11902         4450 :   if (decl_group)
   11903              :     {
   11904         1042 :       tree grp_first = *decl_group->grp_start;
   11905              :       /* We might be called during omp_build_struct_sibling_lists, when
   11906              :          GOMP_MAP_STRUCT might have been inserted at the start of the group.
   11907              :          Skip over that, and also possibly the node after it.  */
   11908         1042 :       if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT
   11909         1042 :           || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT_UNORD)
   11910              :         {
   11911            6 :           grp_first = OMP_CLAUSE_CHAIN (grp_first);
   11912            6 :           if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_FIRSTPRIVATE_POINTER
   11913            6 :               || (OMP_CLAUSE_MAP_KIND (grp_first)
   11914              :                   == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
   11915           12 :               || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_ATTACH_DETACH)
   11916            0 :             grp_first = OMP_CLAUSE_CHAIN (grp_first);
   11917              :         }
   11918         1042 :       enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
   11919         1042 :       if (!to_specifically
   11920          592 :           || GOMP_MAP_COPY_TO_P (first_kind)
   11921          382 :           || first_kind == GOMP_MAP_ALLOC)
   11922              :         {
   11923          960 :           *base_group = decl_group;
   11924          960 :           return true;
   11925              :         }
   11926              :     }
   11927              : 
   11928         3490 :   if (contained_in_struct
   11929         3490 :       && omp_mapped_by_containing_struct (grpmap, decl, base_group))
   11930              :     return true;
   11931              : 
   11932              :   return false;
   11933              : }
   11934              : 
   11935              : /* If we have mappings INNER and OUTER, where INNER is a component access and
   11936              :    OUTER is a mapping of the whole containing struct, check that the mappings
   11937              :    are compatible.  We'll be deleting the inner mapping, so we need to make
   11938              :    sure the outer mapping does (at least) the same transfers to/from the device
   11939              :    as the inner mapping.  */
   11940              : 
   11941              : bool
   11942          172 : omp_check_mapping_compatibility (location_t loc,
   11943              :                                  omp_mapping_group *outer,
   11944              :                                  omp_mapping_group *inner)
   11945              : {
   11946          172 :   tree first_outer = *outer->grp_start, first_inner = *inner->grp_start;
   11947              : 
   11948          172 :   gcc_assert (OMP_CLAUSE_CODE (first_outer) == OMP_CLAUSE_MAP);
   11949          172 :   gcc_assert (OMP_CLAUSE_CODE (first_inner) == OMP_CLAUSE_MAP);
   11950              : 
   11951          172 :   enum gomp_map_kind outer_kind = OMP_CLAUSE_MAP_KIND (first_outer);
   11952          172 :   enum gomp_map_kind inner_kind = OMP_CLAUSE_MAP_KIND (first_inner);
   11953              : 
   11954          172 :   if (outer_kind == inner_kind)
   11955              :     return true;
   11956              : 
   11957           70 :   switch (outer_kind)
   11958              :     {
   11959            0 :     case GOMP_MAP_ALWAYS_TO:
   11960            0 :       if (inner_kind == GOMP_MAP_FORCE_PRESENT
   11961            0 :           || inner_kind == GOMP_MAP_ALLOC
   11962            0 :           || inner_kind == GOMP_MAP_TO)
   11963              :         return true;
   11964              :       break;
   11965              : 
   11966            0 :     case GOMP_MAP_ALWAYS_FROM:
   11967            0 :       if (inner_kind == GOMP_MAP_FORCE_PRESENT
   11968            0 :           || inner_kind == GOMP_MAP_RELEASE
   11969              :           || inner_kind == GOMP_MAP_FROM)
   11970              :         return true;
   11971              :       break;
   11972              : 
   11973           10 :     case GOMP_MAP_TO:
   11974           10 :       if (inner_kind == GOMP_MAP_FORCE_PRESENT
   11975           10 :           || inner_kind == GOMP_MAP_ALLOC)
   11976              :         return true;
   11977              :       break;
   11978              : 
   11979            8 :     case GOMP_MAP_FROM:
   11980            8 :       if (inner_kind == GOMP_MAP_RELEASE
   11981            8 :           || inner_kind == GOMP_MAP_FORCE_PRESENT)
   11982              :         return true;
   11983              :       break;
   11984              : 
   11985           32 :     case GOMP_MAP_ALWAYS_TOFROM:
   11986           32 :     case GOMP_MAP_TOFROM:
   11987           32 :       if (inner_kind == GOMP_MAP_FORCE_PRESENT
   11988           32 :           || inner_kind == GOMP_MAP_ALLOC
   11989              :           || inner_kind == GOMP_MAP_TO
   11990           24 :           || inner_kind == GOMP_MAP_FROM
   11991           12 :           || inner_kind == GOMP_MAP_TOFROM)
   11992              :         return true;
   11993              :       break;
   11994              : 
   11995           28 :     default:
   11996           28 :       ;
   11997              :     }
   11998              : 
   11999           84 :   error_at (loc, "data movement for component %qE is not compatible with "
   12000           28 :             "movement for struct %qE", OMP_CLAUSE_DECL (first_inner),
   12001           28 :             OMP_CLAUSE_DECL (first_outer));
   12002              : 
   12003           28 :   return false;
   12004              : }
   12005              : 
   12006              : /* This function handles several cases where clauses on a mapping directive
   12007              :    can interact with each other.
   12008              : 
   12009              :    If we have a FIRSTPRIVATE_POINTER node and we're also mapping the pointer
   12010              :    on the same directive, change the mapping of the first node to
   12011              :    ATTACH_DETACH.  We should have detected that this will happen already in
   12012              :    c-omp.cc:c_omp_adjust_map_clauses and marked the appropriate decl
   12013              :    as addressable.  (If we didn't, bail out.)
   12014              : 
   12015              :    If we have a FIRSTPRIVATE_REFERENCE (for a reference to pointer) and we're
   12016              :    mapping the base pointer also, we may need to change the mapping type to
   12017              :    ATTACH_DETACH and synthesize an alloc node for the reference itself.
   12018              : 
   12019              :    If we have an ATTACH_DETACH node, this is an array section with a pointer
   12020              :    base.  If we're mapping the base on the same directive too, we can drop its
   12021              :    mapping.  However, if we have a reference to pointer, make other appropriate
   12022              :    adjustments to the mapping nodes instead.
   12023              : 
   12024              :    If we have an ATTACH_DETACH node with a Fortran pointer-set (array
   12025              :    descriptor) mapping for a derived-type component, and we're also mapping the
   12026              :    whole of the derived-type variable on another clause, the pointer-set
   12027              :    mapping is removed.
   12028              : 
   12029              :    If we have a component access but we're also mapping the whole of the
   12030              :    containing struct, drop the former access.
   12031              : 
   12032              :    If the expression is a component access, and we're also mapping a base
   12033              :    pointer used in that component access in the same expression, change the
   12034              :    mapping type of the latter to ALLOC (ready for processing by
   12035              :    omp_build_struct_sibling_lists).  */
   12036              : 
   12037              : void
   12038         9439 : omp_resolve_clause_dependencies (enum tree_code code,
   12039              :                                  vec<omp_mapping_group> *groups,
   12040              :                                  hash_map<tree_operand_hash_no_se,
   12041              :                                           omp_mapping_group *> *grpmap)
   12042              : {
   12043         9439 :   int i;
   12044         9439 :   omp_mapping_group *grp;
   12045         9439 :   bool repair_chain = false;
   12046              : 
   12047        26447 :   FOR_EACH_VEC_ELT (*groups, i, grp)
   12048              :     {
   12049        17008 :       tree grp_end = grp->grp_end;
   12050        17008 :       tree decl = OMP_CLAUSE_DECL (grp_end);
   12051              : 
   12052        17008 :       gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
   12053              : 
   12054        17008 :       switch (OMP_CLAUSE_MAP_KIND (grp_end))
   12055              :         {
   12056         1294 :         case GOMP_MAP_FIRSTPRIVATE_POINTER:
   12057         1294 :           {
   12058         1294 :             omp_mapping_group *to_group
   12059         1294 :               = omp_get_nonfirstprivate_group (grpmap, decl);
   12060              : 
   12061         1294 :             if (!to_group || to_group == grp)
   12062         1269 :               continue;
   12063              : 
   12064           25 :             tree grp_first = *to_group->grp_start;
   12065           25 :             enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
   12066              : 
   12067           25 :             if ((GOMP_MAP_COPY_TO_P (first_kind)
   12068            7 :                  || first_kind == GOMP_MAP_ALLOC)
   12069           50 :                 && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
   12070              :                     != GOMP_MAP_FIRSTPRIVATE_POINTER))
   12071              :               {
   12072           25 :                 gcc_assert (TREE_ADDRESSABLE (OMP_CLAUSE_DECL (grp_end)));
   12073           25 :                 OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
   12074              :               }
   12075              :           }
   12076              :           break;
   12077              : 
   12078          160 :         case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
   12079          160 :           {
   12080          160 :             tree ptr = build_fold_indirect_ref (decl);
   12081              : 
   12082          160 :             omp_mapping_group *to_group
   12083          160 :               = omp_get_nonfirstprivate_group (grpmap, ptr);
   12084              : 
   12085          160 :             if (!to_group || to_group == grp)
   12086          156 :               continue;
   12087              : 
   12088            4 :             tree grp_first = *to_group->grp_start;
   12089            4 :             enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
   12090              : 
   12091            4 :             if (GOMP_MAP_COPY_TO_P (first_kind)
   12092            4 :                 || first_kind == GOMP_MAP_ALLOC)
   12093              :               {
   12094            4 :                 OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
   12095            4 :                 OMP_CLAUSE_DECL (grp_end) = ptr;
   12096            4 :                 if ((OMP_CLAUSE_CHAIN (*to_group->grp_start)
   12097            4 :                      == to_group->grp_end)
   12098            4 :                     && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
   12099              :                         == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
   12100              :                   {
   12101            0 :                     gcc_assert (TREE_ADDRESSABLE
   12102              :                                   (OMP_CLAUSE_DECL (to_group->grp_end)));
   12103            0 :                     OMP_CLAUSE_SET_MAP_KIND (to_group->grp_end,
   12104              :                                              GOMP_MAP_ATTACH_DETACH);
   12105              : 
   12106            0 :                     location_t loc = OMP_CLAUSE_LOCATION (to_group->grp_end);
   12107            0 :                     tree alloc
   12108            0 :                       = build_omp_clause (loc, OMP_CLAUSE_MAP);
   12109            0 :                     OMP_CLAUSE_SET_MAP_KIND (alloc, GOMP_MAP_ALLOC);
   12110            0 :                     tree tmp = build_fold_addr_expr (OMP_CLAUSE_DECL
   12111              :                                                       (to_group->grp_end));
   12112            0 :                     tree char_ptr_type = build_pointer_type (char_type_node);
   12113            0 :                     OMP_CLAUSE_DECL (alloc)
   12114            0 :                       = build2 (MEM_REF, char_type_node,
   12115              :                                 tmp,
   12116              :                                 build_int_cst (char_ptr_type, 0));
   12117            0 :                     OMP_CLAUSE_SIZE (alloc) = TYPE_SIZE_UNIT (TREE_TYPE (tmp));
   12118              : 
   12119            0 :                     OMP_CLAUSE_CHAIN (alloc)
   12120            0 :                       = OMP_CLAUSE_CHAIN (*to_group->grp_start);
   12121            0 :                     OMP_CLAUSE_CHAIN (*to_group->grp_start) = alloc;
   12122              :                   }
   12123              :               }
   12124              :           }
   12125              :           break;
   12126              : 
   12127              :         case GOMP_MAP_ATTACH_DETACH:
   12128              :         case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
   12129              :           {
   12130         2626 :             tree base_ptr, referenced_ptr_node = NULL_TREE;
   12131              : 
   12132         2626 :             while (TREE_CODE (decl) == ARRAY_REF)
   12133            0 :               decl = TREE_OPERAND (decl, 0);
   12134              : 
   12135         2626 :             if (TREE_CODE (decl) == INDIRECT_REF)
   12136           13 :               decl = TREE_OPERAND (decl, 0);
   12137              : 
   12138              :             /* Only component accesses.  */
   12139         2626 :             if (DECL_P (decl))
   12140          230 :               continue;
   12141              : 
   12142              :             /* We want the pointer itself when checking if the base pointer is
   12143              :                mapped elsewhere in the same directive -- if we have a
   12144              :                reference to the pointer, don't use that.  */
   12145              : 
   12146         2396 :             if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
   12147         2396 :                 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
   12148              :               {
   12149          394 :                 referenced_ptr_node = OMP_CLAUSE_CHAIN (*grp->grp_start);
   12150          394 :                 base_ptr = OMP_CLAUSE_DECL (referenced_ptr_node);
   12151              :               }
   12152              :             else
   12153              :               base_ptr = decl;
   12154              : 
   12155         1978 :             gomp_map_kind zlas_kind
   12156         2396 :               = (code == OACC_EXIT_DATA || code == OMP_TARGET_EXIT_DATA)
   12157         2396 :                 ? GOMP_MAP_DETACH : GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
   12158              : 
   12159         2396 :             if (TREE_CODE (TREE_TYPE (base_ptr)) == POINTER_TYPE)
   12160              :               {
   12161              :                 /* If we map the base TO, and we're doing an attachment, we can
   12162              :                    skip the TO mapping altogether and create an ALLOC mapping
   12163              :                    instead, since the attachment will overwrite the device
   12164              :                    pointer in that location immediately anyway.  Otherwise,
   12165              :                    change our mapping to
   12166              :                    GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION in case the
   12167              :                    attachment target has not been copied to the device already
   12168              :                    by some earlier directive.  */
   12169              : 
   12170         1972 :                 bool base_mapped_to = false;
   12171              : 
   12172         1972 :                 omp_mapping_group *base_group;
   12173              : 
   12174         1972 :                 if (omp_directive_maps_explicitly (grpmap, base_ptr,
   12175              :                                                    &base_group, false, true,
   12176              :                                                    false))
   12177              :                   {
   12178          450 :                     if (referenced_ptr_node)
   12179              :                       {
   12180          129 :                         base_mapped_to = true;
   12181          129 :                         if ((OMP_CLAUSE_MAP_KIND (base_group->grp_end)
   12182              :                              == GOMP_MAP_ATTACH_DETACH)
   12183          129 :                             && (OMP_CLAUSE_CHAIN (*base_group->grp_start)
   12184              :                                 == base_group->grp_end))
   12185              :                           {
   12186          258 :                             OMP_CLAUSE_CHAIN (*base_group->grp_start)
   12187          129 :                               = OMP_CLAUSE_CHAIN (base_group->grp_end);
   12188          129 :                             base_group->grp_end = *base_group->grp_start;
   12189          129 :                             repair_chain = true;
   12190              :                           }
   12191              :                       }
   12192              :                     else
   12193              :                       {
   12194          321 :                         base_group->deleted = true;
   12195          321 :                         OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end) = 1;
   12196              :                       }
   12197              :                   }
   12198              : 
   12199              :                 /* We're dealing with a reference to a pointer, and we are
   12200              :                    attaching both the reference and the pointer.  We know the
   12201              :                    reference itself is on the target, because we are going to
   12202              :                    create an ALLOC node for it in accumulate_sibling_list.  The
   12203              :                    pointer might be on the target already or it might not, but
   12204              :                    if it isn't then it's not an error, so use
   12205              :                    GOMP_MAP_ATTACH_ZLAS for it.  */
   12206         1972 :                 if (!base_mapped_to && referenced_ptr_node)
   12207           56 :                   OMP_CLAUSE_SET_MAP_KIND (referenced_ptr_node, zlas_kind);
   12208              : 
   12209         1972 :                 omp_mapping_group *struct_group;
   12210         1972 :                 tree desc;
   12211         1972 :                 if ((desc = OMP_CLAUSE_CHAIN (*grp->grp_start))
   12212         1972 :                     && omp_map_clause_descriptor_p (desc)
   12213         3050 :                     && omp_mapped_by_containing_struct (grpmap, decl,
   12214              :                                                         &struct_group))
   12215              :                   /* If we have a pointer set but we're mapping (or unmapping)
   12216              :                      the whole of the containing struct, we can remove the
   12217              :                      pointer set mapping.  */
   12218           15 :                   OMP_CLAUSE_CHAIN (*grp->grp_start) = OMP_CLAUSE_CHAIN (desc);
   12219              :               }
   12220          424 :             else if (TREE_CODE (TREE_TYPE (base_ptr)) == REFERENCE_TYPE
   12221          424 :                      && (TREE_CODE (TREE_TYPE (TREE_TYPE (base_ptr)))
   12222              :                          == ARRAY_TYPE)
   12223          562 :                      && OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION
   12224              :                           (*grp->grp_start))
   12225           92 :               OMP_CLAUSE_SET_MAP_KIND (grp->grp_end, zlas_kind);
   12226              :           }
   12227              :           break;
   12228              : 
   12229              :         case GOMP_MAP_ATTACH:
   12230              :           /* Ignore standalone attach here.  */
   12231              :           break;
   12232              : 
   12233        12861 :         default:
   12234        12861 :           {
   12235        12861 :             omp_mapping_group *struct_group;
   12236        12861 :             if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
   12237        12861 :                 && *grp->grp_start == grp_end)
   12238              :               {
   12239           94 :                 omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
   12240              :                                                  struct_group, grp);
   12241              :                 /* Remove the whole of this mapping -- redundant.  */
   12242           94 :                 grp->deleted = true;
   12243              :               }
   12244              : 
   12245        12861 :             tree base = decl;
   12246        14473 :             while ((base = omp_get_base_pointer (base)))
   12247              :               {
   12248         1612 :                 omp_mapping_group *base_group;
   12249              : 
   12250         1612 :                 if (omp_directive_maps_explicitly (grpmap, base, &base_group,
   12251              :                                                    true, true, false))
   12252              :                   {
   12253          343 :                     tree grp_first = *base_group->grp_start;
   12254          343 :                     OMP_CLAUSE_SET_MAP_KIND (grp_first, GOMP_MAP_ALLOC);
   12255              :                   }
   12256              :               }
   12257              :           }
   12258              :         }
   12259              :     }
   12260              : 
   12261         9439 :   if (repair_chain)
   12262              :     {
   12263              :       /* Group start pointers may have become detached from the
   12264              :          OMP_CLAUSE_CHAIN of previous groups if elements were removed from the
   12265              :          end of those groups.  Fix that now.  */
   12266              :       tree *new_next = NULL;
   12267          716 :       FOR_EACH_VEC_ELT (*groups, i, grp)
   12268              :         {
   12269          587 :           if (new_next)
   12270          458 :             grp->grp_start = new_next;
   12271              : 
   12272          587 :           new_next = &OMP_CLAUSE_CHAIN (grp->grp_end);
   12273              :         }
   12274              :     }
   12275         9439 : }
   12276              : 
   12277              : /* Similar to omp_resolve_clause_dependencies, but for OpenACC.  The only
   12278              :    clause dependencies we handle for now are struct element mappings and
   12279              :    whole-struct mappings on the same directive, and duplicate clause
   12280              :    detection.  */
   12281              : 
   12282              : void
   12283         9462 : oacc_resolve_clause_dependencies (vec<omp_mapping_group> *groups,
   12284              :                                   hash_map<tree_operand_hash_no_se,
   12285              :                                            omp_mapping_group *> *grpmap)
   12286              : {
   12287         9462 :   int i;
   12288         9462 :   omp_mapping_group *grp;
   12289         9462 :   hash_set<tree_operand_hash> *seen_components = NULL;
   12290         9462 :   hash_set<tree_operand_hash> *shown_error = NULL;
   12291              : 
   12292        24643 :   FOR_EACH_VEC_ELT (*groups, i, grp)
   12293              :     {
   12294        15181 :       tree grp_end = grp->grp_end;
   12295        15181 :       tree decl = OMP_CLAUSE_DECL (grp_end);
   12296              : 
   12297        15181 :       gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
   12298              : 
   12299        15181 :       if (DECL_P (grp_end))
   12300        14664 :         continue;
   12301              : 
   12302        15181 :       tree c = OMP_CLAUSE_DECL (*grp->grp_start);
   12303        16806 :       while (TREE_CODE (c) == ARRAY_REF)
   12304         1625 :         c = TREE_OPERAND (c, 0);
   12305        15181 :       if (TREE_CODE (c) != COMPONENT_REF)
   12306        14664 :         continue;
   12307          517 :       if (!seen_components)
   12308          474 :         seen_components = new hash_set<tree_operand_hash> ();
   12309          517 :       if (!shown_error)
   12310          474 :         shown_error = new hash_set<tree_operand_hash> ();
   12311          517 :       if (seen_components->contains (c)
   12312          517 :           && !shown_error->contains (c))
   12313              :         {
   12314           10 :           error_at (OMP_CLAUSE_LOCATION (grp_end),
   12315              :                     "%qE appears more than once in map clauses",
   12316            5 :                     OMP_CLAUSE_DECL (grp_end));
   12317            5 :           shown_error->add (c);
   12318              :         }
   12319              :       else
   12320          512 :         seen_components->add (c);
   12321              : 
   12322          517 :       omp_mapping_group *struct_group;
   12323          517 :       if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
   12324          517 :           && *grp->grp_start == grp_end)
   12325              :         {
   12326           78 :           omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
   12327              :                                            struct_group, grp);
   12328              :           /* Remove the whole of this mapping -- redundant.  */
   12329           78 :           grp->deleted = true;
   12330              :         }
   12331              :     }
   12332              : 
   12333         9462 :   if (seen_components)
   12334          474 :     delete seen_components;
   12335         9462 :   if (shown_error)
   12336          474 :     delete shown_error;
   12337         9462 : }
   12338              : 
   12339              : /* Link node NEWNODE so it is pointed to by chain INSERT_AT.  NEWNODE's chain
   12340              :    is linked to the previous node pointed to by INSERT_AT.  */
   12341              : 
   12342              : static tree *
   12343         1146 : omp_siblist_insert_node_after (tree newnode, tree *insert_at)
   12344              : {
   12345         1146 :   OMP_CLAUSE_CHAIN (newnode) = *insert_at;
   12346         1146 :   *insert_at = newnode;
   12347         1146 :   return &OMP_CLAUSE_CHAIN (newnode);
   12348              : }
   12349              : 
   12350              : /* Move NODE (which is currently pointed to by the chain OLD_POS) so it is
   12351              :    pointed to by chain MOVE_AFTER instead.  */
   12352              : 
   12353              : static void
   12354         1323 : omp_siblist_move_node_after (tree node, tree *old_pos, tree *move_after)
   12355              : {
   12356         1323 :   gcc_assert (node == *old_pos);
   12357         1323 :   *old_pos = OMP_CLAUSE_CHAIN (node);
   12358         1323 :   OMP_CLAUSE_CHAIN (node) = *move_after;
   12359         1323 :   *move_after = node;
   12360         1323 : }
   12361              : 
   12362              : /* Move nodes from FIRST_PTR (pointed to by previous node's chain) to
   12363              :    LAST_NODE to after MOVE_AFTER chain.  Similar to below function, but no
   12364              :    new nodes are prepended to the list before splicing into the new position.
   12365              :    Return the position we should continue scanning the list at, or NULL to
   12366              :    stay where we were.  */
   12367              : 
   12368              : static tree *
   12369          254 : omp_siblist_move_nodes_after (tree *first_ptr, tree last_node,
   12370              :                               tree *move_after)
   12371              : {
   12372          254 :   if (first_ptr == move_after)
   12373              :     return NULL;
   12374              : 
   12375          243 :   tree tmp = *first_ptr;
   12376          243 :   *first_ptr = OMP_CLAUSE_CHAIN (last_node);
   12377          243 :   OMP_CLAUSE_CHAIN (last_node) = *move_after;
   12378          243 :   *move_after = tmp;
   12379              : 
   12380          243 :   return first_ptr;
   12381              : }
   12382              : 
   12383              : /* Concatenate two lists described by [FIRST_NEW, LAST_NEW_TAIL] and
   12384              :    [FIRST_PTR, LAST_NODE], and insert them in the OMP clause list after chain
   12385              :    pointer MOVE_AFTER.
   12386              : 
   12387              :    The latter list was previously part of the OMP clause list, and the former
   12388              :    (prepended) part is comprised of new nodes.
   12389              : 
   12390              :    We start with a list of nodes starting with a struct mapping node.  We
   12391              :    rearrange the list so that new nodes starting from FIRST_NEW and whose last
   12392              :    node's chain is LAST_NEW_TAIL comes directly after MOVE_AFTER, followed by
   12393              :    the group of mapping nodes we are currently processing (from the chain
   12394              :    FIRST_PTR to LAST_NODE).  The return value is the pointer to the next chain
   12395              :    we should continue processing from, or NULL to stay where we were.
   12396              : 
   12397              :    The transformation (in the case where MOVE_AFTER and FIRST_PTR are
   12398              :    different) is worked through below.  Here we are processing LAST_NODE, and
   12399              :    FIRST_PTR points at the preceding mapping clause:
   12400              : 
   12401              :   #. mapping node               chain
   12402              :   ---------------------------------------------------
   12403              :   A. struct_node                [->B]
   12404              :   B. comp_1                     [->C]
   12405              :   C. comp_2                     [->D (move_after)]
   12406              :   D. map_to_3                   [->E]
   12407              :   E. attach_3                   [->F (first_ptr)]
   12408              :   F. map_to_4                   [->G (continue_at)]
   12409              :   G. attach_4 (last_node)       [->H]
   12410              :   H. ...
   12411              : 
   12412              :      *last_new_tail = *first_ptr;
   12413              : 
   12414              :   I. new_node (first_new)       [->F (last_new_tail)]
   12415              : 
   12416              :      *first_ptr = OMP_CLAUSE_CHAIN (last_node)
   12417              : 
   12418              :   #. mapping node               chain
   12419              :   ----------------------------------------------------
   12420              :   A. struct_node                [->B]
   12421              :   B. comp_1                     [->C]
   12422              :   C. comp_2                     [->D (move_after)]
   12423              :   D. map_to_3                   [->E]
   12424              :   E. attach_3                   [->H (first_ptr)]
   12425              :   F. map_to_4                   [->G (continue_at)]
   12426              :   G. attach_4 (last_node)       [->H]
   12427              :   H. ...
   12428              : 
   12429              :   I. new_node (first_new)       [->F  (last_new_tail)]
   12430              : 
   12431              :      OMP_CLAUSE_CHAIN (last_node) = *move_after;
   12432              : 
   12433              :   #. mapping node               chain
   12434              :   ---------------------------------------------------
   12435              :   A. struct_node                [->B]
   12436              :   B. comp_1                     [->C]
   12437              :   C. comp_2                     [->D (move_after)]
   12438              :   D. map_to_3                   [->E]
   12439              :   E. attach_3                   [->H (continue_at)]
   12440              :   F. map_to_4                   [->G]
   12441              :   G. attach_4 (last_node)       [->D]
   12442              :   H. ...
   12443              : 
   12444              :   I. new_node (first_new)       [->F  (last_new_tail)]
   12445              : 
   12446              :      *move_after = first_new;
   12447              : 
   12448              :   #. mapping node               chain
   12449              :   ---------------------------------------------------
   12450              :   A. struct_node                [->B]
   12451              :   B. comp_1                     [->C]
   12452              :   C. comp_2                     [->I (move_after)]
   12453              :   D. map_to_3                   [->E]
   12454              :   E. attach_3                   [->H (continue_at)]
   12455              :   F. map_to_4                   [->G]
   12456              :   G. attach_4 (last_node)       [->D]
   12457              :   H. ...
   12458              :   I. new_node (first_new)       [->F (last_new_tail)]
   12459              : 
   12460              :   or, in order:
   12461              : 
   12462              :   #. mapping node               chain
   12463              :   ---------------------------------------------------
   12464              :   A. struct_node                [->B]
   12465              :   B. comp_1                     [->C]
   12466              :   C. comp_2                     [->I (move_after)]
   12467              :   I. new_node (first_new)       [->F (last_new_tail)]
   12468              :   F. map_to_4                   [->G]
   12469              :   G. attach_4 (last_node)       [->D]
   12470              :   D. map_to_3                   [->E]
   12471              :   E. attach_3                   [->H (continue_at)]
   12472              :   H. ...
   12473              : */
   12474              : 
   12475              : static tree *
   12476           71 : omp_siblist_move_concat_nodes_after (tree first_new, tree *last_new_tail,
   12477              :                                      tree *first_ptr, tree last_node,
   12478              :                                      tree *move_after)
   12479              : {
   12480           71 :   tree *continue_at = NULL;
   12481           71 :   *last_new_tail = *first_ptr;
   12482           71 :   if (first_ptr == move_after)
   12483           12 :     *move_after = first_new;
   12484              :   else
   12485              :     {
   12486           59 :       *first_ptr = OMP_CLAUSE_CHAIN (last_node);
   12487           59 :       continue_at = first_ptr;
   12488           59 :       OMP_CLAUSE_CHAIN (last_node) = *move_after;
   12489           59 :       *move_after = first_new;
   12490              :     }
   12491           71 :   return continue_at;
   12492              : }
   12493              : 
   12494              : static omp_addr_token *
   12495        11614 : omp_first_chained_access_token (vec<omp_addr_token *> &addr_tokens)
   12496              : {
   12497        11614 :   using namespace omp_addr_tokenizer;
   12498        11614 :   int idx = addr_tokens.length () - 1;
   12499        11614 :   gcc_assert (idx >= 0);
   12500        11614 :   if (addr_tokens[idx]->type != ACCESS_METHOD)
   12501              :     return addr_tokens[idx];
   12502        11645 :   while (idx > 0 && addr_tokens[idx - 1]->type == ACCESS_METHOD)
   12503              :     idx--;
   12504        11614 :   return addr_tokens[idx];
   12505              : }
   12506              : 
   12507              : /* Mapping struct members causes an additional set of nodes to be created,
   12508              :    starting with GOMP_MAP_STRUCT followed by a number of mappings equal to the
   12509              :    number of members being mapped, in order of ascending position (address or
   12510              :    bitwise).
   12511              : 
   12512              :    We scan through the list of mapping clauses, calling this function for each
   12513              :    struct member mapping we find, and build up the list of mappings after the
   12514              :    initial GOMP_MAP_STRUCT node.  For pointer members, these will be
   12515              :    newly-created ALLOC nodes.  For non-pointer members, the existing mapping is
   12516              :    moved into place in the sorted list.
   12517              : 
   12518              :      struct {
   12519              :        int *a;
   12520              :        int *b;
   12521              :        int c;
   12522              :        int *d;
   12523              :      };
   12524              : 
   12525              :      #pragma (acc|omp directive) copy(struct.a[0:n], struct.b[0:n], struct.c,
   12526              :                                       struct.d[0:n])
   12527              : 
   12528              :      GOMP_MAP_STRUCT (4)
   12529              :      [GOMP_MAP_FIRSTPRIVATE_REFERENCE -- for refs to structs]
   12530              :      GOMP_MAP_ALLOC  (struct.a)
   12531              :      GOMP_MAP_ALLOC  (struct.b)
   12532              :      GOMP_MAP_TO     (struct.c)
   12533              :      GOMP_MAP_ALLOC  (struct.d)
   12534              :      ...
   12535              : 
   12536              :    In the case where we are mapping references to pointers, or in Fortran if
   12537              :    we are mapping an array with a descriptor, additional nodes may be created
   12538              :    after the struct node list also.
   12539              : 
   12540              :    The return code is either a pointer to the next node to process (if the
   12541              :    list has been rearranged), else NULL to continue with the next node in the
   12542              :    original list.  */
   12543              : 
   12544              : static tree *
   12545         4781 : omp_accumulate_sibling_list (enum omp_region_type region_type,
   12546              :                              enum tree_code code,
   12547              :                              hash_map<tree_operand_hash, tree>
   12548              :                                *&struct_map_to_clause,
   12549              :                              hash_map<tree_operand_hash_no_se,
   12550              :                                       omp_mapping_group *> *group_map,
   12551              :                              tree *grp_start_p, tree grp_end,
   12552              :                              vec<omp_addr_token *> &addr_tokens, tree **inner,
   12553              :                              bool *fragile_p, bool reprocessing_struct,
   12554              :                              tree **added_tail)
   12555              : {
   12556         4781 :   using namespace omp_addr_tokenizer;
   12557         4781 :   poly_offset_int coffset;
   12558         4781 :   poly_int64 cbitpos;
   12559         4781 :   tree ocd = OMP_CLAUSE_DECL (grp_end);
   12560         4781 :   bool openmp = !(region_type & ORT_ACC);
   12561         4781 :   bool target = (region_type & ORT_TARGET) != 0;
   12562         4781 :   tree *continue_at = NULL;
   12563              : 
   12564         5094 :   while (TREE_CODE (ocd) == ARRAY_REF)
   12565          313 :     ocd = TREE_OPERAND (ocd, 0);
   12566              : 
   12567         4781 :   if (*fragile_p)
   12568              :     {
   12569          156 :       omp_mapping_group *to_group
   12570          156 :         = omp_get_nonfirstprivate_group (group_map, ocd, true);
   12571              : 
   12572          156 :       if (to_group)
   12573              :         return NULL;
   12574              :     }
   12575              : 
   12576         4693 :   omp_addr_token *last_token = omp_first_chained_access_token (addr_tokens);
   12577         4693 :   if (last_token->type == ACCESS_METHOD)
   12578              :     {
   12579         4693 :       switch (last_token->u.access_kind)
   12580              :         {
   12581          735 :         case ACCESS_REF:
   12582          735 :         case ACCESS_REF_TO_POINTER:
   12583          735 :         case ACCESS_REF_TO_POINTER_OFFSET:
   12584          735 :         case ACCESS_INDEXED_REF_TO_ARRAY:
   12585              :           /* We may see either a bare reference or a dereferenced
   12586              :              "convert_from_reference"-like one here.  Handle either way.  */
   12587          735 :           if (TREE_CODE (ocd) == INDIRECT_REF)
   12588           64 :             ocd = TREE_OPERAND (ocd, 0);
   12589          735 :           gcc_assert (TREE_CODE (TREE_TYPE (ocd)) == REFERENCE_TYPE);
   12590              :           break;
   12591              : 
   12592              :         default:
   12593              :           ;
   12594              :         }
   12595              :     }
   12596              : 
   12597         4693 :   bool variable_offset;
   12598         9386 :   tree iterators = OMP_CLAUSE_HAS_ITERATORS (grp_end)
   12599         4726 :                      ? OMP_CLAUSE_ITERATORS (grp_end) : NULL_TREE;
   12600         4693 :   tree base
   12601         4693 :     = extract_base_bit_offset (ocd, &cbitpos, &coffset, &variable_offset,
   12602         4693 :                                iterators);
   12603              : 
   12604         4693 :   int base_token;
   12605        23977 :   for (base_token = addr_tokens.length () - 1; base_token >= 0; base_token--)
   12606              :     {
   12607        19284 :       if (addr_tokens[base_token]->type == ARRAY_BASE
   12608        19284 :           || addr_tokens[base_token]->type == STRUCTURE_BASE)
   12609              :         break;
   12610              :     }
   12611              : 
   12612              :   /* The two expressions in the assertion below aren't quite the same: if we
   12613              :      have 'struct_base_decl access_indexed_array' for something like
   12614              :      "myvar[2].x" then base will be "myvar" and addr_tokens[base_token]->expr
   12615              :      will be "myvar[2]" -- the actual base of the structure.
   12616              :      The former interpretation leads to a strange situation where we get
   12617              :        struct(myvar) alloc(myvar[2].ptr1)
   12618              :      That is, the array of structures is kind of treated as one big structure
   12619              :      for the purposes of gathering sibling lists, etc.  */
   12620              :   /* gcc_assert (base == addr_tokens[base_token]->expr);  */
   12621              : 
   12622         4693 :   bool attach_detach = ((OMP_CLAUSE_MAP_KIND (grp_end)
   12623              :                          == GOMP_MAP_ATTACH_DETACH)
   12624         4693 :                         || (OMP_CLAUSE_MAP_KIND (grp_end)
   12625         4693 :                             == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
   12626         4693 :   bool has_descriptor = false;
   12627         4693 :   if (OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
   12628              :     {
   12629         3153 :       tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
   12630         3153 :       if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
   12631              :         has_descriptor = true;
   12632              :     }
   12633              : 
   12634         4693 :   if (!struct_map_to_clause || struct_map_to_clause->get (base) == NULL)
   12635              :     {
   12636         3071 :       enum gomp_map_kind str_kind = GOMP_MAP_STRUCT;
   12637              : 
   12638         3071 :       if (struct_map_to_clause == NULL)
   12639         2507 :         struct_map_to_clause = new hash_map<tree_operand_hash, tree>;
   12640              : 
   12641         3071 :       if (variable_offset)
   12642          275 :         str_kind = GOMP_MAP_STRUCT_UNORD;
   12643              : 
   12644         3071 :       tree l = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
   12645              : 
   12646         3071 :       OMP_CLAUSE_SET_MAP_KIND (l, str_kind);
   12647         3071 :       OMP_CLAUSE_DECL (l) = unshare_expr (base);
   12648         3071 :       OMP_CLAUSE_SIZE (l) = size_int (1);
   12649              : 
   12650         3071 :       struct_map_to_clause->put (base, l);
   12651              : 
   12652              :       /* On first iterating through the clause list, we insert the struct node
   12653              :          just before the component access node that triggers the initial
   12654              :          omp_accumulate_sibling_list call for a particular sibling list (and
   12655              :          it then forms the first entry in that list).  When reprocessing
   12656              :          struct bases that are themselves component accesses, we insert the
   12657              :          struct node on an off-side list to avoid inserting the new
   12658              :          GOMP_MAP_STRUCT into the middle of the old one.  */
   12659         3071 :       tree *insert_node_pos = reprocessing_struct ? *added_tail : grp_start_p;
   12660              : 
   12661         3071 :       if (has_descriptor)
   12662              :         {
   12663          759 :           tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
   12664          759 :           if (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
   12665          187 :             OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
   12666          759 :           tree sc = *insert_node_pos;
   12667          759 :           OMP_CLAUSE_CHAIN (l) = desc;
   12668          759 :           OMP_CLAUSE_CHAIN (*grp_start_p) = OMP_CLAUSE_CHAIN (desc);
   12669          759 :           OMP_CLAUSE_CHAIN (desc) = sc;
   12670          759 :           *insert_node_pos = l;
   12671              :         }
   12672         2312 :       else if (attach_detach)
   12673              :         {
   12674         1396 :           tree extra_node;
   12675         1396 :           tree alloc_node
   12676         1396 :             = build_omp_struct_comp_nodes (code, *grp_start_p, grp_end,
   12677              :                                            &extra_node);
   12678         1396 :           tree *tail;
   12679         1396 :           OMP_CLAUSE_CHAIN (l) = alloc_node;
   12680              : 
   12681         1396 :           if (extra_node)
   12682              :             {
   12683            0 :               OMP_CLAUSE_CHAIN (extra_node) = *insert_node_pos;
   12684            0 :               OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
   12685            0 :               tail = &OMP_CLAUSE_CHAIN (extra_node);
   12686              :             }
   12687              :           else
   12688              :             {
   12689         1396 :               OMP_CLAUSE_CHAIN (alloc_node) = *insert_node_pos;
   12690         1396 :               tail = &OMP_CLAUSE_CHAIN (alloc_node);
   12691              :             }
   12692              : 
   12693              :           /* For OpenMP semantics, we don't want to implicitly allocate
   12694              :              space for the pointer here for non-compute regions (e.g. "enter
   12695              :              data").  A FRAGILE_P node is only being created so that
   12696              :              omp-low.cc is able to rewrite the struct properly.
   12697              :              For references (to pointers), we want to actually allocate the
   12698              :              space for the reference itself in the sorted list following the
   12699              :              struct node.
   12700              :              For pointers, we want to allocate space if we had an explicit
   12701              :              mapping of the attachment point, but not otherwise.  */
   12702         1396 :           if (*fragile_p
   12703         1396 :               || (openmp
   12704              :                   && !target
   12705              :                   && attach_detach
   12706          234 :                   && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
   12707           88 :                   && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
   12708              :             {
   12709          133 :               if (!lang_GNU_Fortran ())
   12710              :                /* In Fortran, pointers are dereferenced automatically, but may
   12711              :                   be unassociated.  So we still want to allocate space for the
   12712              :                   pointer (as the base for an attach operation that should be
   12713              :                   present in the same directive's clause list also).  */
   12714          103 :                 OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
   12715          133 :               OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
   12716              :             }
   12717              : 
   12718         1396 :           *insert_node_pos = l;
   12719              : 
   12720         1396 :           if (reprocessing_struct)
   12721              :             {
   12722              :               /* When reprocessing a struct node group used as the base of a
   12723              :                  subcomponent access, if we have a reference-to-pointer base,
   12724              :                  we will see:
   12725              :                    struct(**ptr) attach(*ptr)
   12726              :                  whereas for a non-reprocess-struct group, we see, e.g.:
   12727              :                    tofrom(**ptr) attach(*ptr) attach(ptr)
   12728              :                  and we create the "alloc" for the second "attach", i.e.
   12729              :                  for the reference itself.  When reprocessing a struct group we
   12730              :                  thus change the pointer attachment into a reference attachment
   12731              :                  by stripping the indirection.  (The attachment of the
   12732              :                  referenced pointer must happen elsewhere, either on the same
   12733              :                  directive, or otherwise.)  */
   12734          180 :               tree adecl = OMP_CLAUSE_DECL (alloc_node);
   12735              : 
   12736          180 :               if ((TREE_CODE (adecl) == INDIRECT_REF
   12737          148 :                    || (TREE_CODE (adecl) == MEM_REF
   12738            0 :                        && integer_zerop (TREE_OPERAND (adecl, 1))))
   12739           32 :                   && (TREE_CODE (TREE_TYPE (TREE_OPERAND (adecl, 0)))
   12740              :                       == REFERENCE_TYPE)
   12741          212 :                   && (TREE_CODE (TREE_TYPE (TREE_TYPE
   12742              :                         (TREE_OPERAND (adecl, 0)))) == POINTER_TYPE))
   12743           32 :                 OMP_CLAUSE_DECL (alloc_node) = TREE_OPERAND (adecl, 0);
   12744              : 
   12745          180 :               *added_tail = tail;
   12746              :             }
   12747              :         }
   12748              :       else
   12749              :         {
   12750          916 :           gcc_assert (*grp_start_p == grp_end);
   12751          916 :           if (reprocessing_struct)
   12752              :             {
   12753              :               /* If we don't have an attach/detach node, this is a
   12754              :                  "target data" directive or similar, not an offload region.
   12755              :                  Synthesize an "alloc" node using just the initiating
   12756              :                  GOMP_MAP_STRUCT decl.  */
   12757           16 :               gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
   12758           32 :                                  || code == OACC_EXIT_DATA)
   12759           32 :                                 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
   12760           32 :               tree alloc_node
   12761           32 :                 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
   12762              :                                     OMP_CLAUSE_MAP);
   12763           32 :               OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
   12764           32 :               OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
   12765           64 :               OMP_CLAUSE_SIZE (alloc_node)
   12766           32 :                 = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
   12767              : 
   12768           32 :               OMP_CLAUSE_CHAIN (alloc_node) = OMP_CLAUSE_CHAIN (l);
   12769           32 :               OMP_CLAUSE_CHAIN (l) = alloc_node;
   12770           32 :               *insert_node_pos = l;
   12771           32 :               *added_tail = &OMP_CLAUSE_CHAIN (alloc_node);
   12772              :             }
   12773              :           else
   12774          884 :             grp_start_p = omp_siblist_insert_node_after (l, insert_node_pos);
   12775              :         }
   12776              : 
   12777         3071 :       unsigned last_access = base_token + 1;
   12778              : 
   12779         3071 :       while (last_access + 1 < addr_tokens.length ()
   12780         3406 :              && addr_tokens[last_access + 1]->type == ACCESS_METHOD)
   12781              :         last_access++;
   12782              : 
   12783         3071 :       if ((region_type & ORT_TARGET)
   12784         3071 :           && addr_tokens[base_token + 1]->type == ACCESS_METHOD)
   12785              :         {
   12786         1719 :           bool base_ref = false;
   12787         1719 :           access_method_kinds access_kind
   12788         1719 :             = addr_tokens[last_access]->u.access_kind;
   12789              : 
   12790         1719 :           switch (access_kind)
   12791              :             {
   12792              :             case ACCESS_DIRECT:
   12793              :             case ACCESS_INDEXED_ARRAY:
   12794         1071 :               return NULL;
   12795              : 
   12796          403 :             case ACCESS_REF:
   12797          403 :             case ACCESS_REF_TO_POINTER:
   12798          403 :             case ACCESS_REF_TO_POINTER_OFFSET:
   12799          403 :             case ACCESS_INDEXED_REF_TO_ARRAY:
   12800          403 :               base_ref = true;
   12801          403 :               break;
   12802              : 
   12803          866 :             default:
   12804          866 :               ;
   12805              :             }
   12806          866 :           tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
   12807              :                                       OMP_CLAUSE_MAP);
   12808          866 :           enum gomp_map_kind mkind;
   12809          866 :           omp_mapping_group *decl_group;
   12810          866 :           tree use_base;
   12811          866 :           switch (access_kind)
   12812              :             {
   12813          463 :             case ACCESS_POINTER:
   12814          463 :             case ACCESS_POINTER_OFFSET:
   12815          463 :               use_base = addr_tokens[last_access]->expr;
   12816          463 :               break;
   12817          198 :             case ACCESS_REF_TO_POINTER:
   12818          198 :             case ACCESS_REF_TO_POINTER_OFFSET:
   12819          198 :               use_base
   12820          198 :                 = build_fold_indirect_ref (addr_tokens[last_access]->expr);
   12821          198 :               break;
   12822          205 :             default:
   12823          205 :               use_base = addr_tokens[base_token]->expr;
   12824              :             }
   12825          866 :           bool mapped_to_p
   12826          866 :             = omp_directive_maps_explicitly (group_map, use_base, &decl_group,
   12827              :                                              true, false, true);
   12828          866 :           if (addr_tokens[base_token]->type == STRUCTURE_BASE
   12829          866 :               && DECL_P (addr_tokens[last_access]->expr)
   12830         1372 :               && !mapped_to_p)
   12831          444 :             mkind = base_ref ? GOMP_MAP_FIRSTPRIVATE_REFERENCE
   12832              :                              : GOMP_MAP_FIRSTPRIVATE_POINTER;
   12833              :           else
   12834              :             mkind = GOMP_MAP_ATTACH_DETACH;
   12835              : 
   12836          866 :           OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
   12837              :           /* If we have a reference to pointer base, we want to attach the
   12838              :              pointer here, not the reference.  The reference attachment happens
   12839              :              elsewhere.  */
   12840          866 :           bool ref_to_ptr
   12841          866 :             = (access_kind == ACCESS_REF_TO_POINTER
   12842          866 :                || access_kind == ACCESS_REF_TO_POINTER_OFFSET);
   12843          866 :           tree sdecl = addr_tokens[last_access]->expr;
   12844          866 :           tree sdecl_ptr = ref_to_ptr ? build_fold_indirect_ref (sdecl)
   12845              :                                       : sdecl;
   12846              :           /* For the FIRSTPRIVATE_REFERENCE after the struct node, we
   12847              :              want to use the reference itself for the decl, but we
   12848              :              still want to use the pointer to calculate the bias.  */
   12849          866 :           OMP_CLAUSE_DECL (c2) = (mkind == GOMP_MAP_ATTACH_DETACH)
   12850          866 :                                  ? sdecl_ptr : sdecl;
   12851          866 :           sdecl = sdecl_ptr;
   12852          866 :           tree baddr = build_fold_addr_expr (base);
   12853          866 :           baddr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
   12854              :                                     ptrdiff_type_node, baddr);
   12855          866 :           tree decladdr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
   12856              :                                             ptrdiff_type_node, sdecl);
   12857          866 :           OMP_CLAUSE_SIZE (c2)
   12858          866 :             = fold_build2_loc (OMP_CLAUSE_LOCATION (grp_end), MINUS_EXPR,
   12859              :                                ptrdiff_type_node, baddr, decladdr);
   12860              :           /* Insert after struct node.  */
   12861          866 :           OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
   12862          866 :           OMP_CLAUSE_CHAIN (l) = c2;
   12863              : 
   12864          866 :           if (addr_tokens[base_token]->type == STRUCTURE_BASE
   12865          866 :               && (addr_tokens[base_token]->u.structure_base_kind
   12866              :                   == BASE_COMPONENT_EXPR)
   12867          286 :               && mkind == GOMP_MAP_ATTACH_DETACH
   12868         1152 :               && addr_tokens[last_access]->u.access_kind != ACCESS_REF)
   12869              :             {
   12870          218 :               *inner = insert_node_pos;
   12871          218 :               if (openmp)
   12872          166 :                 *fragile_p = true;
   12873              :               return NULL;
   12874              :             }
   12875              :         }
   12876              : 
   12877         2000 :       if (addr_tokens[base_token]->type == STRUCTURE_BASE
   12878         2000 :           && (addr_tokens[base_token]->u.structure_base_kind
   12879              :               == BASE_COMPONENT_EXPR)
   12880         2467 :           && addr_tokens[last_access]->u.access_kind == ACCESS_REF)
   12881          100 :         *inner = insert_node_pos;
   12882              : 
   12883              :       return NULL;
   12884              :     }
   12885         1622 :   else if (struct_map_to_clause)
   12886              :     {
   12887         1622 :       tree *osc = struct_map_to_clause->get (base);
   12888         1622 :       tree *sc = NULL, *scp = NULL;
   12889              : 
   12890         1622 :       unsigned HOST_WIDE_INT i, elems = tree_to_uhwi (OMP_CLAUSE_SIZE (*osc));
   12891         1622 :       sc = &OMP_CLAUSE_CHAIN (*osc);
   12892              :       /* The struct mapping might be immediately followed by a
   12893              :          FIRSTPRIVATE_POINTER,  FIRSTPRIVATE_REFERENCE or an ATTACH_DETACH --
   12894              :          if it's an indirect access or a reference, or if the structure base
   12895              :          is not a decl.  The FIRSTPRIVATE_* nodes are removed in omp-low.cc
   12896              :          after they have been processed there, and ATTACH_DETACH nodes are
   12897              :          recomputed and moved out of the GOMP_MAP_STRUCT construct once
   12898              :          sibling list building is complete.  */
   12899         1622 :       if (OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_POINTER
   12900         1577 :           || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
   12901         3154 :           || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_ATTACH_DETACH)
   12902          201 :         sc = &OMP_CLAUSE_CHAIN (*sc);
   12903         4111 :       for (i = 0; i < elems; i++, sc = &OMP_CLAUSE_CHAIN (*sc))
   12904         3065 :         if (attach_detach && sc == grp_start_p)
   12905              :           break;
   12906         3065 :         else if (TREE_CODE (OMP_CLAUSE_DECL (*sc)) != COMPONENT_REF
   12907          182 :                  && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != INDIRECT_REF
   12908         3247 :                  && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != ARRAY_REF)
   12909              :           break;
   12910              :         else
   12911              :           {
   12912         3065 :             tree sc_decl = OMP_CLAUSE_DECL (*sc);
   12913         3065 :             poly_offset_int offset;
   12914         3065 :             poly_int64 bitpos;
   12915              : 
   12916         3065 :             if (TREE_CODE (sc_decl) == ARRAY_REF)
   12917              :               {
   12918          366 :                 while (TREE_CODE (sc_decl) == ARRAY_REF)
   12919          184 :                   sc_decl = TREE_OPERAND (sc_decl, 0);
   12920          182 :                 if (TREE_CODE (sc_decl) != COMPONENT_REF
   12921          182 :                     || TREE_CODE (TREE_TYPE (sc_decl)) != ARRAY_TYPE)
   12922              :                   break;
   12923              :               }
   12924         2883 :             else if (INDIRECT_REF_P (sc_decl)
   12925            0 :                      && TREE_CODE (TREE_OPERAND (sc_decl, 0)) == COMPONENT_REF
   12926         2883 :                      && (TREE_CODE (TREE_TYPE (TREE_OPERAND (sc_decl, 0)))
   12927              :                          == REFERENCE_TYPE))
   12928            0 :               sc_decl = TREE_OPERAND (sc_decl, 0);
   12929              : 
   12930         3065 :             bool variable_offset2;
   12931         3065 :             tree iterators2 = OMP_CLAUSE_HAS_ITERATORS (*sc)
   12932         3065 :                                 ? OMP_CLAUSE_ITERATORS (*sc) : NULL_TREE;
   12933              : 
   12934         3065 :             tree base2 = extract_base_bit_offset (sc_decl, &bitpos, &offset,
   12935              :                                                   &variable_offset2,
   12936              :                                                   iterators2);
   12937         3065 :             if (!base2 || !operand_equal_p (base2, base, 0))
   12938              :               break;
   12939         3065 :             if (scp)
   12940          469 :               continue;
   12941         2845 :             if (variable_offset2)
   12942              :               {
   12943          341 :                 OMP_CLAUSE_SET_MAP_KIND (*osc, GOMP_MAP_STRUCT_UNORD);
   12944              : 
   12945          341 :                 if (has_descriptor)
   12946              :                   {
   12947              :                     /* Sort mapped components by offset. This is needed for
   12948              :                        libgomp to handle Fortran derived-type allocatable
   12949              :                        components transparently.  */
   12950              : 
   12951          323 :                     poly_int64 bitsize;
   12952          323 :                     tree offset, coffset;
   12953          323 :                     machine_mode mode;
   12954          323 :                     int unsignedp, reversep, volatilep;
   12955          323 :                     tree inner_ref1
   12956          323 :                       = get_inner_reference (sc_decl, &bitsize, &bitpos,
   12957              :                                              &offset, &mode, &unsignedp,
   12958              :                                              &reversep, &volatilep);
   12959          323 :                     tree osc_decl = ocd;
   12960          323 :                     STRIP_NOPS (osc_decl);
   12961          323 :                     tree inner_ref2
   12962          323 :                       = get_inner_reference (osc_decl, &bitsize, &bitpos,
   12963              :                                              &coffset, &mode, &unsignedp,
   12964              :                                              &reversep, &volatilep);
   12965          323 :                     gcc_assert (operand_equal_p (inner_ref1, inner_ref2, 0));
   12966          323 :                     tree offset_diff
   12967          323 :                       = fold_binary_to_constant (MINUS_EXPR, size_type_node,
   12968              :                                                  coffset, offset);
   12969          572 :                     if (offset_diff == NULL_TREE
   12970          323 :                         || TREE_INT_CST_ELT (offset_diff, 0) > 0)
   12971          249 :                       continue;
   12972              :                     else
   12973              :                       break;
   12974              :                   }
   12975              :               }
   12976         2504 :             else if ((region_type & ORT_ACC) != 0)
   12977              :               {
   12978              :                 /* For OpenACC, allow (ignore) duplicate struct accesses in
   12979              :                    the middle of a mapping clause, e.g. "mystruct->foo" in:
   12980              :                    copy(mystruct->foo->bar) copy(mystruct->foo->qux).  */
   12981          223 :                 if (reprocessing_struct
   12982            8 :                     && known_eq (coffset, offset)
   12983          223 :                     && known_eq (cbitpos, bitpos))
   12984           16 :                   return NULL;
   12985              :               }
   12986         2281 :             else if (known_eq (coffset, offset)
   12987         2281 :                      && known_eq (cbitpos, bitpos))
   12988              :               {
   12989              :                 /* Having two struct members at the same offset doesn't work,
   12990              :                    so make sure we don't.  (We're allowed to ignore this.
   12991              :                    Should we report the error?)  */
   12992              :                 /*error_at (OMP_CLAUSE_LOCATION (grp_end),
   12993              :                           "duplicate struct member %qE in map clauses",
   12994              :                           OMP_CLAUSE_DECL (grp_end));*/
   12995              :                 return NULL;
   12996              :               }
   12997         2506 :             if (maybe_lt (coffset, offset)
   12998         4234 :                 || (known_eq (coffset, offset)
   12999           20 :                     && maybe_lt (cbitpos, bitpos)))
   13000              :               {
   13001          778 :                 if (attach_detach)
   13002              :                   scp = sc;
   13003              :                 else
   13004              :                   break;
   13005              :               }
   13006              :           }
   13007              : 
   13008         1606 :       OMP_CLAUSE_SIZE (*osc)
   13009         1606 :         = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc), size_one_node);
   13010              : 
   13011         1606 :       if (reprocessing_struct)
   13012              :         {
   13013              :           /* If we're reprocessing a struct node, we don't want to do most of
   13014              :              the list manipulation below.  We only need to handle the (pointer
   13015              :              or reference) attach/detach case.  */
   13016            8 :           tree extra_node, alloc_node;
   13017            8 :           if (has_descriptor)
   13018            0 :             gcc_unreachable ();
   13019            8 :           else if (attach_detach)
   13020            8 :             alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
   13021              :                                                       grp_end, &extra_node);
   13022              :           else
   13023              :             {
   13024              :               /* If we don't have an attach/detach node, this is a
   13025              :                  "target data" directive or similar, not an offload region.
   13026              :                  Synthesize an "alloc" node using just the initiating
   13027              :                  GOMP_MAP_STRUCT decl.  */
   13028            0 :               gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
   13029            0 :                                  || code == OACC_EXIT_DATA)
   13030            0 :                                 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
   13031            0 :               alloc_node
   13032            0 :                 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
   13033              :                                     OMP_CLAUSE_MAP);
   13034            0 :               OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
   13035            0 :               OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
   13036            0 :               OMP_CLAUSE_SIZE (alloc_node)
   13037            0 :                 = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
   13038              :             }
   13039              : 
   13040            8 :           if (scp)
   13041            0 :             omp_siblist_insert_node_after (alloc_node, scp);
   13042              :           else
   13043              :             {
   13044            8 :               tree *new_end = omp_siblist_insert_node_after (alloc_node, sc);
   13045            8 :               if (sc == *added_tail)
   13046            8 :                 *added_tail = new_end;
   13047              :             }
   13048              : 
   13049            8 :           return NULL;
   13050              :         }
   13051              : 
   13052         1598 :       if (has_descriptor)
   13053              :         {
   13054          538 :           tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
   13055          538 :           if (code == OMP_TARGET_EXIT_DATA
   13056          538 :               || code == OACC_EXIT_DATA)
   13057          105 :             OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
   13058         1076 :           omp_siblist_move_node_after (desc,
   13059          538 :                                        &OMP_CLAUSE_CHAIN (*grp_start_p),
   13060              :                                        scp ? scp : sc);
   13061              :         }
   13062         1060 :       else if (attach_detach)
   13063              :         {
   13064          325 :           tree cl = NULL_TREE, extra_node;
   13065          325 :           tree alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
   13066              :                                                          grp_end, &extra_node);
   13067          325 :           tree *tail_chain = NULL;
   13068              : 
   13069          325 :           if (*fragile_p
   13070          325 :               || (openmp
   13071              :                   && !target
   13072              :                   && attach_detach
   13073           62 :                   && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
   13074           23 :                   && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
   13075              :             {
   13076            6 :               if (!lang_GNU_Fortran ())
   13077            6 :                 OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
   13078            6 :               OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
   13079              :             }
   13080              : 
   13081              :           /* Here, we have:
   13082              : 
   13083              :              grp_end : the last (or only) node in this group.
   13084              :              grp_start_p : pointer to the first node in a pointer mapping group
   13085              :                            up to and including GRP_END.
   13086              :              sc : pointer to the chain for the end of the struct component
   13087              :                   list.
   13088              :              scp : pointer to the chain for the sorted position at which we
   13089              :                    should insert in the middle of the struct component list
   13090              :                    (else NULL to insert at end).
   13091              :              alloc_node : the "alloc" node for the structure (pointer-type)
   13092              :                           component. We insert at SCP (if present), else SC
   13093              :                           (the end of the struct component list).
   13094              :              extra_node : a newly-synthesized node for an additional indirect
   13095              :                           pointer mapping or a Fortran pointer set, if needed.
   13096              :              cl : first node to prepend before grp_start_p.
   13097              :              tail_chain : pointer to chain of last prepended node.
   13098              : 
   13099              :              The general idea is we move the nodes for this struct mapping
   13100              :              together: the alloc node goes into the sorted list directly after
   13101              :              the struct mapping, and any extra nodes (together with the nodes
   13102              :              mapping arrays pointed to by struct components) get moved after
   13103              :              that list.  When SCP is NULL, we insert the nodes at SC, i.e. at
   13104              :              the end of the struct component mapping list.  It's important that
   13105              :              the alloc_node comes first in that case because it's part of the
   13106              :              sorted component mapping list (but subsequent nodes are not!).  */
   13107              : 
   13108          325 :           if (scp)
   13109          254 :             omp_siblist_insert_node_after (alloc_node, scp);
   13110              : 
   13111              :           /* Make [cl,tail_chain] a list of the alloc node (if we haven't
   13112              :              already inserted it) and the extra_node (if it is present).  The
   13113              :              list can be empty if we added alloc_node above and there is no
   13114              :              extra node.  */
   13115          254 :           if (scp && extra_node)
   13116              :             {
   13117            0 :               cl = extra_node;
   13118            0 :               tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
   13119              :             }
   13120          325 :           else if (extra_node)
   13121              :             {
   13122            0 :               OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
   13123            0 :               cl = alloc_node;
   13124            0 :               tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
   13125              :             }
   13126          325 :           else if (!scp)
   13127              :             {
   13128           71 :               cl = alloc_node;
   13129           71 :               tail_chain = &OMP_CLAUSE_CHAIN (alloc_node);
   13130              :             }
   13131              : 
   13132           71 :           continue_at
   13133           71 :             = cl ? omp_siblist_move_concat_nodes_after (cl, tail_chain,
   13134              :                                                         grp_start_p, grp_end,
   13135              :                                                         sc)
   13136          254 :                  : omp_siblist_move_nodes_after (grp_start_p, grp_end, sc);
   13137              :         }
   13138          735 :       else if (*sc != grp_end)
   13139              :         {
   13140          695 :           gcc_assert (*grp_start_p == grp_end);
   13141              : 
   13142              :           /* We are moving the current node back to a previous struct node:
   13143              :              the node that used to point to the current node will now point to
   13144              :              the next node.  */
   13145          695 :           continue_at = grp_start_p;
   13146              :           /* In the non-pointer case, the mapping clause itself is moved into
   13147              :              the correct position in the struct component list, which in this
   13148              :              case is just SC.  */
   13149          695 :           omp_siblist_move_node_after (*grp_start_p, grp_start_p, sc);
   13150              :         }
   13151              :     }
   13152              :   return continue_at;
   13153              : }
   13154              : 
   13155              : /* Scan through GROUPS, and create sorted structure sibling lists without
   13156              :    gimplifying.  */
   13157              : 
   13158              : static bool
   13159        18901 : omp_build_struct_sibling_lists (enum tree_code code,
   13160              :                                 enum omp_region_type region_type,
   13161              :                                 vec<omp_mapping_group> *groups,
   13162              :                                 hash_map<tree_operand_hash_no_se,
   13163              :                                          omp_mapping_group *> **grpmap,
   13164              :                                 tree *list_p)
   13165              : {
   13166        18901 :   using namespace omp_addr_tokenizer;
   13167        18901 :   unsigned i;
   13168        18901 :   omp_mapping_group *grp;
   13169        18901 :   hash_map<tree_operand_hash, tree> *struct_map_to_clause = NULL;
   13170        18901 :   bool success = true;
   13171        18901 :   tree *new_next = NULL;
   13172        37802 :   tree *tail = &OMP_CLAUSE_CHAIN ((*groups)[groups->length () - 1].grp_end);
   13173        18901 :   tree added_nodes = NULL_TREE;
   13174        18901 :   tree *added_tail = &added_nodes;
   13175        18901 :   auto_vec<omp_mapping_group> pre_hwm_groups;
   13176              : 
   13177        70309 :   FOR_EACH_VEC_ELT (*groups, i, grp)
   13178              :     {
   13179        32507 :       tree c = grp->grp_end;
   13180        32507 :       tree decl = OMP_CLAUSE_DECL (c);
   13181        32507 :       tree grp_end = grp->grp_end;
   13182        32507 :       auto_vec<omp_addr_token *> addr_tokens;
   13183        32507 :       tree sentinel = OMP_CLAUSE_CHAIN (grp_end);
   13184              : 
   13185        32507 :       if (new_next && !grp->reprocess_struct)
   13186          625 :         grp->grp_start = new_next;
   13187              : 
   13188        32507 :       new_next = NULL;
   13189              : 
   13190        32507 :       tree *grp_start_p = grp->grp_start;
   13191              : 
   13192        32507 :       if (DECL_P (decl))
   13193        22301 :         continue;
   13194              : 
   13195              :       /* Skip groups we marked for deletion in
   13196              :          {omp,oacc}_resolve_clause_dependencies.  */
   13197        10206 :       if (grp->deleted)
   13198          462 :         continue;
   13199              : 
   13200         9744 :       if (OMP_CLAUSE_CHAIN (*grp_start_p)
   13201         9744 :           && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
   13202              :         {
   13203              :           /* Don't process an array descriptor that isn't inside a derived type
   13204              :              as a struct (the GOMP_MAP_POINTER following will have the form
   13205              :              "var.data", but such mappings are handled specially).  */
   13206         6156 :           tree grpmid = OMP_CLAUSE_CHAIN (*grp_start_p);
   13207         6156 :           if (omp_map_clause_descriptor_p (grpmid)
   13208        10323 :               && DECL_P (OMP_CLAUSE_DECL (grpmid)))
   13209         2823 :             continue;
   13210              :         }
   13211              : 
   13212         7965 :       tree expr = decl;
   13213              : 
   13214         7965 :       while (TREE_CODE (expr) == ARRAY_REF)
   13215         1044 :         expr = TREE_OPERAND (expr, 0);
   13216              : 
   13217         6921 :       if (!omp_parse_expr (addr_tokens, expr))
   13218            0 :         continue;
   13219              : 
   13220         6921 :       omp_addr_token *last_token
   13221         6921 :         = omp_first_chained_access_token (addr_tokens);
   13222              : 
   13223              :       /* A mapping of a reference to a pointer member that doesn't specify an
   13224              :          array section, etc., like this:
   13225              :            *mystruct.ref_to_ptr
   13226              :          should not be processed by the struct sibling-list handling code --
   13227              :          it just transfers the referenced pointer.
   13228              : 
   13229              :          In contrast, the quite similar-looking construct:
   13230              :            *mystruct.ptr
   13231              :          which is equivalent to e.g.
   13232              :            mystruct.ptr[0]
   13233              :          *does* trigger sibling-list processing.
   13234              : 
   13235              :          An exception for the former case is for "fragile" groups where the
   13236              :          reference itself is not handled otherwise; this is subject to special
   13237              :          handling in omp_accumulate_sibling_list also.  */
   13238              : 
   13239         6921 :       if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
   13240         3402 :           && last_token->type == ACCESS_METHOD
   13241         3402 :           && last_token->u.access_kind == ACCESS_REF
   13242         7203 :           && !grp->fragile)
   13243          217 :         continue;
   13244              : 
   13245         6704 :       tree d = decl;
   13246         6704 :       if (TREE_CODE (d) == ARRAY_REF)
   13247              :         {
   13248         2036 :           while (TREE_CODE (d) == ARRAY_REF)
   13249         1029 :             d = TREE_OPERAND (d, 0);
   13250         1007 :           if (TREE_CODE (d) == COMPONENT_REF
   13251         1007 :               && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
   13252              :             decl = d;
   13253              :         }
   13254         6704 :       if (d == decl
   13255         6010 :           && INDIRECT_REF_P (decl)
   13256          738 :           && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
   13257          105 :           && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
   13258              :               == REFERENCE_TYPE)
   13259         6801 :           && (OMP_CLAUSE_MAP_KIND (c)
   13260              :               != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
   13261           97 :         decl = TREE_OPERAND (decl, 0);
   13262              : 
   13263         6704 :       STRIP_NOPS (decl);
   13264              : 
   13265         6704 :       if (TREE_CODE (decl) != COMPONENT_REF)
   13266         1368 :         continue;
   13267              : 
   13268              :       /* If we're mapping the whole struct in another node, skip adding this
   13269              :          node to a sibling list.  */
   13270         5336 :       omp_mapping_group *wholestruct;
   13271         5336 :       if (omp_mapped_by_containing_struct (*grpmap, OMP_CLAUSE_DECL (c),
   13272              :                                            &wholestruct))
   13273              :         {
   13274          192 :           if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
   13275          161 :             OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (c) = 0;
   13276          192 :           continue;
   13277              :         }
   13278              : 
   13279         5144 :       if (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_TO_PSET
   13280         5144 :           && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
   13281         5039 :           && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
   13282              :           && code != OACC_UPDATE
   13283        10116 :           && code != OMP_TARGET_UPDATE)
   13284              :         {
   13285         4781 :           if (error_operand_p (decl))
   13286              :             {
   13287            0 :               success = false;
   13288            0 :               goto error_out;
   13289              :             }
   13290              : 
   13291         4781 :           tree stype = TREE_TYPE (decl);
   13292         4781 :           if (TREE_CODE (stype) == REFERENCE_TYPE)
   13293          768 :             stype = TREE_TYPE (stype);
   13294         4781 :           if (TYPE_SIZE_UNIT (stype) == NULL
   13295         4781 :               || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
   13296              :             {
   13297            0 :               error_at (OMP_CLAUSE_LOCATION (c),
   13298              :                         "mapping field %qE of variable length "
   13299            0 :                         "structure", OMP_CLAUSE_DECL (c));
   13300            0 :               success = false;
   13301            0 :               goto error_out;
   13302              :             }
   13303              : 
   13304         4781 :           tree *inner = NULL;
   13305         4781 :           bool fragile_p = grp->fragile;
   13306              : 
   13307         4781 :           new_next
   13308         4781 :             = omp_accumulate_sibling_list (region_type, code,
   13309              :                                            struct_map_to_clause, *grpmap,
   13310              :                                            grp_start_p, grp_end, addr_tokens,
   13311              :                                            &inner, &fragile_p,
   13312              :                                            grp->reprocess_struct, &added_tail);
   13313              : 
   13314         4781 :           if (inner)
   13315              :             {
   13316          318 :               omp_mapping_group newgrp;
   13317          318 :               newgrp.grp_start = inner;
   13318          318 :               if (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (*inner))
   13319              :                   == GOMP_MAP_ATTACH_DETACH)
   13320          286 :                 newgrp.grp_end = OMP_CLAUSE_CHAIN (*inner);
   13321              :               else
   13322              :                 newgrp.grp_end = *inner;
   13323          318 :               newgrp.mark = UNVISITED;
   13324          318 :               newgrp.sibling = NULL;
   13325          318 :               newgrp.deleted = false;
   13326          318 :               newgrp.reprocess_struct = true;
   13327          318 :               newgrp.fragile = fragile_p;
   13328          318 :               newgrp.next = NULL;
   13329          318 :               groups->safe_push (newgrp);
   13330              : 
   13331              :               /* !!! Growing GROUPS might invalidate the pointers in the group
   13332              :                  map.  Rebuild it here.  This is a bit inefficient, but
   13333              :                  shouldn't happen very often.  */
   13334          636 :               delete (*grpmap);
   13335          318 :               *grpmap
   13336          318 :                 = omp_reindex_mapping_groups (list_p, groups, &pre_hwm_groups,
   13337              :                                               sentinel);
   13338              :             }
   13339              :         }
   13340        32507 :     }
   13341              : 
   13342              :   /* Delete groups marked for deletion above.  At this point the order of the
   13343              :      groups may no longer correspond to the order of the underlying list,
   13344              :      which complicates this a little.  First clear out OMP_CLAUSE_DECL for
   13345              :      deleted nodes...  */
   13346              : 
   13347        51408 :   FOR_EACH_VEC_ELT (*groups, i, grp)
   13348        32507 :     if (grp->deleted)
   13349          462 :       for (tree d = *grp->grp_start;
   13350          924 :            d != OMP_CLAUSE_CHAIN (grp->grp_end);
   13351          462 :            d = OMP_CLAUSE_CHAIN (d))
   13352          462 :         OMP_CLAUSE_DECL (d) = NULL_TREE;
   13353              : 
   13354              :   /* ...then sweep through the list removing the now-empty nodes.  */
   13355              : 
   13356              :   tail = list_p;
   13357       100384 :   while (*tail)
   13358              :     {
   13359        81483 :       if (OMP_CLAUSE_CODE (*tail) == OMP_CLAUSE_MAP
   13360        81483 :           && OMP_CLAUSE_DECL (*tail) == NULL_TREE)
   13361          462 :         *tail = OMP_CLAUSE_CHAIN (*tail);
   13362              :       else
   13363        81021 :         tail = &OMP_CLAUSE_CHAIN (*tail);
   13364              :     }
   13365              : 
   13366              :   /* Tack on the struct nodes added during nested struct reprocessing.  */
   13367        18901 :   if (added_nodes)
   13368              :     {
   13369          192 :       *tail = added_nodes;
   13370          192 :       tail = added_tail;
   13371              :     }
   13372              : 
   13373              :   /* Find each attach node whose bias needs to be adjusted and move it to the
   13374              :      group containing its pointee, right after the struct node, so that it can
   13375              :      be picked up by the adjustment code further down in this function.  */
   13376        18901 :   bool attach_bias_needs_adjustment;
   13377        18901 :   attach_bias_needs_adjustment = false;
   13378        70309 :   FOR_EACH_VEC_ELT_REVERSE (*groups, i, grp)
   13379              :     {
   13380        32507 :       tree c = *grp->grp_start;
   13381        31568 :       if (c != NULL && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   13382        31553 :           && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
   13383        28470 :               || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
   13384         3364 :           && OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c)) == GOMP_MAP_TO_PSET
   13385          560 :           && OMP_CLAUSE_MAP_KIND (grp->grp_end) == GOMP_MAP_ATTACH_DETACH
   13386        33067 :           && OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (grp->grp_end))
   13387              :         {
   13388           90 :           OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (grp->grp_end) = 0;
   13389           90 :           attach_bias_needs_adjustment = true;
   13390           90 :           tree *cp;
   13391           90 :           for (cp = &OMP_CLAUSE_CHAIN (c); cp != NULL;
   13392          206 :                cp = &OMP_CLAUSE_CHAIN (*cp))
   13393          296 :             if (*cp == grp->grp_end)
   13394              :               {
   13395              :                 c = *cp;
   13396              :                 break;
   13397              :               }
   13398              : 
   13399           90 :           tree base = OMP_CLAUSE_DECL (c);
   13400           90 :           gcc_assert (TREE_CODE (base) == NOP_EXPR);
   13401           90 :           base = build_fold_indirect_ref (base);
   13402           90 :           tree *struct_node = struct_map_to_clause->get (base);
   13403           90 :           omp_siblist_move_node_after (c, cp, &OMP_CLAUSE_CHAIN (*struct_node));
   13404              :         }
   13405              :     }
   13406              : 
   13407              :   /* Now we have finished building the struct sibling lists, reprocess
   13408              :      newly-added "attach" nodes: we need the address of the first
   13409              :      mapped element of each struct sibling list for the bias of the attach
   13410              :      operation -- not necessarily the base address of the whole struct.  */
   13411        18901 :   if (struct_map_to_clause)
   13412         3071 :     for (hash_map<tree_operand_hash, tree>::iterator iter
   13413         2507 :            = struct_map_to_clause->begin ();
   13414         8085 :          iter != struct_map_to_clause->end ();
   13415         3071 :          ++iter)
   13416              :       {
   13417         3071 :         tree struct_node = (*iter).second;
   13418         3071 :         gcc_assert (OMP_CLAUSE_CODE (struct_node) == OMP_CLAUSE_MAP);
   13419         3071 :         tree attach = OMP_CLAUSE_CHAIN (struct_node);
   13420              : 
   13421         3071 :         if (OMP_CLAUSE_CODE (attach) != OMP_CLAUSE_MAP
   13422         3071 :             || OMP_CLAUSE_MAP_KIND (attach) != GOMP_MAP_ATTACH_DETACH)
   13423         2559 :           continue;
   13424              : 
   13425          512 :         OMP_CLAUSE_SET_MAP_KIND (attach, GOMP_MAP_ATTACH);
   13426              : 
   13427              :         /* Sanity check: the standalone attach node will not work if we have
   13428              :            an "enter data" operation (because for those, variables need to be
   13429              :            mapped separately and attach nodes must be grouped together with the
   13430              :            base they attach to).  We should only have created the
   13431              :            ATTACH_DETACH node either after GOMP_MAP_STRUCT for a target region
   13432              :            or for an intermediate descriptor that needs adjustment -- so this
   13433              :            should never be true.  */
   13434          512 :         gcc_assert ((region_type & ORT_TARGET) != 0
   13435              :                     || attach_bias_needs_adjustment);
   13436              : 
   13437              :         /* This is the first sorted node in the struct sibling list.  Use it
   13438              :            to recalculate the correct bias to use.
   13439              :            (&first_node - attach_decl).
   13440              :            For GOMP_MAP_STRUCT_UNORD, we need e.g. the
   13441              :            min(min(min(first,second),third),fourth) element, because the
   13442              :            elements aren't in any particular order.  */
   13443          512 :         tree lowest_addr;
   13444          512 :         if (OMP_CLAUSE_MAP_KIND (struct_node) == GOMP_MAP_STRUCT_UNORD)
   13445              :           {
   13446           94 :             tree first_node = OMP_CLAUSE_CHAIN (attach);
   13447           94 :             unsigned HOST_WIDE_INT num_mappings
   13448           94 :               = tree_to_uhwi (OMP_CLAUSE_SIZE (struct_node));
   13449           94 :             lowest_addr = OMP_CLAUSE_DECL (first_node);
   13450           94 :             lowest_addr = build_fold_addr_expr (lowest_addr);
   13451           94 :             lowest_addr = fold_convert (pointer_sized_int_node, lowest_addr);
   13452           94 :             tree next_node = OMP_CLAUSE_CHAIN (first_node);
   13453          185 :             while (num_mappings > 1)
   13454              :               {
   13455           91 :                 tree tmp = OMP_CLAUSE_DECL (next_node);
   13456           91 :                 tmp = build_fold_addr_expr (tmp);
   13457           91 :                 tmp = fold_convert (pointer_sized_int_node, tmp);
   13458           91 :                 lowest_addr = fold_build2 (MIN_EXPR, pointer_sized_int_node,
   13459              :                                            lowest_addr, tmp);
   13460           91 :                 next_node = OMP_CLAUSE_CHAIN (next_node);
   13461           91 :                 num_mappings--;
   13462              :               }
   13463           94 :             lowest_addr = fold_convert (ptrdiff_type_node, lowest_addr);
   13464              :           }
   13465              :         else
   13466              :           {
   13467          418 :             tree first_node = OMP_CLAUSE_DECL (OMP_CLAUSE_CHAIN (attach));
   13468          418 :             first_node = build_fold_addr_expr (first_node);
   13469          418 :             lowest_addr = fold_convert (ptrdiff_type_node, first_node);
   13470              :           }
   13471          512 :         tree attach_decl = OMP_CLAUSE_DECL (attach);
   13472          512 :         attach_decl = fold_convert (ptrdiff_type_node, attach_decl);
   13473          512 :         OMP_CLAUSE_SIZE (attach)
   13474          512 :           = fold_build2 (MINUS_EXPR, ptrdiff_type_node, lowest_addr,
   13475              :                          attach_decl);
   13476              : 
   13477              :         /* Remove GOMP_MAP_ATTACH node from after struct node.  */
   13478          512 :         OMP_CLAUSE_CHAIN (struct_node) = OMP_CLAUSE_CHAIN (attach);
   13479              :         /* ...and re-insert it at the end of our clause list.  */
   13480          512 :         *tail = attach;
   13481          512 :         OMP_CLAUSE_CHAIN (attach) = NULL_TREE;
   13482          512 :         tail = &OMP_CLAUSE_CHAIN (attach);
   13483              :       }
   13484              : 
   13485        16394 : error_out:
   13486        18901 :   if (struct_map_to_clause)
   13487         2507 :     delete struct_map_to_clause;
   13488              : 
   13489        18901 :   return success;
   13490        18901 : }
   13491              : 
   13492              : struct instantiate_mapper_info
   13493              : {
   13494              :   tree *mapper_clauses_p;
   13495              :   struct gimplify_omp_ctx *omp_ctx;
   13496              :   gimple_seq *pre_p;
   13497              : };
   13498              : 
   13499              : /* Helper function for omp_instantiate_mapper.  */
   13500              : 
   13501              : static tree
   13502         1592 : remap_mapper_decl_1 (tree *tp, int *walk_subtrees, void *data)
   13503              : {
   13504         1592 :   copy_body_data *id = (copy_body_data *) data;
   13505              : 
   13506         1592 :   if (DECL_P (*tp))
   13507              :     {
   13508          724 :       tree replacement = remap_decl (*tp, id);
   13509          724 :       if (*tp != replacement)
   13510              :         {
   13511          369 :           *tp = unshare_expr (replacement);
   13512          369 :           *walk_subtrees = 0;
   13513              :         }
   13514              :     }
   13515              : 
   13516         1592 :   return NULL_TREE;
   13517              : }
   13518              : 
   13519              : /* A copy_decl implementation (for use with tree-inline.cc functions) that
   13520              :    only transform decls or SSA names that are part of a map we already
   13521              :    prepared.  */
   13522              : 
   13523              : static tree
   13524          126 : omp_mapper_copy_decl (tree var, copy_body_data *cb)
   13525              : {
   13526          126 :   tree *repl = cb->decl_map->get (var);
   13527              : 
   13528          126 :   if (repl)
   13529            0 :     return *repl;
   13530              : 
   13531          126 :   return var;
   13532              : }
   13533              : 
   13534              : static tree *
   13535           90 : omp_instantiate_mapper (gimple_seq *pre_p,
   13536              :                         hash_map<omp_name_type<tree>, tree> *implicit_mappers,
   13537              :                         tree mapperfn, tree expr, enum gomp_map_kind outer_kind,
   13538              :                         tree *mapper_clauses_p)
   13539              : {
   13540           90 :   tree mapper_name = NULL_TREE;
   13541           90 :   tree mapper = lang_hooks.decls.omp_extract_mapper_directive (mapperfn);
   13542           90 :   gcc_assert (TREE_CODE (mapper) == OMP_DECLARE_MAPPER);
   13543              : 
   13544           90 :   tree clause = OMP_DECLARE_MAPPER_CLAUSES (mapper);
   13545           90 :   tree dummy_var = OMP_DECLARE_MAPPER_DECL (mapper);
   13546              : 
   13547              :   /* The "extraction map" is used to map the mapper variable in the "declare
   13548              :      mapper" directive, and also any temporary variables that have been created
   13549              :      as part of expanding the mapper function's body (which are expanded as a
   13550              :      "bind" expression in the pre_p sequence).  */
   13551           90 :   hash_map<tree, tree> extraction_map;
   13552              : 
   13553           90 :   extraction_map.put (dummy_var, expr);
   13554           90 :   extraction_map.put (expr, expr);
   13555              : 
   13556              :   /* This copy_body_data is only used to remap the decls in the
   13557              :      OMP_DECLARE_MAPPER tree node expansion itself.  All relevant decls should
   13558              :      already be in the current function.  */
   13559           90 :   copy_body_data id;
   13560           90 :   memset (&id, 0, sizeof (id));
   13561           90 :   id.src_fn = current_function_decl;
   13562           90 :   id.dst_fn = current_function_decl;
   13563           90 :   id.src_cfun = cfun;
   13564           90 :   id.decl_map = &extraction_map;
   13565           90 :   id.copy_decl = omp_mapper_copy_decl;
   13566           90 :   id.transform_call_graph_edges = CB_CGE_DUPLICATE; // ???
   13567           90 :   id.transform_new_cfg = true; // ???
   13568              : 
   13569          282 :   for (; clause; clause = OMP_CLAUSE_CHAIN (clause))
   13570              :     {
   13571          192 :       enum gomp_map_kind map_kind = OMP_CLAUSE_MAP_KIND (clause);
   13572          192 :       tree *nested_mapper_p = NULL;
   13573              : 
   13574          192 :       if (map_kind == GOMP_MAP_PUSH_MAPPER_NAME)
   13575              :         {
   13576            0 :           mapper_name = OMP_CLAUSE_DECL (clause);
   13577           25 :           continue;
   13578              :         }
   13579          192 :       else if (map_kind == GOMP_MAP_POP_MAPPER_NAME)
   13580              :         {
   13581            0 :           mapper_name = NULL_TREE;
   13582            0 :           continue;
   13583              :         }
   13584              : 
   13585          192 :       if (OMP_CLAUSE_HAS_ITERATORS (clause))
   13586              :         {
   13587            2 :           sorry_at (OMP_CLAUSE_LOCATION (clause),
   13588              :                     "user-defined mapper that uses a %<map%> clause "
   13589              :                     "with %<iterator%>");
   13590            2 :           continue;
   13591              :         }
   13592              : 
   13593          190 :       tree decl = OMP_CLAUSE_DECL (clause);
   13594          190 :       tree unshared, type;
   13595          190 :       bool nonunit_array_with_mapper = false;
   13596              : 
   13597          190 :       if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
   13598              :         {
   13599           62 :           location_t loc = OMP_CLAUSE_LOCATION (clause);
   13600           62 :           tree tmp = lang_hooks.decls.omp_map_array_section (loc, decl);
   13601           62 :           if (tmp == decl)
   13602              :             {
   13603           48 :               unshared = unshare_expr (clause);
   13604           48 :               nonunit_array_with_mapper = true;
   13605           48 :               type = TREE_TYPE (TREE_TYPE (decl));
   13606              :             }
   13607              :           else
   13608              :             {
   13609           14 :               unshared = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
   13610           14 :                                            OMP_CLAUSE_CODE (clause));
   13611           14 :               OMP_CLAUSE_DECL (unshared) = tmp;
   13612           14 :               OMP_CLAUSE_SIZE (unshared)
   13613           28 :                 = DECL_P (tmp) ? DECL_SIZE_UNIT (tmp)
   13614           14 :                                : TYPE_SIZE_UNIT (TREE_TYPE (tmp));
   13615           14 :               type = TREE_TYPE (tmp);
   13616              :             }
   13617              :         }
   13618              :       else
   13619              :         {
   13620          128 :           unshared = unshare_expr (clause);
   13621          128 :           type = TREE_TYPE (decl);
   13622              :         }
   13623              : 
   13624          190 :       walk_tree (&unshared, remap_mapper_decl_1, &id, NULL);
   13625              : 
   13626          190 :       if (OMP_CLAUSE_MAP_KIND (unshared) == GOMP_MAP_UNSET)
   13627           28 :         OMP_CLAUSE_SET_MAP_KIND (unshared, outer_kind);
   13628              : 
   13629          190 :       decl = OMP_CLAUSE_DECL (unshared);
   13630          190 :       type = TYPE_MAIN_VARIANT (type);
   13631              : 
   13632          190 :       nested_mapper_p = implicit_mappers->get ({ mapper_name, type });
   13633              : 
   13634          190 :       if (nested_mapper_p && *nested_mapper_p != mapperfn)
   13635              :         {
   13636           23 :           if (nonunit_array_with_mapper)
   13637              :             {
   13638            8 :               sorry ("user-defined mapper with non-unit length array section");
   13639            8 :               continue;
   13640              :             }
   13641              : 
   13642           15 :           if (map_kind == GOMP_MAP_UNSET)
   13643            0 :             map_kind = outer_kind;
   13644              : 
   13645           15 :           mapper_clauses_p
   13646           15 :             = omp_instantiate_mapper (pre_p, implicit_mappers,
   13647              :                                       *nested_mapper_p, decl, map_kind,
   13648              :                                       mapper_clauses_p);
   13649           15 :           continue;
   13650              :         }
   13651              : 
   13652          167 :       *mapper_clauses_p = unshared;
   13653          167 :       mapper_clauses_p = &OMP_CLAUSE_CHAIN (unshared);
   13654              :     }
   13655              : 
   13656           90 :   return mapper_clauses_p;
   13657           90 : }
   13658              : 
   13659              : static int
   13660       144027 : omp_instantiate_implicit_mappers (splay_tree_node n, void *data)
   13661              : {
   13662       144027 :   tree decl = (tree) n->key;
   13663       144027 :   instantiate_mapper_info *im_info = (instantiate_mapper_info *) data;
   13664       144027 :   gimplify_omp_ctx *ctx = im_info->omp_ctx;
   13665       144027 :   tree *mapper_p = NULL;
   13666       144027 :   tree type = TREE_TYPE (decl);
   13667       144027 :   bool ref_p = false;
   13668       144027 :   unsigned flags = n->value;
   13669              : 
   13670       144027 :   if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
   13671              :     return 0;
   13672        24820 :   if ((flags & GOVD_SEEN) == 0)
   13673              :     return 0;
   13674              :   /* If we already have clauses pertaining to a struct variable, then we don't
   13675              :      want to implicitly invoke a user-defined mapper.  */
   13676        21248 :   if ((flags & GOVD_EXPLICIT) != 0 && AGGREGATE_TYPE_P (TREE_TYPE (decl)))
   13677              :     return 0;
   13678              : 
   13679        21248 :   if (TREE_CODE (type) == REFERENCE_TYPE)
   13680              :     {
   13681          910 :       ref_p = true;
   13682          910 :       type = TREE_TYPE (type);
   13683              :     }
   13684              : 
   13685        21248 :   type = TYPE_MAIN_VARIANT (type);
   13686              : 
   13687        21248 :   if (DECL_P (decl) && type && AGGREGATE_TYPE_P (type))
   13688              :     {
   13689         5524 :       gcc_assert (ctx);
   13690         5524 :       mapper_p = ctx->implicit_mappers->get ({ NULL_TREE, type });
   13691              :     }
   13692              : 
   13693         5524 :   if (mapper_p)
   13694              :     {
   13695              :       /* If we have a reference, map the pointed-to object rather than the
   13696              :          reference itself.  */
   13697           75 :       if (ref_p)
   13698            2 :         decl = build_fold_indirect_ref (decl);
   13699              : 
   13700           75 :       im_info->mapper_clauses_p
   13701           75 :         = omp_instantiate_mapper (im_info->pre_p, ctx->implicit_mappers,
   13702              :                                   *mapper_p, decl, GOMP_MAP_TOFROM,
   13703              :                                   im_info->mapper_clauses_p);
   13704              :       /* Make sure we don't map the same variable implicitly in
   13705              :          gimplify_adjust_omp_clauses_1 also.  */
   13706           75 :       n->value |= GOVD_EXPLICIT;
   13707              :     }
   13708              : 
   13709              :   return 0;
   13710              : }
   13711              : 
   13712              : /* Scan the OMP clauses in *LIST_P, installing mappings into a new
   13713              :    and previous omp contexts.  */
   13714              : 
   13715              : static void
   13716       136966 : gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
   13717              :                            enum omp_region_type region_type,
   13718              :                            enum tree_code code,
   13719              :                            gimple_seq *loops_seq_p = NULL)
   13720              : {
   13721       136966 :   using namespace omp_addr_tokenizer;
   13722       136966 :   struct gimplify_omp_ctx *ctx, *outer_ctx;
   13723       136966 :   tree c;
   13724       136966 :   tree *orig_list_p = list_p;
   13725       136966 :   int handled_depend_iterators = -1;
   13726       136966 :   int nowait = -1;
   13727              : 
   13728       136966 :   ctx = new_omp_context (region_type);
   13729       136966 :   ctx->code = code;
   13730       136966 :   outer_ctx = ctx->outer_context;
   13731       136966 :   if (code == OMP_TARGET)
   13732              :     {
   13733        14596 :       if (!lang_GNU_Fortran ())
   13734        12129 :         ctx->defaultmap[GDMK_POINTER] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
   13735        14596 :       ctx->defaultmap[GDMK_SCALAR] = GOVD_FIRSTPRIVATE;
   13736        29192 :       ctx->defaultmap[GDMK_SCALAR_TARGET] = (lang_GNU_Fortran ()
   13737        14596 :                                              ? GOVD_MAP : GOVD_FIRSTPRIVATE);
   13738              :     }
   13739       136966 :   if (!lang_GNU_Fortran ())
   13740       104941 :     switch (code)
   13741              :       {
   13742        19632 :       case OMP_TARGET:
   13743        19632 :       case OMP_TARGET_DATA:
   13744        19632 :       case OMP_TARGET_ENTER_DATA:
   13745        19632 :       case OMP_TARGET_EXIT_DATA:
   13746        19632 :       case OACC_DECLARE:
   13747        19632 :       case OACC_HOST_DATA:
   13748        19632 :       case OACC_PARALLEL:
   13749        19632 :       case OACC_KERNELS:
   13750        19632 :         ctx->target_firstprivatize_array_bases = true;
   13751              :       default:
   13752              :         break;
   13753              :       }
   13754              : 
   13755       136966 :   vec<omp_mapping_group> *groups = NULL;
   13756       136966 :   hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
   13757       136966 :   unsigned grpnum = 0;
   13758       136966 :   tree *grp_start_p = NULL, grp_end = NULL_TREE;
   13759              : 
   13760       136966 :   if (code == OMP_TARGET
   13761       136966 :       || code == OMP_TARGET_DATA
   13762       136966 :       || code == OMP_TARGET_ENTER_DATA
   13763              :       || code == OMP_TARGET_EXIT_DATA
   13764              :       || code == OACC_DATA
   13765              :       || code == OACC_KERNELS
   13766              :       || code == OACC_PARALLEL
   13767              :       || code == OACC_SERIAL
   13768              :       || code == OACC_ENTER_DATA
   13769              :       || code == OACC_EXIT_DATA
   13770              :       || code == OACC_UPDATE
   13771              :       || code == OACC_DECLARE)
   13772              :     {
   13773        35030 :       if (!(region_type & ORT_ACC))
   13774        18518 :         *list_p = omp_remove_duplicate_maps (*list_p, false);
   13775        35030 :       groups = omp_gather_mapping_groups (list_p);
   13776              : 
   13777        35030 :       if (groups)
   13778        19102 :         grpmap = omp_index_mapping_groups (groups);
   13779              :     }
   13780              : 
   13781       351884 :   while ((c = *list_p) != NULL)
   13782              :     {
   13783       214918 :       bool remove = false;
   13784       214918 :       bool notice_outer = true;
   13785       214918 :       bool map_descriptor;
   13786       214918 :       const char *check_non_private = NULL;
   13787       214918 :       unsigned int flags;
   13788       214918 :       tree decl;
   13789       214918 :       auto_vec<omp_addr_token *, 10> addr_tokens;
   13790       214918 :       tree op = NULL_TREE;
   13791       214918 :       location_t loc = OMP_CLAUSE_LOCATION (c);
   13792              : 
   13793       252958 :       if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
   13794              :         {
   13795              :           grp_start_p = NULL;
   13796              :           grp_end = NULL_TREE;
   13797              :         }
   13798              : 
   13799       214918 :       if (code == OMP_TARGET
   13800              :           || code == OMP_TARGET_DATA
   13801              :           || code == OMP_TARGET_ENTER_DATA
   13802       180134 :           || code == OMP_TARGET_EXIT_DATA)
   13803              :         /* Do some target-specific type checks for map operands.  */
   13804        37217 :         switch (OMP_CLAUSE_CODE (c))
   13805              :           {
   13806        27005 :           case OMP_CLAUSE_MAP:
   13807        27005 :             op = OMP_CLAUSE_OPERAND (c, 0);
   13808        27005 :             verify_type_context (loc, TCTX_OMP_MAP, TREE_TYPE (op));
   13809        27005 :             break;
   13810          186 :           case OMP_CLAUSE_PRIVATE:
   13811          186 :             op = OMP_CLAUSE_OPERAND (c, 0);
   13812          186 :             verify_type_context (loc, TCTX_OMP_PRIVATE, TREE_TYPE (op));
   13813          186 :             break;
   13814         1828 :           case OMP_CLAUSE_FIRSTPRIVATE:
   13815         1828 :             op = OMP_CLAUSE_OPERAND (c, 0);
   13816         1828 :             verify_type_context (loc, TCTX_OMP_FIRSTPRIVATE, TREE_TYPE (op));
   13817         1828 :             break;
   13818         2923 :           case OMP_CLAUSE_IS_DEVICE_PTR:
   13819         2923 :           case OMP_CLAUSE_USE_DEVICE_ADDR:
   13820         2923 :           case OMP_CLAUSE_USE_DEVICE_PTR:
   13821         2923 :           case OMP_CLAUSE_HAS_DEVICE_ADDR:
   13822         2923 :             op = OMP_CLAUSE_OPERAND (c, 0);
   13823         2923 :             verify_type_context (loc, TCTX_OMP_DEVICE_ADDR, TREE_TYPE (op));
   13824         2923 :             break;
   13825              :           default:
   13826              :             break;
   13827              :           }
   13828              : 
   13829       214918 :       switch (OMP_CLAUSE_CODE (c))
   13830              :         {
   13831        12383 :         case OMP_CLAUSE_PRIVATE:
   13832        12383 :           flags = GOVD_PRIVATE | GOVD_EXPLICIT;
   13833        12383 :           if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
   13834              :             {
   13835          167 :               flags |= GOVD_PRIVATE_OUTER_REF;
   13836          167 :               OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
   13837              :             }
   13838              :           else
   13839              :             notice_outer = false;
   13840        12383 :           goto do_add;
   13841         5584 :         case OMP_CLAUSE_SHARED:
   13842         5584 :           flags = GOVD_SHARED | GOVD_EXPLICIT;
   13843         5584 :           goto do_add;
   13844         8726 :         case OMP_CLAUSE_FIRSTPRIVATE:
   13845         8726 :           flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
   13846         8726 :           check_non_private = "firstprivate";
   13847         8726 :           if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
   13848              :             {
   13849          516 :               gcc_assert (code == OMP_TARGET);
   13850              :               flags |= GOVD_FIRSTPRIVATE_IMPLICIT;
   13851              :             }
   13852         8726 :           goto do_add;
   13853         7774 :         case OMP_CLAUSE_LASTPRIVATE:
   13854         7774 :           if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
   13855          540 :             switch (code)
   13856              :               {
   13857           25 :               case OMP_DISTRIBUTE:
   13858           25 :                 error_at (OMP_CLAUSE_LOCATION (c),
   13859              :                           "conditional %<lastprivate%> clause on "
   13860              :                           "%qs construct", "distribute");
   13861           25 :                 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
   13862           25 :                 break;
   13863           13 :               case OMP_TASKLOOP:
   13864           13 :                 error_at (OMP_CLAUSE_LOCATION (c),
   13865              :                           "conditional %<lastprivate%> clause on "
   13866              :                           "%qs construct", "taskloop");
   13867           13 :                 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
   13868           13 :                 break;
   13869              :               default:
   13870              :                 break;
   13871              :               }
   13872         7774 :           flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
   13873         7774 :           if (code != OMP_LOOP)
   13874         7423 :             check_non_private = "lastprivate";
   13875         7774 :           decl = OMP_CLAUSE_DECL (c);
   13876         7774 :           if (error_operand_p (decl))
   13877            0 :             goto do_add;
   13878         7774 :           if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
   13879         7774 :               && !lang_hooks.decls.omp_scalar_p (decl, true))
   13880              :             {
   13881           21 :               error_at (OMP_CLAUSE_LOCATION (c),
   13882              :                         "non-scalar variable %qD in conditional "
   13883              :                         "%<lastprivate%> clause", decl);
   13884           21 :               OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
   13885              :             }
   13886         7774 :           if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
   13887          481 :             flags |= GOVD_LASTPRIVATE_CONDITIONAL;
   13888         7774 :           omp_lastprivate_for_combined_outer_constructs (outer_ctx, decl,
   13889              :                                                          false);
   13890         7774 :           goto do_add;
   13891        16078 :         case OMP_CLAUSE_REDUCTION:
   13892        16078 :           if (OMP_CLAUSE_REDUCTION_TASK (c))
   13893              :             {
   13894          612 :               if (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
   13895              :                 {
   13896          417 :                   if (nowait == -1)
   13897          302 :                     nowait = omp_find_clause (*list_p,
   13898          302 :                                               OMP_CLAUSE_NOWAIT) != NULL_TREE;
   13899          417 :                   if (nowait
   13900           15 :                       && (outer_ctx == NULL
   13901            0 :                           || outer_ctx->region_type != ORT_COMBINED_PARALLEL))
   13902              :                     {
   13903           15 :                       error_at (OMP_CLAUSE_LOCATION (c),
   13904              :                                 "%<task%> reduction modifier on a construct "
   13905              :                                 "with a %<nowait%> clause");
   13906           15 :                       OMP_CLAUSE_REDUCTION_TASK (c) = 0;
   13907              :                     }
   13908              :                 }
   13909          195 :               else if ((region_type & ORT_PARALLEL) != ORT_PARALLEL)
   13910              :                 {
   13911           40 :                   error_at (OMP_CLAUSE_LOCATION (c),
   13912              :                             "invalid %<task%> reduction modifier on construct "
   13913              :                             "other than %<parallel%>, %qs, %<sections%> or "
   13914           20 :                             "%<scope%>", lang_GNU_Fortran () ? "do" : "for");
   13915           20 :                   OMP_CLAUSE_REDUCTION_TASK (c) = 0;
   13916              :                 }
   13917              :             }
   13918        16078 :           if (OMP_CLAUSE_REDUCTION_INSCAN (c))
   13919          855 :             switch (code)
   13920              :               {
   13921            4 :               case OMP_SECTIONS:
   13922            4 :                 error_at (OMP_CLAUSE_LOCATION (c),
   13923              :                           "%<inscan%> %<reduction%> clause on "
   13924              :                           "%qs construct", "sections");
   13925            4 :                 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
   13926            4 :                 break;
   13927            4 :               case OMP_PARALLEL:
   13928            4 :                 error_at (OMP_CLAUSE_LOCATION (c),
   13929              :                           "%<inscan%> %<reduction%> clause on "
   13930              :                           "%qs construct", "parallel");
   13931            4 :                 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
   13932            4 :                 break;
   13933            4 :               case OMP_TEAMS:
   13934            4 :                 error_at (OMP_CLAUSE_LOCATION (c),
   13935              :                           "%<inscan%> %<reduction%> clause on "
   13936              :                           "%qs construct", "teams");
   13937            4 :                 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
   13938            4 :                 break;
   13939            4 :               case OMP_TASKLOOP:
   13940            4 :                 error_at (OMP_CLAUSE_LOCATION (c),
   13941              :                           "%<inscan%> %<reduction%> clause on "
   13942              :                           "%qs construct", "taskloop");
   13943            4 :                 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
   13944            4 :                 break;
   13945            4 :               case OMP_SCOPE:
   13946            4 :                 error_at (OMP_CLAUSE_LOCATION (c),
   13947              :                           "%<inscan%> %<reduction%> clause on "
   13948              :                           "%qs construct", "scope");
   13949            4 :                 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
   13950            4 :                 break;
   13951              :               default:
   13952              :                 break;
   13953              :               }
   13954              :           /* FALLTHRU */
   13955        18707 :         case OMP_CLAUSE_IN_REDUCTION:
   13956        18707 :         case OMP_CLAUSE_TASK_REDUCTION:
   13957        18707 :           flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
   13958              :           /* OpenACC permits reductions on private variables.  */
   13959        18707 :           if (!(region_type & ORT_ACC)
   13960              :               /* taskgroup is actually not a worksharing region.  */
   13961        13188 :               && code != OMP_TASKGROUP)
   13962        12658 :             check_non_private = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
   13963        18707 :           decl = OMP_CLAUSE_DECL (c);
   13964        18707 :           if (TREE_CODE (decl) == MEM_REF)
   13965              :             {
   13966         2626 :               tree type = TREE_TYPE (decl);
   13967         2626 :               bool saved_into_ssa = gimplify_ctxp->into_ssa;
   13968         2626 :               gimplify_ctxp->into_ssa = false;
   13969         2626 :               if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
   13970              :                                  NULL, is_gimple_val, fb_rvalue, false)
   13971              :                   == GS_ERROR)
   13972              :                 {
   13973            0 :                   gimplify_ctxp->into_ssa = saved_into_ssa;
   13974            0 :                   remove = true;
   13975            0 :                   break;
   13976              :                 }
   13977         2626 :               gimplify_ctxp->into_ssa = saved_into_ssa;
   13978         2626 :               tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
   13979         2626 :               if (DECL_P (v))
   13980              :                 {
   13981          571 :                   omp_firstprivatize_variable (ctx, v);
   13982          571 :                   omp_notice_variable (ctx, v, true);
   13983              :                 }
   13984         2626 :               decl = TREE_OPERAND (decl, 0);
   13985         2626 :               if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
   13986              :                 {
   13987          477 :                   gimplify_ctxp->into_ssa = false;
   13988          477 :                   if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
   13989              :                                      NULL, is_gimple_val, fb_rvalue, false)
   13990              :                       == GS_ERROR)
   13991              :                     {
   13992            0 :                       gimplify_ctxp->into_ssa = saved_into_ssa;
   13993            0 :                       remove = true;
   13994            0 :                       break;
   13995              :                     }
   13996          477 :                   gimplify_ctxp->into_ssa = saved_into_ssa;
   13997          477 :                   v = TREE_OPERAND (decl, 1);
   13998          477 :                   if (DECL_P (v))
   13999              :                     {
   14000          477 :                       omp_firstprivatize_variable (ctx, v);
   14001          477 :                       omp_notice_variable (ctx, v, true);
   14002              :                     }
   14003          477 :                   decl = TREE_OPERAND (decl, 0);
   14004              :                 }
   14005         2626 :               if (TREE_CODE (decl) == ADDR_EXPR
   14006         1220 :                   || TREE_CODE (decl) == INDIRECT_REF)
   14007         1522 :                 decl = TREE_OPERAND (decl, 0);
   14008              :             }
   14009        18707 :           goto do_add_decl;
   14010         2755 :         case OMP_CLAUSE_LINEAR:
   14011         2755 :           if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
   14012              :                              is_gimple_val, fb_rvalue) == GS_ERROR)
   14013              :             {
   14014              :               remove = true;
   14015              :               break;
   14016              :             }
   14017              :           else
   14018              :             {
   14019         2755 :               if (code == OMP_SIMD
   14020         2755 :                   && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
   14021              :                 {
   14022         1160 :                   struct gimplify_omp_ctx *octx = outer_ctx;
   14023         1160 :                   if (octx
   14024          747 :                       && octx->region_type == ORT_WORKSHARE
   14025          500 :                       && octx->combined_loop
   14026          500 :                       && !octx->distribute)
   14027              :                     {
   14028          492 :                       if (octx->outer_context
   14029          430 :                           && (octx->outer_context->region_type
   14030              :                               == ORT_COMBINED_PARALLEL))
   14031          393 :                         octx = octx->outer_context->outer_context;
   14032              :                       else
   14033              :                         octx = octx->outer_context;
   14034              :                     }
   14035          905 :                   if (octx
   14036          322 :                       && octx->region_type == ORT_WORKSHARE
   14037           16 :                       && octx->combined_loop
   14038           16 :                       && octx->distribute)
   14039              :                     {
   14040           16 :                       error_at (OMP_CLAUSE_LOCATION (c),
   14041              :                                 "%<linear%> clause for variable other than "
   14042              :                                 "loop iterator specified on construct "
   14043              :                                 "combined with %<distribute%>");
   14044           16 :                       remove = true;
   14045           16 :                       break;
   14046              :                     }
   14047              :                 }
   14048              :               /* For combined #pragma omp parallel for simd, need to put
   14049              :                  lastprivate and perhaps firstprivate too on the
   14050              :                  parallel.  Similarly for #pragma omp for simd.  */
   14051              :               struct gimplify_omp_ctx *octx = outer_ctx;
   14052              :               bool taskloop_seen = false;
   14053              :               decl = NULL_TREE;
   14054         3555 :               do
   14055              :                 {
   14056         3555 :                   if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
   14057         3555 :                       && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
   14058              :                     break;
   14059         3493 :                   decl = OMP_CLAUSE_DECL (c);
   14060         3493 :                   if (error_operand_p (decl))
   14061              :                     {
   14062              :                       decl = NULL_TREE;
   14063              :                       break;
   14064              :                     }
   14065         3493 :                   flags = GOVD_SEEN;
   14066         3493 :                   if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
   14067         1678 :                     flags |= GOVD_FIRSTPRIVATE;
   14068         3493 :                   if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
   14069         3493 :                     flags |= GOVD_LASTPRIVATE;
   14070         3493 :                   if (octx
   14071         2569 :                       && octx->region_type == ORT_WORKSHARE
   14072         1246 :                       && octx->combined_loop)
   14073              :                     {
   14074         1240 :                       if (octx->outer_context
   14075         1068 :                           && (octx->outer_context->region_type
   14076              :                               == ORT_COMBINED_PARALLEL))
   14077              :                         octx = octx->outer_context;
   14078          488 :                       else if (omp_check_private (octx, decl, false))
   14079              :                         break;
   14080              :                     }
   14081              :                   else if (octx
   14082         1329 :                            && (octx->region_type & ORT_TASK) != 0
   14083          307 :                            && octx->combined_loop)
   14084              :                     taskloop_seen = true;
   14085              :                   else if (octx
   14086         1026 :                            && octx->region_type == ORT_COMBINED_PARALLEL
   14087          301 :                            && ((ctx->region_type == ORT_WORKSHARE
   14088          201 :                                 && octx == outer_ctx)
   14089          100 :                                || taskloop_seen))
   14090              :                     flags = GOVD_SEEN | GOVD_SHARED;
   14091              :                   else if (octx
   14092          725 :                            && ((octx->region_type & ORT_COMBINED_TEAMS)
   14093              :                                == ORT_COMBINED_TEAMS))
   14094              :                     flags = GOVD_SEEN | GOVD_SHARED;
   14095          560 :                   else if (octx
   14096          560 :                            && octx->region_type == ORT_COMBINED_TARGET)
   14097              :                     {
   14098          201 :                       if (flags & GOVD_LASTPRIVATE)
   14099          201 :                         flags = GOVD_SEEN | GOVD_MAP;
   14100              :                     }
   14101              :                   else
   14102              :                     break;
   14103         2118 :                   splay_tree_node on
   14104         2118 :                     = splay_tree_lookup (octx->variables,
   14105              :                                          (splay_tree_key) decl);
   14106         2118 :                   if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
   14107              :                     {
   14108              :                       octx = NULL;
   14109              :                       break;
   14110              :                     }
   14111         2114 :                   omp_add_variable (octx, decl, flags);
   14112         2114 :                   if (octx->outer_context == NULL)
   14113              :                     break;
   14114              :                   octx = octx->outer_context;
   14115              :                 }
   14116              :               while (1);
   14117         2739 :               if (octx
   14118         2739 :                   && decl
   14119         2739 :                   && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
   14120          760 :                       || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
   14121         1749 :                 omp_notice_variable (octx, decl, true);
   14122              :             }
   14123         2739 :           flags = GOVD_LINEAR | GOVD_EXPLICIT;
   14124         2739 :           if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
   14125         2739 :               && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
   14126              :             {
   14127              :               notice_outer = false;
   14128              :               flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
   14129              :             }
   14130         2739 :           goto do_add;
   14131              : 
   14132        52590 :         case OMP_CLAUSE_MAP:
   14133        52590 :           if (!grp_start_p)
   14134              :             {
   14135        32284 :               grp_start_p = list_p;
   14136        32284 :               grp_end = (*groups)[grpnum].grp_end;
   14137        32284 :               grpnum++;
   14138              :             }
   14139        52590 :           decl = OMP_CLAUSE_DECL (c);
   14140              : 
   14141        52590 :           if (error_operand_p (decl))
   14142              :             {
   14143              :               remove = true;
   14144              :               break;
   14145              :             }
   14146              : 
   14147        52590 :           if (!omp_parse_expr (addr_tokens, decl))
   14148              :             {
   14149              :               remove = true;
   14150              :               break;
   14151              :             }
   14152              : 
   14153        52590 :           if (remove)
   14154              :             break;
   14155        52590 :           if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
   14156              :             {
   14157              :               struct gimplify_omp_ctx *octx;
   14158         1167 :               for (octx = outer_ctx; octx; octx = octx->outer_context)
   14159              :                 {
   14160         1167 :                   if (octx->region_type != ORT_ACC_HOST_DATA)
   14161              :                     break;
   14162           12 :                   splay_tree_node n2
   14163           12 :                     = splay_tree_lookup (octx->variables,
   14164              :                                          (splay_tree_key) decl);
   14165           12 :                   if (n2)
   14166            4 :                     error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
   14167              :                               "declared in enclosing %<host_data%> region",
   14168            4 :                               DECL_NAME (decl));
   14169              :                 }
   14170              :             }
   14171              : 
   14172        52590 :           map_descriptor = false;
   14173              : 
   14174              :           /* This condition checks if we're mapping an array descriptor that
   14175              :              isn't inside a derived type -- these have special handling, and
   14176              :              are not handled as structs in omp_build_struct_sibling_lists.
   14177              :              See that function for further details.  */
   14178        52590 :           if (*grp_start_p != grp_end
   14179        34418 :               && OMP_CLAUSE_CHAIN (*grp_start_p)
   14180        87008 :               && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
   14181              :             {
   14182        17144 :               tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
   14183        17144 :               if (omp_map_clause_descriptor_p (grp_mid)
   14184        32461 :                   && DECL_P (OMP_CLAUSE_DECL (grp_mid)))
   14185              :                 map_descriptor = true;
   14186              :             }
   14187        35446 :           else if (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP
   14188        35446 :                    && (OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_RELEASE
   14189        34702 :                        || OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_DELETE)
   14190        36368 :                    && OMP_CLAUSE_RELEASE_DESCRIPTOR (grp_end))
   14191              :             map_descriptor = true;
   14192              : 
   14193              :           /* Adding the decl for a struct access: we haven't created
   14194              :              GOMP_MAP_STRUCT nodes yet, so this statement needs to predict
   14195              :              whether they will be created in gimplify_adjust_omp_clauses.
   14196              :              NOTE: Technically we should probably look through DECL_VALUE_EXPR
   14197              :              here because something that looks like a DECL_P may actually be a
   14198              :              struct access, e.g. variables in a lambda closure
   14199              :              (__closure->__foo) or class members (this->foo). Currently in both
   14200              :              those cases we map the whole of the containing object (directly in
   14201              :              the C++ FE) though, so struct nodes are not created.  */
   14202        52590 :           if (c == grp_end
   14203        32284 :               && addr_tokens[0]->type == STRUCTURE_BASE
   14204         8447 :               && addr_tokens[0]->u.structure_base_kind == BASE_DECL
   14205        61037 :               && !map_descriptor)
   14206              :             {
   14207         5627 :               gcc_assert (addr_tokens[1]->type == ACCESS_METHOD);
   14208              :               /* If we got to this struct via a chain of pointers, maybe we
   14209              :                  want to map it implicitly instead.  */
   14210         5627 :               if (omp_access_chain_p (addr_tokens, 1))
   14211              :                 break;
   14212         5466 :               omp_mapping_group *wholestruct;
   14213         5466 :               if (!(region_type & ORT_ACC)
   14214         9657 :                   && omp_mapped_by_containing_struct (grpmap,
   14215         4191 :                                                       OMP_CLAUSE_DECL (c),
   14216              :                                                       &wholestruct))
   14217              :                 break;
   14218         5215 :               decl = addr_tokens[1]->expr;
   14219         5215 :               if (splay_tree_lookup (ctx->variables, (splay_tree_key) decl))
   14220              :                 break;
   14221              :               /* Standalone attach or detach clauses for a struct element
   14222              :                  should not inhibit implicit mapping of the whole struct.  */
   14223         2998 :               if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
   14224         2998 :                   || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
   14225              :                 break;
   14226         2848 :               flags = GOVD_MAP | GOVD_EXPLICIT;
   14227              : 
   14228         2848 :               gcc_assert (addr_tokens[1]->u.access_kind != ACCESS_DIRECT
   14229              :                           || TREE_ADDRESSABLE (decl));
   14230         2848 :               goto do_add_decl;
   14231              :             }
   14232              : 
   14233        46963 :           if (!DECL_P (decl))
   14234              :             {
   14235        21235 :               tree d = decl, *pd;
   14236        21235 :               if (TREE_CODE (d) == ARRAY_REF)
   14237              :                 {
   14238         5136 :                   while (TREE_CODE (d) == ARRAY_REF)
   14239         2625 :                     d = TREE_OPERAND (d, 0);
   14240         2511 :                   if (TREE_CODE (d) == COMPONENT_REF
   14241         2511 :                       && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
   14242              :                     decl = d;
   14243              :                 }
   14244        21235 :               pd = &OMP_CLAUSE_DECL (c);
   14245        21235 :               if (d == decl
   14246        18770 :                   && TREE_CODE (decl) == INDIRECT_REF
   14247        14026 :                   && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
   14248         1084 :                   && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
   14249              :                       == REFERENCE_TYPE)
   14250        21900 :                   && (OMP_CLAUSE_MAP_KIND (c)
   14251              :                       != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
   14252              :                 {
   14253          661 :                   pd = &TREE_OPERAND (decl, 0);
   14254          661 :                   decl = TREE_OPERAND (decl, 0);
   14255              :                 }
   14256              : 
   14257        21235 :               if (addr_tokens[0]->type == STRUCTURE_BASE
   14258        11486 :                   && addr_tokens[0]->u.structure_base_kind == BASE_DECL
   14259        11485 :                   && addr_tokens[1]->type == ACCESS_METHOD
   14260        11485 :                   && (addr_tokens[1]->u.access_kind == ACCESS_POINTER
   14261        10987 :                       || (addr_tokens[1]->u.access_kind
   14262              :                           == ACCESS_POINTER_OFFSET))
   14263        21815 :                   && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)))
   14264              :                 {
   14265            0 :                   tree base = addr_tokens[1]->expr;
   14266            0 :                   splay_tree_node n
   14267            0 :                     = splay_tree_lookup (ctx->variables,
   14268              :                                          (splay_tree_key) base);
   14269            0 :                   n->value |= GOVD_SEEN;
   14270              :                 }
   14271              : 
   14272        21235 :               if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
   14273              :                 {
   14274              :                   /* Don't gimplify *pd fully at this point, as the base
   14275              :                      will need to be adjusted during omp lowering.  */
   14276           88 :                   auto_vec<tree, 10> expr_stack;
   14277           88 :                   tree *p = pd;
   14278           88 :                   while (handled_component_p (*p)
   14279              :                          || TREE_CODE (*p) == INDIRECT_REF
   14280              :                          || TREE_CODE (*p) == ADDR_EXPR
   14281              :                          || TREE_CODE (*p) == MEM_REF
   14282          224 :                          || TREE_CODE (*p) == NON_LVALUE_EXPR)
   14283              :                     {
   14284          136 :                       expr_stack.safe_push (*p);
   14285          136 :                       p = &TREE_OPERAND (*p, 0);
   14286              :                     }
   14287          312 :                   for (int i = expr_stack.length () - 1; i >= 0; i--)
   14288              :                     {
   14289          136 :                       tree t = expr_stack[i];
   14290          136 :                       if (TREE_CODE (t) == ARRAY_REF
   14291          136 :                           || TREE_CODE (t) == ARRAY_RANGE_REF)
   14292              :                         {
   14293           56 :                           if (TREE_OPERAND (t, 2) == NULL_TREE)
   14294              :                             {
   14295           56 :                               tree low = unshare_expr (array_ref_low_bound (t));
   14296           56 :                               if (!is_gimple_min_invariant (low))
   14297              :                                 {
   14298            0 :                                   TREE_OPERAND (t, 2) = low;
   14299            0 :                                   if (gimplify_expr (&TREE_OPERAND (t, 2),
   14300              :                                                      pre_p, NULL,
   14301              :                                                      is_gimple_reg,
   14302              :                                                      fb_rvalue) == GS_ERROR)
   14303           56 :                                     remove = true;
   14304              :                                 }
   14305              :                             }
   14306            0 :                           else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
   14307              :                                                   NULL, is_gimple_reg,
   14308              :                                                   fb_rvalue) == GS_ERROR)
   14309           56 :                             remove = true;
   14310           56 :                           if (TREE_OPERAND (t, 3) == NULL_TREE)
   14311              :                             {
   14312           56 :                               tree elmt_size = array_ref_element_size (t);
   14313           56 :                               if (!is_gimple_min_invariant (elmt_size))
   14314              :                                 {
   14315            0 :                                   elmt_size = unshare_expr (elmt_size);
   14316            0 :                                   tree elmt_type
   14317            0 :                                     = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t,
   14318              :                                                                           0)));
   14319            0 :                                   tree factor
   14320            0 :                                     = size_int (TYPE_ALIGN_UNIT (elmt_type));
   14321            0 :                                   elmt_size
   14322            0 :                                     = size_binop (EXACT_DIV_EXPR, elmt_size,
   14323              :                                                   factor);
   14324            0 :                                   TREE_OPERAND (t, 3) = elmt_size;
   14325            0 :                                   if (gimplify_expr (&TREE_OPERAND (t, 3),
   14326              :                                                      pre_p, NULL,
   14327              :                                                      is_gimple_reg,
   14328              :                                                      fb_rvalue) == GS_ERROR)
   14329            0 :                                     remove = true;
   14330              :                                 }
   14331              :                             }
   14332            0 :                           else if (gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
   14333              :                                                   NULL, is_gimple_reg,
   14334              :                                                   fb_rvalue) == GS_ERROR)
   14335            0 :                             remove = true;
   14336              :                         }
   14337           80 :                       else if (TREE_CODE (t) == COMPONENT_REF)
   14338              :                         {
   14339            0 :                           if (TREE_OPERAND (t, 2) == NULL_TREE)
   14340              :                             {
   14341            0 :                               tree offset = component_ref_field_offset (t);
   14342            0 :                               if (!is_gimple_min_invariant (offset))
   14343              :                                 {
   14344            0 :                                   offset = unshare_expr (offset);
   14345            0 :                                   tree field = TREE_OPERAND (t, 1);
   14346            0 :                                   tree factor
   14347            0 :                                     = size_int (DECL_OFFSET_ALIGN (field)
   14348              :                                                 / BITS_PER_UNIT);
   14349            0 :                                   offset = size_binop (EXACT_DIV_EXPR, offset,
   14350              :                                                        factor);
   14351            0 :                                   TREE_OPERAND (t, 2) = offset;
   14352            0 :                                   if (gimplify_expr (&TREE_OPERAND (t, 2),
   14353              :                                                      pre_p, NULL,
   14354              :                                                      is_gimple_reg,
   14355              :                                                      fb_rvalue) == GS_ERROR)
   14356            0 :                                     remove = true;
   14357              :                                 }
   14358              :                             }
   14359            0 :                           else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
   14360              :                                                   NULL, is_gimple_reg,
   14361              :                                                   fb_rvalue) == GS_ERROR)
   14362            0 :                             remove = true;
   14363              :                         }
   14364              :                     }
   14365          224 :                   for (; expr_stack.length () > 0; )
   14366              :                     {
   14367          136 :                       tree t = expr_stack.pop ();
   14368              : 
   14369          136 :                       if (TREE_CODE (t) == ARRAY_REF
   14370          136 :                           || TREE_CODE (t) == ARRAY_RANGE_REF)
   14371              :                         {
   14372           56 :                           if (!is_gimple_min_invariant (TREE_OPERAND (t, 1))
   14373           56 :                               && gimplify_expr (&TREE_OPERAND (t, 1), pre_p,
   14374              :                                                 NULL, is_gimple_val,
   14375              :                                                 fb_rvalue) == GS_ERROR)
   14376              :                             remove = true;
   14377              :                         }
   14378              :                     }
   14379           88 :                 }
   14380              :               break;
   14381              :             }
   14382              : 
   14383        25728 :           if ((code == OMP_TARGET
   14384              :                || code == OMP_TARGET_DATA
   14385              :                || code == OMP_TARGET_ENTER_DATA
   14386        14229 :                || code == OMP_TARGET_EXIT_DATA)
   14387        26370 :               && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
   14388              :             {
   14389              :               /* If we have attach/detach but the decl we have is a pointer to
   14390              :                  pointer, we're probably mapping the "base level" array
   14391              :                  implicitly.  Make sure we don't add the decl as if we mapped
   14392              :                  it explicitly.  That is,
   14393              : 
   14394              :                    int **arr;
   14395              :                    [...]
   14396              :                    #pragma omp target map(arr[a][b:c])
   14397              : 
   14398              :                  should *not* map "arr" explicitly.  That way we get a
   14399              :                  zero-length "alloc" mapping for it, and assuming it's been
   14400              :                  mapped by some previous directive, etc., things work as they
   14401              :                  should.  */
   14402              : 
   14403          217 :               tree basetype = TREE_TYPE (addr_tokens[0]->expr);
   14404              : 
   14405          217 :               if (TREE_CODE (basetype) == REFERENCE_TYPE)
   14406           34 :                 basetype = TREE_TYPE (basetype);
   14407              : 
   14408          217 :               if (code == OMP_TARGET
   14409           54 :                   && addr_tokens[0]->type == ARRAY_BASE
   14410           54 :                   && addr_tokens[0]->u.structure_base_kind == BASE_DECL
   14411           54 :                   && TREE_CODE (basetype) == POINTER_TYPE
   14412          271 :                   && TREE_CODE (TREE_TYPE (basetype)) == POINTER_TYPE)
   14413              :                 break;
   14414              :             }
   14415              : 
   14416        25706 :           flags = GOVD_MAP | GOVD_EXPLICIT;
   14417        25706 :           if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
   14418        25569 :               || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM
   14419        25060 :               || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TO
   14420        50736 :               || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TOFROM)
   14421              :             flags |= GOVD_MAP_ALWAYS_TO;
   14422              : 
   14423        25706 :           goto do_add;
   14424              : 
   14425          510 :         case OMP_CLAUSE_AFFINITY:
   14426          510 :           gimplify_omp_affinity (list_p, pre_p);
   14427          510 :           remove = true;
   14428          510 :           break;
   14429            8 :         case OMP_CLAUSE_DOACROSS:
   14430            8 :           if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
   14431              :             {
   14432            4 :               tree deps = OMP_CLAUSE_DECL (c);
   14433            8 :               while (deps && TREE_CODE (deps) == TREE_LIST)
   14434              :                 {
   14435            4 :                   if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
   14436            4 :                       && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
   14437            0 :                     gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
   14438              :                                    pre_p, NULL, is_gimple_val, fb_rvalue);
   14439            4 :                   deps = TREE_CHAIN (deps);
   14440              :                 }
   14441              :             }
   14442              :           else
   14443            4 :             gcc_assert (OMP_CLAUSE_DOACROSS_KIND (c)
   14444              :                         == OMP_CLAUSE_DOACROSS_SOURCE);
   14445              :           break;
   14446         2207 :         case OMP_CLAUSE_DEPEND:
   14447         2207 :           if (handled_depend_iterators == -1)
   14448         1905 :             handled_depend_iterators = gimplify_omp_depend (list_p, pre_p);
   14449         2207 :           if (handled_depend_iterators)
   14450              :             {
   14451          348 :               if (handled_depend_iterators == 2)
   14452            0 :                 remove = true;
   14453              :               break;
   14454              :             }
   14455         1859 :           if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
   14456              :             {
   14457            0 :               gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
   14458              :                              NULL, is_gimple_val, fb_rvalue);
   14459            0 :               OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
   14460              :             }
   14461         1859 :           if (error_operand_p (OMP_CLAUSE_DECL (c)))
   14462              :             {
   14463              :               remove = true;
   14464              :               break;
   14465              :             }
   14466         1859 :           if (OMP_CLAUSE_DECL (c) != null_pointer_node)
   14467              :             {
   14468         1828 :               OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
   14469         1828 :               if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
   14470              :                                  is_gimple_val, fb_rvalue) == GS_ERROR)
   14471              :                 {
   14472              :                   remove = true;
   14473              :                   break;
   14474              :                 }
   14475              :             }
   14476         1859 :           if (code == OMP_TASK)
   14477         1401 :             ctx->has_depend = true;
   14478              :           break;
   14479              : 
   14480         9909 :         case OMP_CLAUSE_TO:
   14481         9909 :         case OMP_CLAUSE_FROM:
   14482         9909 :         case OMP_CLAUSE__CACHE_:
   14483         9909 :           decl = OMP_CLAUSE_DECL (c);
   14484         9909 :           if (error_operand_p (decl))
   14485              :             {
   14486              :               remove = true;
   14487              :               break;
   14488              :             }
   14489         9909 :           if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
   14490         9208 :             OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
   14491          800 :                                   : TYPE_SIZE_UNIT (TREE_TYPE (decl));
   14492         9909 :           gimple_seq *seq_p;
   14493         9909 :           seq_p = enter_omp_iterator_loop_context (c, loops_seq_p, pre_p);
   14494         9909 :           if (gimplify_expr (&OMP_CLAUSE_SIZE (c), seq_p, NULL,
   14495              :                              is_gimple_val, fb_rvalue) == GS_ERROR)
   14496              :             {
   14497            0 :               remove = true;
   14498            0 :               exit_omp_iterator_loop_context (c);
   14499            0 :               break;
   14500              :             }
   14501         9909 :           if (!DECL_P (decl))
   14502              :             {
   14503         2180 :               if (gimplify_expr (&OMP_CLAUSE_DECL (c), seq_p, NULL,
   14504              :                                  is_gimple_lvalue, fb_lvalue) == GS_ERROR)
   14505            0 :                 remove = true;
   14506         2180 :               exit_omp_iterator_loop_context (c);
   14507         2180 :               break;
   14508              :             }
   14509         7729 :           exit_omp_iterator_loop_context (c);
   14510         7729 :           goto do_notice;
   14511              : 
   14512          135 :         case OMP_CLAUSE__MAPPER_BINDING_:
   14513          135 :           {
   14514          135 :             tree name = OMP_CLAUSE__MAPPER_BINDING__ID (c);
   14515          135 :             tree var = OMP_CLAUSE__MAPPER_BINDING__DECL (c);
   14516          135 :             tree type = TYPE_MAIN_VARIANT (TREE_TYPE (var));
   14517          135 :             tree fndecl = OMP_CLAUSE__MAPPER_BINDING__MAPPER (c);
   14518          135 :             ctx->implicit_mappers->put ({ name, type }, fndecl);
   14519          135 :             remove = true;
   14520          135 :             break;
   14521              :           }
   14522              : 
   14523         2118 :         case OMP_CLAUSE_USE_DEVICE_PTR:
   14524         2118 :         case OMP_CLAUSE_USE_DEVICE_ADDR:
   14525         2118 :           flags = GOVD_EXPLICIT;
   14526         2118 :           goto do_add;
   14527              : 
   14528          558 :         case OMP_CLAUSE_HAS_DEVICE_ADDR:
   14529          558 :           decl = OMP_CLAUSE_DECL (c);
   14530          558 :           while (TREE_CODE (decl) == INDIRECT_REF
   14531          607 :                  || TREE_CODE (decl) == ARRAY_REF)
   14532           49 :             decl = TREE_OPERAND (decl, 0);
   14533          558 :           flags = GOVD_EXPLICIT;
   14534          558 :           goto do_add_decl;
   14535              : 
   14536          500 :         case OMP_CLAUSE_IS_DEVICE_PTR:
   14537          500 :           flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
   14538          500 :           goto do_add;
   14539              : 
   14540        65720 :         do_add:
   14541        65720 :           decl = OMP_CLAUSE_DECL (c);
   14542        87833 :         do_add_decl:
   14543        87833 :           if (error_operand_p (decl))
   14544              :             {
   14545              :               remove = true;
   14546              :               break;
   14547              :             }
   14548        87825 :           if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
   14549              :             {
   14550         1897 :               tree t = omp_member_access_dummy_var (decl);
   14551         1897 :               if (t)
   14552              :                 {
   14553          668 :                   tree v = DECL_VALUE_EXPR (decl);
   14554          668 :                   DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
   14555          668 :                   if (outer_ctx)
   14556          140 :                     omp_notice_variable (outer_ctx, t, true);
   14557              :                 }
   14558              :             }
   14559        87825 :           if (code == OACC_DATA
   14560         2402 :               && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   14561        90227 :               && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
   14562          307 :             flags |= GOVD_MAP_0LEN_ARRAY;
   14563        87825 :           omp_add_variable (ctx, decl, flags);
   14564        87825 :           if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   14565        71747 :                || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
   14566        69648 :                || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
   14567        90454 :               && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
   14568              :             {
   14569         2150 :               struct gimplify_omp_ctx *pctx
   14570         2210 :                 = code == OMP_TARGET ? outer_ctx : ctx;
   14571         2210 :               if (pctx)
   14572         2180 :                 omp_add_variable (pctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
   14573              :                                   GOVD_LOCAL | GOVD_SEEN);
   14574         2180 :               if (pctx
   14575         2180 :                   && OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
   14576          632 :                   && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
   14577              :                                 find_decl_expr,
   14578              :                                 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
   14579              :                                 NULL) == NULL_TREE)
   14580          208 :                 omp_add_variable (pctx,
   14581          208 :                                   OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
   14582              :                                   GOVD_LOCAL | GOVD_SEEN);
   14583         2210 :               gimplify_omp_ctxp = pctx;
   14584         2210 :               push_gimplify_context ();
   14585              : 
   14586         2210 :               OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
   14587         2210 :               OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
   14588              : 
   14589         2210 :               gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
   14590         2210 :                                 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
   14591         2210 :               pop_gimplify_context
   14592         2210 :                 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
   14593         2210 :               push_gimplify_context ();
   14594         4420 :               gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
   14595         2210 :                                 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
   14596         2210 :               pop_gimplify_context
   14597         2210 :                 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
   14598         2210 :               OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
   14599         2210 :               OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
   14600              : 
   14601         2210 :               gimplify_omp_ctxp = outer_ctx;
   14602              :             }
   14603        85615 :           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
   14604        85615 :                    && OMP_CLAUSE_LASTPRIVATE_STMT (c))
   14605              :             {
   14606          405 :               gimplify_omp_ctxp = ctx;
   14607          405 :               push_gimplify_context ();
   14608          405 :               if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
   14609              :                 {
   14610          405 :                   tree bind = build3 (BIND_EXPR, void_type_node, NULL,
   14611              :                                       NULL, NULL);
   14612          405 :                   TREE_SIDE_EFFECTS (bind) = 1;
   14613          405 :                   BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
   14614          405 :                   OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
   14615              :                 }
   14616          810 :               gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
   14617          405 :                                 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
   14618          405 :               pop_gimplify_context
   14619          405 :                 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
   14620          405 :               OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
   14621              : 
   14622          405 :               gimplify_omp_ctxp = outer_ctx;
   14623              :             }
   14624        85210 :           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
   14625        85210 :                    && OMP_CLAUSE_LINEAR_STMT (c))
   14626              :             {
   14627           62 :               gimplify_omp_ctxp = ctx;
   14628           62 :               push_gimplify_context ();
   14629           62 :               if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
   14630              :                 {
   14631           62 :                   tree bind = build3 (BIND_EXPR, void_type_node, NULL,
   14632              :                                       NULL, NULL);
   14633           62 :                   TREE_SIDE_EFFECTS (bind) = 1;
   14634           62 :                   BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
   14635           62 :                   OMP_CLAUSE_LINEAR_STMT (c) = bind;
   14636              :                 }
   14637          124 :               gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
   14638           62 :                                 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
   14639           62 :               pop_gimplify_context
   14640           62 :                 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
   14641           62 :               OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
   14642              : 
   14643           62 :               gimplify_omp_ctxp = outer_ctx;
   14644              :             }
   14645        87825 :           if (notice_outer)
   14646        75547 :             goto do_notice;
   14647              :           break;
   14648              : 
   14649          913 :         case OMP_CLAUSE_COPYIN:
   14650          913 :         case OMP_CLAUSE_COPYPRIVATE:
   14651          913 :           decl = OMP_CLAUSE_DECL (c);
   14652          913 :           if (error_operand_p (decl))
   14653              :             {
   14654              :               remove = true;
   14655              :               break;
   14656              :             }
   14657          913 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
   14658              :               && !remove
   14659          913 :               && !omp_check_private (ctx, decl, true))
   14660              :             {
   14661           35 :               remove = true;
   14662           35 :               if (is_global_var (decl))
   14663              :                 {
   14664           30 :                   if (DECL_THREAD_LOCAL_P (decl))
   14665              :                     remove = false;
   14666           10 :                   else if (DECL_HAS_VALUE_EXPR_P (decl))
   14667              :                     {
   14668            2 :                       tree value = get_base_address (DECL_VALUE_EXPR (decl));
   14669              : 
   14670            2 :                       if (value
   14671            2 :                           && DECL_P (value)
   14672            4 :                           && DECL_THREAD_LOCAL_P (value))
   14673              :                         remove = false;
   14674              :                     }
   14675              :                 }
   14676              :               if (remove)
   14677           13 :                 error_at (OMP_CLAUSE_LOCATION (c),
   14678              :                           "copyprivate variable %qE is not threadprivate"
   14679           13 :                           " or private in outer context", DECL_NAME (decl));
   14680              :             }
   14681        84189 :         do_notice:
   14682        84189 :           if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   14683        68111 :                || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
   14684        59385 :                || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
   14685        32578 :               && outer_ctx
   14686        18936 :               && ((region_type & ORT_TASKLOOP) == ORT_TASKLOOP
   14687        17597 :                    || (region_type == ORT_WORKSHARE
   14688         3987 :                        && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   14689         1115 :                        && (OMP_CLAUSE_REDUCTION_INSCAN (c)
   14690          930 :                            || code == OMP_LOOP)))
   14691        86144 :               && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
   14692          857 :                   || (code == OMP_LOOP
   14693          138 :                       && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   14694          138 :                       && ((outer_ctx->region_type & ORT_COMBINED_TEAMS)
   14695              :                           == ORT_COMBINED_TEAMS))))
   14696              :             {
   14697         1219 :               splay_tree_node on
   14698         1219 :                 = splay_tree_lookup (outer_ctx->variables,
   14699              :                                      (splay_tree_key)decl);
   14700         1219 :               if (on == NULL || (on->value & GOVD_DATA_SHARE_CLASS) == 0)
   14701              :                 {
   14702          901 :                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   14703          715 :                       && TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
   14704         1013 :                       && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
   14705           56 :                           || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
   14706            0 :                               && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
   14707              :                                   == POINTER_TYPE))))
   14708           56 :                     omp_firstprivatize_variable (outer_ctx, decl);
   14709              :                   else
   14710              :                     {
   14711          845 :                       omp_add_variable (outer_ctx, decl,
   14712              :                                         GOVD_SEEN | GOVD_SHARED);
   14713          845 :                       if (outer_ctx->outer_context)
   14714          237 :                         omp_notice_variable (outer_ctx->outer_context, decl,
   14715              :                                              true);
   14716              :                     }
   14717              :                 }
   14718              :             }
   14719        83581 :           if (outer_ctx)
   14720        31039 :             omp_notice_variable (outer_ctx, decl, true);
   14721        84189 :           if (check_non_private
   14722        28807 :               && (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
   14723         4882 :               && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
   14724         1693 :                   || decl == OMP_CLAUSE_DECL (c)
   14725          224 :                   || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
   14726          224 :                       && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
   14727              :                           == ADDR_EXPR
   14728          103 :                           || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
   14729              :                               == POINTER_PLUS_EXPR
   14730           22 :                               && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
   14731              :                                                 (OMP_CLAUSE_DECL (c), 0), 0))
   14732              :                                   == ADDR_EXPR)))))
   14733        88983 :               && omp_check_private (ctx, decl, false))
   14734              :             {
   14735           58 :               error ("%s variable %qE is private in outer context",
   14736           29 :                      check_non_private, DECL_NAME (decl));
   14737           29 :               remove = true;
   14738              :             }
   14739              :           break;
   14740              : 
   14741          190 :         case OMP_CLAUSE_DETACH:
   14742          190 :           flags = GOVD_FIRSTPRIVATE | GOVD_SEEN;
   14743          190 :           goto do_add;
   14744              : 
   14745         4124 :         case OMP_CLAUSE_IF:
   14746         4124 :           if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
   14747         4124 :               && OMP_CLAUSE_IF_MODIFIER (c) != code)
   14748              :             {
   14749              :               const char *p[2];
   14750          168 :               for (int i = 0; i < 2; i++)
   14751          112 :                 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
   14752              :                   {
   14753            8 :                   case VOID_CST: p[i] = "cancel"; break;
   14754           24 :                   case OMP_PARALLEL: p[i] = "parallel"; break;
   14755            4 :                   case OMP_SIMD: p[i] = "simd"; break;
   14756           12 :                   case OMP_TASK: p[i] = "task"; break;
   14757           12 :                   case OMP_TASKLOOP: p[i] = "taskloop"; break;
   14758            8 :                   case OMP_TARGET_DATA: p[i] = "target data"; break;
   14759           12 :                   case OMP_TARGET: p[i] = "target"; break;
   14760           12 :                   case OMP_TARGET_UPDATE: p[i] = "target update"; break;
   14761            8 :                   case OMP_TARGET_ENTER_DATA:
   14762            8 :                     p[i] = "target enter data"; break;
   14763           12 :                   case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
   14764            0 :                   default: gcc_unreachable ();
   14765              :                   }
   14766           56 :               error_at (OMP_CLAUSE_LOCATION (c),
   14767              :                         "expected %qs %<if%> clause modifier rather than %qs",
   14768              :                         p[0], p[1]);
   14769           56 :               remove = true;
   14770              :             }
   14771              :           /* Fall through.  */
   14772              : 
   14773         4834 :         case OMP_CLAUSE_SELF:
   14774         4834 :         case OMP_CLAUSE_FINAL:
   14775         4834 :           OMP_CLAUSE_OPERAND (c, 0)
   14776         9668 :             = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
   14777              :           /* Fall through.  */
   14778              : 
   14779         5932 :         case OMP_CLAUSE_NUM_TEAMS:
   14780         5932 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
   14781         1098 :               && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
   14782         6208 :               && !is_gimple_min_invariant (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
   14783              :             {
   14784          217 :               if (error_operand_p (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
   14785              :                 {
   14786              :                   remove = true;
   14787              :                   break;
   14788              :                 }
   14789          217 :               OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
   14790          434 :                 = get_initialized_tmp_var (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c),
   14791              :                                            pre_p, NULL, true);
   14792              :             }
   14793              :           /* Fall through.  */
   14794              : 
   14795        22279 :         case OMP_CLAUSE_SCHEDULE:
   14796        22279 :         case OMP_CLAUSE_NUM_THREADS:
   14797        22279 :         case OMP_CLAUSE_THREAD_LIMIT:
   14798        22279 :         case OMP_CLAUSE_DIST_SCHEDULE:
   14799        22279 :         case OMP_CLAUSE_DEVICE:
   14800        22279 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
   14801        22279 :               && OMP_CLAUSE_DEVICE_ANCESTOR (c))
   14802              :             {
   14803          125 :               if (code != OMP_TARGET)
   14804              :                 {
   14805           20 :                   error_at (OMP_CLAUSE_LOCATION (c),
   14806              :                             "%<device%> clause with %<ancestor%> is only "
   14807              :                             "allowed on %<target%> construct");
   14808           20 :                   remove = true;
   14809           20 :                   break;
   14810              :                 }
   14811              : 
   14812          105 :               tree clauses = *orig_list_p;
   14813          330 :               for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
   14814          236 :                 if (OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEVICE
   14815              :                     && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_FIRSTPRIVATE
   14816              :                     && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_PRIVATE
   14817              :                     && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEFAULTMAP
   14818              :                     && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_MAP
   14819              :                    )
   14820              :                   {
   14821           11 :                     error_at (OMP_CLAUSE_LOCATION (c),
   14822              :                               "with %<ancestor%>, only the %<device%>, "
   14823              :                               "%<firstprivate%>, %<private%>, %<defaultmap%>, "
   14824              :                               "and %<map%> clauses may appear on the "
   14825              :                               "construct");
   14826           11 :                     remove = true;
   14827           11 :                     break;
   14828              :                   }
   14829              :             }
   14830        22154 :           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
   14831        22154 :                    && code == OMP_DISPATCH)
   14832              :             {
   14833          272 :               bool saved_into_ssa = gimplify_ctxp->into_ssa;
   14834          272 :               gimplify_ctxp->into_ssa = false;
   14835          272 :               if (gimplify_expr (&OMP_CLAUSE_DEVICE_ID (c), pre_p, NULL,
   14836              :                                  is_gimple_val, fb_rvalue)
   14837              :                   == GS_ERROR)
   14838              :                 remove = true;
   14839          272 :               else if (DECL_P (OMP_CLAUSE_DEVICE_ID (c)))
   14840           55 :                 omp_add_variable (ctx, OMP_CLAUSE_DEVICE_ID (c),
   14841              :                                   GOVD_SHARED | GOVD_SEEN);
   14842          272 :               gimplify_ctxp->into_ssa = saved_into_ssa;
   14843          272 :               break;
   14844              :             }
   14845              :           /* Fall through.  */
   14846              : 
   14847        31733 :         case OMP_CLAUSE_PRIORITY:
   14848        31733 :         case OMP_CLAUSE_GRAINSIZE:
   14849        31733 :         case OMP_CLAUSE_NUM_TASKS:
   14850        31733 :         case OMP_CLAUSE_FILTER:
   14851        31733 :         case OMP_CLAUSE_HINT:
   14852        31733 :         case OMP_CLAUSE_ASYNC:
   14853        31733 :         case OMP_CLAUSE_WAIT:
   14854        31733 :         case OMP_CLAUSE_NUM_GANGS:
   14855        31733 :         case OMP_CLAUSE_NUM_WORKERS:
   14856        31733 :         case OMP_CLAUSE_VECTOR_LENGTH:
   14857        31733 :         case OMP_CLAUSE_WORKER:
   14858        31733 :         case OMP_CLAUSE_VECTOR:
   14859        31733 :           if (OMP_CLAUSE_OPERAND (c, 0)
   14860        31733 :               && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) == TREE_LIST)
   14861              :             {
   14862         2541 :               for (tree t = OMP_CLAUSE_OPERAND (c, 0); t; t = TREE_CHAIN (t))
   14863         1314 :                 if (!is_gimple_min_invariant (TREE_VALUE (t)))
   14864              :                   {
   14865          207 :                     if (error_operand_p (TREE_VALUE (t)))
   14866              :                       {
   14867              :                         remove = true;
   14868              :                         break;
   14869              :                       }
   14870          414 :                     TREE_VALUE (t)
   14871          207 :                       = get_initialized_tmp_var (TREE_VALUE (t), pre_p,
   14872              :                                                  NULL, true);
   14873              :                   }
   14874              :             }
   14875        30506 :           else if (OMP_CLAUSE_OPERAND (c, 0)
   14876        30506 :                    && !is_gimple_min_invariant (OMP_CLAUSE_OPERAND (c, 0)))
   14877              :             {
   14878         7678 :               if (error_operand_p (OMP_CLAUSE_OPERAND (c, 0)))
   14879              :                 {
   14880              :                   remove = true;
   14881              :                   break;
   14882              :                 }
   14883              :               /* All these clauses care about value, not a particular decl,
   14884              :                  so try to force it into a SSA_NAME or fresh temporary.  */
   14885         7671 :               OMP_CLAUSE_OPERAND (c, 0)
   14886        15342 :                 = get_initialized_tmp_var (OMP_CLAUSE_OPERAND (c, 0),
   14887              :                                            pre_p, NULL, true);
   14888              :             }
   14889              :           break;
   14890              : 
   14891         2332 :         case OMP_CLAUSE_GANG:
   14892         2332 :           if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
   14893              :                              is_gimple_val, fb_rvalue) == GS_ERROR)
   14894            0 :             remove = true;
   14895         2332 :           if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
   14896              :                              is_gimple_val, fb_rvalue) == GS_ERROR)
   14897            0 :             remove = true;
   14898              :           break;
   14899              : 
   14900        13854 :         case OMP_CLAUSE_NOWAIT:
   14901        13854 :           nowait = 1;
   14902        13854 :           break;
   14903              : 
   14904          116 :         case OMP_CLAUSE_USES_ALLOCATORS:
   14905          116 :           if (TREE_CODE (OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c))
   14906              :               != INTEGER_CST)
   14907              :             {
   14908          112 :               decl = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
   14909          112 :               omp_add_variable (ctx, decl,
   14910              :                                 GOVD_SEEN | GOVD_USES_ALLOCATORS_ALLOCATOR);
   14911              :             }
   14912              :           else
   14913              :             remove = true;
   14914              :           break;
   14915              : 
   14916              :         case OMP_CLAUSE_ORDERED:
   14917              :         case OMP_CLAUSE_UNTIED:
   14918              :         case OMP_CLAUSE_COLLAPSE:
   14919              :         case OMP_CLAUSE_TILE:
   14920              :         case OMP_CLAUSE_AUTO:
   14921              :         case OMP_CLAUSE_SEQ:
   14922              :         case OMP_CLAUSE_INDEPENDENT:
   14923              :         case OMP_CLAUSE_MERGEABLE:
   14924              :         case OMP_CLAUSE_MESSAGE:
   14925              :         case OMP_CLAUSE_PROC_BIND:
   14926              :         case OMP_CLAUSE_SAFELEN:
   14927              :         case OMP_CLAUSE_NOGROUP:
   14928              :         case OMP_CLAUSE_THREADS:
   14929              :         case OMP_CLAUSE_SIMD:
   14930              :         case OMP_CLAUSE_BIND:
   14931              :         case OMP_CLAUSE_IF_PRESENT:
   14932              :         case OMP_CLAUSE_FINALIZE:
   14933              :         case OMP_CLAUSE_INTEROP:
   14934              :         case OMP_CLAUSE_INIT:
   14935              :         case OMP_CLAUSE_USE:
   14936              :         case OMP_CLAUSE_DESTROY:
   14937              :         case OMP_CLAUSE_DEVICE_TYPE:
   14938              :           break;
   14939              : 
   14940          684 :         case OMP_CLAUSE_SIMDLEN:
   14941          684 :           if (OMP_CLAUSE_SIMDLEN_TYPE (c))
   14942              :             {
   14943           30 :               remove = true;
   14944           30 :               sorry_at (OMP_CLAUSE_LOCATION (c),
   14945              :                         "%<simdlen%> clause with %<scaled%> modifier");
   14946              :             }
   14947              :           break;
   14948              : 
   14949           85 :         case OMP_CLAUSE_DYN_GROUPPRIVATE:
   14950           85 :           remove = true;
   14951           85 :           sorry_at (OMP_CLAUSE_LOCATION (c),"%<dyn_groupprivate%> clause");
   14952           85 :           break;
   14953              : 
   14954         4004 :         case OMP_CLAUSE_ORDER:
   14955         4004 :           ctx->order_concurrent = true;
   14956         4004 :           break;
   14957              : 
   14958         1050 :         case OMP_CLAUSE_DEFAULTMAP:
   14959         1050 :           enum gimplify_defaultmap_kind gdmkmin, gdmkmax;
   14960         1050 :           switch (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c))
   14961              :             {
   14962              :             case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
   14963              :             case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
   14964              :               gdmkmin = GDMK_SCALAR;
   14965              :               gdmkmax = GDMK_POINTER;
   14966              :               break;
   14967              :             case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
   14968              :               gdmkmin = GDMK_SCALAR;
   14969              :               gdmkmax = GDMK_SCALAR_TARGET;
   14970              :               break;
   14971              :             case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
   14972              :               gdmkmin = gdmkmax = GDMK_AGGREGATE;
   14973              :               break;
   14974              :             case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE:
   14975              :               gdmkmin = gdmkmax = GDMK_ALLOCATABLE;
   14976              :               break;
   14977              :             case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
   14978              :               gdmkmin = gdmkmax = GDMK_POINTER;
   14979              :               break;
   14980            0 :             default:
   14981            0 :               gcc_unreachable ();
   14982              :             }
   14983         4615 :           for (int gdmk = gdmkmin; gdmk <= gdmkmax; gdmk++)
   14984         3565 :             switch (OMP_CLAUSE_DEFAULTMAP_BEHAVIOR (c))
   14985              :               {
   14986           94 :               case OMP_CLAUSE_DEFAULTMAP_ALLOC:
   14987           94 :                 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_ALLOC_ONLY;
   14988           94 :                 break;
   14989           96 :               case OMP_CLAUSE_DEFAULTMAP_TO:
   14990           96 :                 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_TO_ONLY;
   14991           96 :                 break;
   14992           28 :               case OMP_CLAUSE_DEFAULTMAP_FROM:
   14993           28 :                 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FROM_ONLY;
   14994           28 :                 break;
   14995          699 :               case OMP_CLAUSE_DEFAULTMAP_TOFROM:
   14996          699 :                 ctx->defaultmap[gdmk] = GOVD_MAP;
   14997          699 :                 break;
   14998          397 :               case OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE:
   14999          397 :                 ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
   15000          397 :                 break;
   15001         2147 :               case OMP_CLAUSE_DEFAULTMAP_NONE:
   15002         2147 :                 ctx->defaultmap[gdmk] = 0;
   15003         2147 :                 break;
   15004           48 :               case OMP_CLAUSE_DEFAULTMAP_PRESENT:
   15005           48 :                 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
   15006           48 :                 break;
   15007           56 :               case OMP_CLAUSE_DEFAULTMAP_DEFAULT:
   15008           56 :                 switch (gdmk)
   15009              :                   {
   15010           11 :                   case GDMK_SCALAR:
   15011           11 :                     ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
   15012           11 :                     break;
   15013           11 :                   case GDMK_SCALAR_TARGET:
   15014           11 :                     ctx->defaultmap[gdmk] = (lang_GNU_Fortran ()
   15015           11 :                                              ? GOVD_MAP : GOVD_FIRSTPRIVATE);
   15016           11 :                     break;
   15017           14 :                   case GDMK_AGGREGATE:
   15018           14 :                   case GDMK_ALLOCATABLE:
   15019           14 :                     ctx->defaultmap[gdmk] = GOVD_MAP;
   15020           14 :                     break;
   15021           20 :                   case GDMK_POINTER:
   15022           20 :                     ctx->defaultmap[gdmk] = GOVD_MAP;
   15023           20 :                     if (!lang_GNU_Fortran ())
   15024           12 :                       ctx->defaultmap[gdmk] |= GOVD_MAP_0LEN_ARRAY;
   15025              :                     break;
   15026              :                   default:
   15027              :                     gcc_unreachable ();
   15028              :                   }
   15029              :                 break;
   15030            0 :               default:
   15031            0 :                 gcc_unreachable ();
   15032              :               }
   15033              :           break;
   15034              : 
   15035          824 :         case OMP_CLAUSE_ALIGNED:
   15036          824 :           decl = OMP_CLAUSE_DECL (c);
   15037          824 :           if (error_operand_p (decl))
   15038              :             {
   15039              :               remove = true;
   15040              :               break;
   15041              :             }
   15042          824 :           if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
   15043              :                              is_gimple_val, fb_rvalue) == GS_ERROR)
   15044              :             {
   15045              :               remove = true;
   15046              :               break;
   15047              :             }
   15048          824 :           if (!is_global_var (decl)
   15049          824 :               && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
   15050          592 :             omp_add_variable (ctx, decl, GOVD_ALIGNED);
   15051              :           break;
   15052              : 
   15053          489 :         case OMP_CLAUSE_NONTEMPORAL:
   15054          489 :           decl = OMP_CLAUSE_DECL (c);
   15055          489 :           if (error_operand_p (decl))
   15056              :             {
   15057              :               remove = true;
   15058              :               break;
   15059              :             }
   15060          489 :           omp_add_variable (ctx, decl, GOVD_NONTEMPORAL);
   15061          489 :           break;
   15062              : 
   15063         3691 :         case OMP_CLAUSE_ALLOCATE:
   15064         3691 :           decl = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
   15065         3691 :           if (decl
   15066         1722 :               && TREE_CODE (decl) == INTEGER_CST
   15067         3720 :               && wi::eq_p (wi::to_widest (decl), GOMP_OMP_PREDEF_ALLOC_THREAD)
   15068         3720 :               && (code == OMP_TARGET || code == OMP_TASK || code == OMP_TASKLOOP))
   15069           35 :             warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
   15070              :                         "allocator with access trait set to %<thread%> "
   15071              :                         "results in undefined behavior for %qs directive",
   15072              :                         code == OMP_TARGET ? "target"
   15073              :                                            : (code == OMP_TASK
   15074           15 :                                               ? "task" : "taskloop"));
   15075         3691 :           decl = OMP_CLAUSE_DECL (c);
   15076         3691 :           if (error_operand_p (decl))
   15077              :             {
   15078              :               remove = true;
   15079              :               break;
   15080              :             }
   15081         3691 :           if ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS) == 0
   15082         3679 :               && OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
   15083         5413 :               && TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)) != INTEGER_CST)
   15084              :             {
   15085          521 :               tree allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
   15086          521 :               tree clauses = NULL_TREE;
   15087              : 
   15088              :               /* Get clause list of the nearest enclosing target construct.  */
   15089          521 :               if (ctx->code == OMP_TARGET)
   15090           14 :                 clauses = *orig_list_p;
   15091              :               else
   15092              :                 {
   15093          507 :                   struct gimplify_omp_ctx *tctx = ctx->outer_context;
   15094          826 :                   while (tctx && tctx->code != OMP_TARGET)
   15095          319 :                     tctx = tctx->outer_context;
   15096          507 :                   if (tctx)
   15097           18 :                     clauses = tctx->clauses;
   15098              :                 }
   15099              : 
   15100           32 :               if (clauses)
   15101              :                 {
   15102           30 :                   tree uc;
   15103           30 :                   if (TREE_CODE (allocator) == MEM_REF
   15104           30 :                       || TREE_CODE (allocator) == INDIRECT_REF)
   15105            0 :                     allocator = TREE_OPERAND (allocator, 0);
   15106          132 :                   for (uc = clauses; uc; uc = OMP_CLAUSE_CHAIN (uc))
   15107          123 :                     if (OMP_CLAUSE_CODE (uc) == OMP_CLAUSE_USES_ALLOCATORS)
   15108              :                       {
   15109           29 :                         tree uc_allocator
   15110           29 :                           = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (uc);
   15111           29 :                         if (operand_equal_p (allocator, uc_allocator))
   15112              :                           break;
   15113              :                       }
   15114           30 :                   if (uc == NULL_TREE)
   15115              :                     {
   15116            9 :                       error_at (OMP_CLAUSE_LOCATION (c), "allocator %qE "
   15117              :                                 "requires %<uses_allocators(%E)%> clause in "
   15118              :                                 "target region", allocator, allocator);
   15119            9 :                       remove = true;
   15120            9 :                       break;
   15121              :                     }
   15122              :                 }
   15123              :             }
   15124         3682 :           if (gimplify_expr (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (c), pre_p, NULL,
   15125              :                              is_gimple_val, fb_rvalue) == GS_ERROR)
   15126              :             {
   15127              :               remove = true;
   15128              :               break;
   15129              :             }
   15130         3682 :           else if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
   15131         3682 :                    || (TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
   15132              :                        == INTEGER_CST))
   15133              :             ;
   15134          512 :           else if (code == OMP_TASKLOOP
   15135          512 :                    || !DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
   15136           66 :             OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
   15137          132 :               = get_initialized_tmp_var (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
   15138              :                                          pre_p, NULL, false);
   15139              :           break;
   15140              : 
   15141         4375 :         case OMP_CLAUSE_DEFAULT:
   15142         4375 :           ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
   15143         4375 :           break;
   15144              : 
   15145          654 :         case OMP_CLAUSE_INCLUSIVE:
   15146          654 :         case OMP_CLAUSE_EXCLUSIVE:
   15147          654 :           decl = OMP_CLAUSE_DECL (c);
   15148          654 :           {
   15149          654 :             splay_tree_node n = splay_tree_lookup (outer_ctx->variables,
   15150              :                                                    (splay_tree_key) decl);
   15151          654 :             if (n == NULL || (n->value & GOVD_REDUCTION) == 0)
   15152              :               {
   15153            5 :                 error_at (OMP_CLAUSE_LOCATION (c),
   15154              :                           "%qD specified in %qs clause but not in %<inscan%> "
   15155              :                           "%<reduction%> clause on the containing construct",
   15156            5 :                           decl, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   15157            5 :                 remove = true;
   15158              :               }
   15159              :             else
   15160              :               {
   15161          649 :                 n->value |= GOVD_REDUCTION_INSCAN;
   15162          649 :                 if (outer_ctx->region_type == ORT_SIMD
   15163          530 :                     && outer_ctx->outer_context
   15164          117 :                     && outer_ctx->outer_context->region_type == ORT_WORKSHARE)
   15165              :                   {
   15166          117 :                     n = splay_tree_lookup (outer_ctx->outer_context->variables,
   15167              :                                            (splay_tree_key) decl);
   15168          117 :                     if (n && (n->value & GOVD_REDUCTION) != 0)
   15169          117 :                       n->value |= GOVD_REDUCTION_INSCAN;
   15170              :                   }
   15171              :               }
   15172              :           }
   15173              :           break;
   15174              : 
   15175          103 :         case OMP_CLAUSE_NOVARIANTS:
   15176          103 :           OMP_CLAUSE_NOVARIANTS_EXPR (c)
   15177          103 :             = gimple_boolify (OMP_CLAUSE_NOVARIANTS_EXPR (c));
   15178          103 :           break;
   15179          115 :         case OMP_CLAUSE_NOCONTEXT:
   15180          115 :           OMP_CLAUSE_NOCONTEXT_EXPR (c)
   15181          115 :             = gimple_boolify (OMP_CLAUSE_NOCONTEXT_EXPR (c));
   15182          115 :           break;
   15183            0 :         case OMP_CLAUSE_NOHOST:
   15184            0 :         default:
   15185            0 :           gcc_unreachable ();
   15186              :         }
   15187              : 
   15188       214918 :       if (code == OACC_DATA
   15189         5123 :           && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   15190       219729 :           && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
   15191         4504 :               || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
   15192              :         remove = true;
   15193       214611 :       if (remove)
   15194         1245 :         *list_p = OMP_CLAUSE_CHAIN (c);
   15195              :       else
   15196       213673 :         list_p = &OMP_CLAUSE_CHAIN (c);
   15197       214918 :     }
   15198              : 
   15199       136966 :   if (groups)
   15200              :     {
   15201        38204 :       delete grpmap;
   15202        19102 :       delete groups;
   15203              :     }
   15204              : 
   15205       136966 :   ctx->clauses = *orig_list_p;
   15206       136966 :   gimplify_omp_ctxp = ctx;
   15207       136966 : }
   15208              : 
   15209              : /* Return true if DECL is a candidate for shared to firstprivate
   15210              :    optimization.  We only consider non-addressable scalars, not
   15211              :    too big, and not references.  */
   15212              : 
   15213              : static bool
   15214       424000 : omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
   15215              : {
   15216       424000 :   if (TREE_ADDRESSABLE (decl))
   15217              :     return false;
   15218       369457 :   tree type = TREE_TYPE (decl);
   15219       369457 :   if (!is_gimple_reg_type (type)
   15220       343822 :       || TREE_CODE (type) == REFERENCE_TYPE
   15221       708835 :       || TREE_ADDRESSABLE (type))
   15222              :     return false;
   15223              :   /* Don't optimize too large decls, as each thread/task will have
   15224              :      its own.  */
   15225       339378 :   HOST_WIDE_INT len = int_size_in_bytes (type);
   15226       339378 :   if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
   15227              :     return false;
   15228       339361 :   if (omp_privatize_by_reference (decl))
   15229         1354 :     return false;
   15230              :   return true;
   15231              : }
   15232              : 
   15233              : /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
   15234              :    For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
   15235              :    GOVD_WRITTEN in outer contexts.  */
   15236              : 
   15237              : static void
   15238       315876 : omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
   15239              : {
   15240       493525 :   for (; ctx; ctx = ctx->outer_context)
   15241              :     {
   15242       476305 :       splay_tree_node n = splay_tree_lookup (ctx->variables,
   15243              :                                              (splay_tree_key) decl);
   15244       476305 :       if (n == NULL)
   15245       174044 :         continue;
   15246       302261 :       else if (n->value & GOVD_SHARED)
   15247              :         {
   15248         9227 :           n->value |= GOVD_WRITTEN;
   15249         9227 :           return;
   15250              :         }
   15251       293034 :       else if (n->value & GOVD_DATA_SHARE_CLASS)
   15252              :         return;
   15253              :     }
   15254              : }
   15255              : 
   15256              : /* Helper callback for walk_gimple_seq to discover possible stores
   15257              :    to omp_shared_to_firstprivate_optimizable_decl_p decls and set
   15258              :    GOVD_WRITTEN if they are GOVD_SHARED in some outer context
   15259              :    for those.  */
   15260              : 
   15261              : static tree
   15262      1169778 : omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
   15263              : {
   15264      1169778 :   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
   15265              : 
   15266      1169778 :   *walk_subtrees = 0;
   15267      1169778 :   if (!wi->is_lhs)
   15268              :     return NULL_TREE;
   15269              : 
   15270       336231 :   tree op = *tp;
   15271       418467 :   do
   15272              :     {
   15273       418467 :       if (handled_component_p (op))
   15274        82236 :         op = TREE_OPERAND (op, 0);
   15275       336231 :       else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
   15276       336231 :                && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
   15277            0 :         op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
   15278              :       else
   15279              :         break;
   15280              :     }
   15281              :   while (1);
   15282       336231 :   if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
   15283              :     return NULL_TREE;
   15284              : 
   15285       275010 :   omp_mark_stores (gimplify_omp_ctxp, op);
   15286       275010 :   return NULL_TREE;
   15287              : }
   15288              : 
   15289              : /* Helper callback for walk_gimple_seq to discover possible stores
   15290              :    to omp_shared_to_firstprivate_optimizable_decl_p decls and set
   15291              :    GOVD_WRITTEN if they are GOVD_SHARED in some outer context
   15292              :    for those.  */
   15293              : 
   15294              : static tree
   15295       654221 : omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
   15296              :                       bool *handled_ops_p,
   15297              :                       struct walk_stmt_info *wi)
   15298              : {
   15299       654221 :   gimple *stmt = gsi_stmt (*gsi_p);
   15300       654221 :   switch (gimple_code (stmt))
   15301              :     {
   15302              :     /* Don't recurse on OpenMP constructs for which
   15303              :        gimplify_adjust_omp_clauses already handled the bodies,
   15304              :        except handle gimple_omp_for_pre_body.  */
   15305        28902 :     case GIMPLE_OMP_FOR:
   15306        28902 :       *handled_ops_p = true;
   15307        28902 :       if (gimple_omp_for_pre_body (stmt))
   15308         1896 :         walk_gimple_seq (gimple_omp_for_pre_body (stmt),
   15309              :                          omp_find_stores_stmt, omp_find_stores_op, wi);
   15310              :       break;
   15311         9306 :     case GIMPLE_OMP_PARALLEL:
   15312         9306 :     case GIMPLE_OMP_TASK:
   15313         9306 :     case GIMPLE_OMP_SECTIONS:
   15314         9306 :     case GIMPLE_OMP_SINGLE:
   15315         9306 :     case GIMPLE_OMP_SCOPE:
   15316         9306 :     case GIMPLE_OMP_TARGET:
   15317         9306 :     case GIMPLE_OMP_TEAMS:
   15318         9306 :     case GIMPLE_OMP_CRITICAL:
   15319         9306 :       *handled_ops_p = true;
   15320         9306 :       break;
   15321              :     default:
   15322              :       break;
   15323              :     }
   15324       654221 :   return NULL_TREE;
   15325              : }
   15326              : 
   15327              : struct gimplify_adjust_omp_clauses_data
   15328              : {
   15329              :   tree *list_p;
   15330              :   gimple_seq *pre_p;
   15331              : };
   15332              : 
   15333              : /* For all variables that were not actually used within the context,
   15334              :    remove PRIVATE, SHARED, and FIRSTPRIVATE clauses.  */
   15335              : 
   15336              : static int
   15337       707642 : gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
   15338              : {
   15339       707642 :   tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
   15340       707642 :   gimple_seq *pre_p
   15341              :     = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
   15342       707642 :   tree decl = (tree) n->key;
   15343       707642 :   unsigned flags = n->value;
   15344       707642 :   enum omp_clause_code code;
   15345       707642 :   tree clause;
   15346       707642 :   bool private_debug;
   15347              : 
   15348       707642 :   if (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
   15349       142297 :       && (flags & GOVD_LASTPRIVATE_CONDITIONAL) != 0)
   15350              :     flags = GOVD_SHARED | GOVD_SEEN | GOVD_WRITTEN;
   15351       707513 :   if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
   15352              :     return 0;
   15353       187978 :   if ((flags & GOVD_SEEN) == 0)
   15354              :     return 0;
   15355       166792 :   if (flags & GOVD_DEBUG_PRIVATE)
   15356              :     {
   15357          261 :       gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
   15358              :       private_debug = true;
   15359              :     }
   15360       166531 :   else if (flags & GOVD_MAP)
   15361              :     private_debug = false;
   15362              :   else
   15363       147445 :     private_debug
   15364       147445 :       = lang_hooks.decls.omp_private_debug_clause (decl,
   15365       147445 :                                                    !!(flags & GOVD_SHARED));
   15366       147445 :   if (private_debug)
   15367              :     code = OMP_CLAUSE_PRIVATE;
   15368       166409 :   else if (flags & GOVD_MAP)
   15369              :     {
   15370        19086 :       code = OMP_CLAUSE_MAP;
   15371        19086 :       if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
   15372        19086 :           && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
   15373              :         {
   15374            2 :           error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
   15375            2 :           return 0;
   15376              :         }
   15377        19084 :       if (VAR_P (decl)
   15378        17148 :           && DECL_IN_CONSTANT_POOL (decl)
   15379        19085 :           && !lookup_attribute ("omp declare target",
   15380            1 :                                 DECL_ATTRIBUTES (decl)))
   15381              :         {
   15382            1 :           tree id = get_identifier ("omp declare target");
   15383            1 :           DECL_ATTRIBUTES (decl)
   15384            1 :             = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
   15385            1 :           varpool_node *node = varpool_node::get (decl);
   15386            1 :           if (node)
   15387              :             {
   15388            1 :               node->offloadable = 1;
   15389            1 :               if (ENABLE_OFFLOADING)
   15390              :                 g->have_offload = true;
   15391              :             }
   15392              :         }
   15393              :     }
   15394       147323 :   else if (flags & GOVD_SHARED)
   15395              :     {
   15396        51437 :       if (is_global_var (decl))
   15397              :         {
   15398        17484 :           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
   15399        27607 :           while (ctx != NULL)
   15400              :             {
   15401        19746 :               splay_tree_node on
   15402        19746 :                 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   15403        19746 :               if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
   15404              :                                       | GOVD_PRIVATE | GOVD_REDUCTION
   15405              :                                       | GOVD_LINEAR | GOVD_MAP)) != 0)
   15406              :                 break;
   15407        10123 :               ctx = ctx->outer_context;
   15408              :             }
   15409        17484 :           if (ctx == NULL)
   15410              :             return 0;
   15411              :         }
   15412        43576 :       code = OMP_CLAUSE_SHARED;
   15413              :       /* Don't optimize shared into firstprivate for read-only vars
   15414              :          on tasks with depend clause, we shouldn't try to copy them
   15415              :          until the dependencies are satisfied.  */
   15416        43576 :       if (gimplify_omp_ctxp->has_depend)
   15417          350 :         flags |= GOVD_WRITTEN;
   15418              :     }
   15419        95886 :   else if (flags & GOVD_PRIVATE)
   15420              :     code = OMP_CLAUSE_PRIVATE;
   15421        32396 :   else if (flags & GOVD_FIRSTPRIVATE)
   15422              :     {
   15423        22252 :       code = OMP_CLAUSE_FIRSTPRIVATE;
   15424        22252 :       if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
   15425        14163 :           && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
   15426        32734 :           && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
   15427              :         {
   15428            1 :           error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
   15429              :                  "%<target%> construct", decl);
   15430            1 :           return 0;
   15431              :         }
   15432              :     }
   15433        10144 :   else if (flags & GOVD_LASTPRIVATE)
   15434              :     code = OMP_CLAUSE_LASTPRIVATE;
   15435          357 :   else if (flags & (GOVD_ALIGNED | GOVD_NONTEMPORAL))
   15436              :     return 0;
   15437          234 :   else if (flags & GOVD_CONDTEMP)
   15438              :     {
   15439          122 :       code = OMP_CLAUSE__CONDTEMP_;
   15440          122 :       gimple_add_tmp_var (decl);
   15441              :     }
   15442          112 :   else if (flags & GOVD_USES_ALLOCATORS_ALLOCATOR)
   15443              :     return 0;
   15444              :   else
   15445            0 :     gcc_unreachable ();
   15446              : 
   15447       148906 :   if (((flags & GOVD_LASTPRIVATE)
   15448       148216 :        || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
   15449       164860 :       && omp_shared_to_firstprivate_optimizable_decl_p (decl))
   15450        15407 :     omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
   15451              : 
   15452       158693 :   tree chain = *list_p;
   15453       158693 :   clause = build_omp_clause (input_location, code);
   15454       158693 :   OMP_CLAUSE_DECL (clause) = decl;
   15455       158693 :   OMP_CLAUSE_CHAIN (clause) = chain;
   15456       158693 :   if (private_debug)
   15457          383 :     OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
   15458       158310 :   else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
   15459            6 :     OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
   15460       158304 :   else if (code == OMP_CLAUSE_SHARED
   15461        43576 :            && (flags & GOVD_WRITTEN) == 0
   15462       195713 :            && omp_shared_to_firstprivate_optimizable_decl_p (decl))
   15463        21598 :     OMP_CLAUSE_SHARED_READONLY (clause) = 1;
   15464       136706 :   else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
   15465        22251 :     OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
   15466       114455 :   else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
   15467              :     {
   15468          630 :       tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
   15469          630 :       OMP_CLAUSE_DECL (nc) = decl;
   15470          630 :       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
   15471          630 :           && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
   15472           15 :         OMP_CLAUSE_DECL (clause)
   15473           30 :           = build_fold_indirect_ref_loc (input_location, decl);
   15474          630 :       OMP_CLAUSE_DECL (clause)
   15475          630 :         = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
   15476              :                   build_int_cst (build_pointer_type (char_type_node), 0));
   15477          630 :       OMP_CLAUSE_SIZE (clause) = size_zero_node;
   15478          630 :       OMP_CLAUSE_SIZE (nc) = size_zero_node;
   15479          630 :       OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
   15480          630 :       OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
   15481          630 :       tree dtype = TREE_TYPE (decl);
   15482          630 :       if (TREE_CODE (dtype) == REFERENCE_TYPE)
   15483           15 :         dtype = TREE_TYPE (dtype);
   15484              :       /* FIRSTPRIVATE_POINTER doesn't work well if we have a
   15485              :          multiply-indirected pointer.  If we have a reference to a pointer to
   15486              :          a pointer, it's possible that this should really be
   15487              :          GOMP_MAP_FIRSTPRIVATE_REFERENCE -- but that also doesn't work at the
   15488              :          moment, so stick with this.  (See PR113279 and testcases
   15489              :          baseptrs-{4,6}.C:ref2ptrptr_offset_decl_member_slice).  */
   15490          630 :       if (TREE_CODE (dtype) == POINTER_TYPE
   15491          630 :           && TREE_CODE (TREE_TYPE (dtype)) == POINTER_TYPE)
   15492           19 :         OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
   15493              :       else
   15494          611 :         OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
   15495          630 :       OMP_CLAUSE_CHAIN (nc) = chain;
   15496          630 :       OMP_CLAUSE_CHAIN (clause) = nc;
   15497          630 :       struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   15498          630 :       gimplify_omp_ctxp = ctx->outer_context;
   15499          630 :       gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
   15500              :                      pre_p, NULL, is_gimple_val, fb_rvalue);
   15501          630 :       gimplify_omp_ctxp = ctx;
   15502          630 :     }
   15503        18454 :   else if (code == OMP_CLAUSE_MAP)
   15504              :     {
   15505        18454 :       int kind;
   15506              :       /* Not all combinations of these GOVD_MAP flags are actually valid.  */
   15507        18454 :       switch (flags & (GOVD_MAP_TO_ONLY
   15508              :                        | GOVD_MAP_FORCE
   15509              :                        | GOVD_MAP_FORCE_PRESENT
   15510              :                        | GOVD_MAP_ALLOC_ONLY
   15511              :                        | GOVD_MAP_FROM_ONLY))
   15512              :         {
   15513              :         case 0:
   15514              :           kind = GOMP_MAP_TOFROM;
   15515              :           break;
   15516         1042 :         case GOVD_MAP_FORCE:
   15517         1042 :           kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
   15518         1042 :           break;
   15519          931 :         case GOVD_MAP_TO_ONLY:
   15520          931 :           kind = GOMP_MAP_TO;
   15521          931 :           break;
   15522           16 :         case GOVD_MAP_FROM_ONLY:
   15523           16 :           kind = GOMP_MAP_FROM;
   15524           16 :           break;
   15525           37 :         case GOVD_MAP_ALLOC_ONLY:
   15526           37 :           kind = GOMP_MAP_ALLOC;
   15527           37 :           break;
   15528            3 :         case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
   15529            3 :           kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
   15530            3 :           break;
   15531              :         case GOVD_MAP_FORCE_PRESENT:
   15532          326 :           kind = GOMP_MAP_FORCE_PRESENT;
   15533              :           break;
   15534              :         case GOVD_MAP_FORCE_PRESENT | GOVD_MAP_ALLOC_ONLY:
   15535          326 :           kind = GOMP_MAP_FORCE_PRESENT;
   15536              :           break;
   15537            0 :         default:
   15538            0 :           gcc_unreachable ();
   15539              :         }
   15540        18454 :       OMP_CLAUSE_SET_MAP_KIND (clause, kind);
   15541              :       /* Setting of the implicit flag for the runtime is currently disabled for
   15542              :          OpenACC.  */
   15543        18454 :       if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
   15544        10920 :         OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (clause) = 1;
   15545        18454 :       if (DECL_SIZE (decl)
   15546        18454 :           && !poly_int_tree_p (DECL_SIZE (decl)))
   15547              :         {
   15548          537 :           tree decl2 = DECL_VALUE_EXPR (decl);
   15549          537 :           gcc_assert (INDIRECT_REF_P (decl2));
   15550          537 :           decl2 = TREE_OPERAND (decl2, 0);
   15551          537 :           gcc_assert (DECL_P (decl2));
   15552          537 :           tree mem = build_simple_mem_ref (decl2);
   15553          537 :           OMP_CLAUSE_DECL (clause) = mem;
   15554          537 :           OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
   15555          537 :           if (gimplify_omp_ctxp->outer_context)
   15556              :             {
   15557          446 :               struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
   15558          446 :               omp_notice_variable (ctx, decl2, true);
   15559          446 :               omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
   15560              :             }
   15561          537 :           tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
   15562              :                                       OMP_CLAUSE_MAP);
   15563          537 :           OMP_CLAUSE_DECL (nc) = decl;
   15564          537 :           OMP_CLAUSE_SIZE (nc) = size_zero_node;
   15565          537 :           if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
   15566          537 :             OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
   15567              :           else
   15568            0 :             OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
   15569          537 :           OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
   15570          537 :           OMP_CLAUSE_CHAIN (clause) = nc;
   15571              :         }
   15572        17917 :       else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
   15573        17917 :                && omp_privatize_by_reference (decl))
   15574              :         {
   15575           28 :           OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
   15576           28 :           OMP_CLAUSE_SIZE (clause)
   15577           28 :             = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
   15578           28 :           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   15579           28 :           gimplify_omp_ctxp = ctx->outer_context;
   15580           28 :           gimplify_expr (&OMP_CLAUSE_SIZE (clause),
   15581              :                          pre_p, NULL, is_gimple_val, fb_rvalue);
   15582           28 :           gimplify_omp_ctxp = ctx;
   15583           28 :           tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
   15584              :                                       OMP_CLAUSE_MAP);
   15585           28 :           OMP_CLAUSE_DECL (nc) = decl;
   15586           28 :           OMP_CLAUSE_SIZE (nc) = size_zero_node;
   15587           28 :           OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
   15588           28 :           OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
   15589           28 :           OMP_CLAUSE_CHAIN (clause) = nc;
   15590              :         }
   15591              :       else
   15592        17889 :         OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
   15593              :     }
   15594       158693 :   if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
   15595              :     {
   15596          690 :       tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
   15597          690 :       OMP_CLAUSE_DECL (nc) = decl;
   15598          690 :       OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
   15599          690 :       OMP_CLAUSE_CHAIN (nc) = chain;
   15600          690 :       OMP_CLAUSE_CHAIN (clause) = nc;
   15601          690 :       struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   15602          690 :       gimplify_omp_ctxp = ctx->outer_context;
   15603          690 :       lang_hooks.decls.omp_finish_clause (nc, pre_p,
   15604          690 :                                           (ctx->region_type & ORT_ACC) != 0);
   15605          690 :       gimplify_omp_ctxp = ctx;
   15606              :     }
   15607       158693 :   *list_p = clause;
   15608       158693 :   struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   15609       158693 :   gimplify_omp_ctxp = ctx->outer_context;
   15610              :   /* Don't call omp_finish_clause on implicitly added OMP_CLAUSE_PRIVATE
   15611              :      in simd.  Those are only added for the local vars inside of simd body
   15612              :      and they don't need to be e.g. default constructible.  */
   15613       158693 :   if (code != OMP_CLAUSE_PRIVATE || ctx->region_type != ORT_SIMD)
   15614       148533 :     lang_hooks.decls.omp_finish_clause (clause, pre_p,
   15615       148533 :                                         (ctx->region_type & ORT_ACC) != 0);
   15616       158693 :   if (gimplify_omp_ctxp)
   15617       198997 :     for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
   15618       100911 :       if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
   15619       100911 :           && DECL_P (OMP_CLAUSE_SIZE (clause)))
   15620         1241 :         omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
   15621              :                              true);
   15622       158693 :   gimplify_omp_ctxp = ctx;
   15623       158693 :   return 0;
   15624              : }
   15625              : 
   15626              : static void
   15627       135502 : gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
   15628              :                              enum tree_code code,
   15629              :                              gimple_seq *loops_seq_p = NULL)
   15630              : {
   15631       135502 :   struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   15632       135502 :   tree *orig_list_p = list_p;
   15633       135502 :   tree c, decl;
   15634       135502 :   bool has_inscan_reductions = false;
   15635              : 
   15636       135502 :   if (body)
   15637              :     {
   15638              :       struct gimplify_omp_ctx *octx;
   15639       235802 :       for (octx = ctx; octx; octx = octx->outer_context)
   15640       182865 :         if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
   15641              :           break;
   15642       117386 :       if (octx)
   15643              :         {
   15644        64449 :           struct walk_stmt_info wi;
   15645        64449 :           memset (&wi, 0, sizeof (wi));
   15646        64449 :           walk_gimple_seq (body, omp_find_stores_stmt,
   15647              :                            omp_find_stores_op, &wi);
   15648              :         }
   15649              :     }
   15650              : 
   15651       135502 :   if (ctx->add_safelen1)
   15652              :     {
   15653              :       /* If there are VLAs in the body of simd loop, prevent
   15654              :          vectorization.  */
   15655            2 :       gcc_assert (ctx->region_type == ORT_SIMD);
   15656            2 :       c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN);
   15657            2 :       OMP_CLAUSE_SAFELEN_EXPR (c) = integer_one_node;
   15658            2 :       OMP_CLAUSE_CHAIN (c) = *list_p;
   15659            2 :       *list_p = c;
   15660            2 :       list_p = &OMP_CLAUSE_CHAIN (c);
   15661              :     }
   15662              : 
   15663       135502 :   if (ctx->region_type == ORT_WORKSHARE
   15664        41753 :       && ctx->outer_context
   15665        29430 :       && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
   15666              :     {
   15667        26145 :       for (c = ctx->outer_context->clauses; c; c = OMP_CLAUSE_CHAIN (c))
   15668        13040 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
   15669        13040 :             && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
   15670              :           {
   15671          129 :             decl = OMP_CLAUSE_DECL (c);
   15672          129 :             splay_tree_node n
   15673          129 :               = splay_tree_lookup (ctx->outer_context->variables,
   15674              :                                    (splay_tree_key) decl);
   15675          129 :             gcc_checking_assert (!splay_tree_lookup (ctx->variables,
   15676              :                                                      (splay_tree_key) decl));
   15677          129 :             omp_add_variable (ctx, decl, n->value);
   15678          129 :             tree c2 = copy_node (c);
   15679          129 :             OMP_CLAUSE_CHAIN (c2) = *list_p;
   15680          129 :             *list_p = c2;
   15681          129 :             if ((n->value & GOVD_FIRSTPRIVATE) == 0)
   15682          107 :               continue;
   15683           22 :             c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   15684              :                                    OMP_CLAUSE_FIRSTPRIVATE);
   15685           22 :             OMP_CLAUSE_DECL (c2) = decl;
   15686           22 :             OMP_CLAUSE_CHAIN (c2) = *list_p;
   15687           22 :             *list_p = c2;
   15688              :           }
   15689              :     }
   15690              : 
   15691       135502 :   if (code == OMP_TARGET
   15692       135502 :       || code == OMP_TARGET_DATA
   15693       135502 :       || code == OMP_TARGET_ENTER_DATA
   15694       118029 :       || code == OMP_TARGET_EXIT_DATA)
   15695              :     {
   15696        18264 :       tree mapper_clauses = NULL_TREE;
   15697        18264 :       instantiate_mapper_info im_info;
   15698              : 
   15699        18264 :       im_info.mapper_clauses_p = &mapper_clauses;
   15700        18264 :       im_info.omp_ctx = ctx;
   15701        18264 :       im_info.pre_p = pre_p;
   15702              : 
   15703        18264 :       splay_tree_foreach (ctx->variables,
   15704              :                           omp_instantiate_implicit_mappers,
   15705              :                           (void *) &im_info);
   15706              : 
   15707        18264 :       if (mapper_clauses)
   15708              :         {
   15709           53 :           mapper_clauses
   15710           53 :             = lang_hooks.decls.omp_finish_mapper_clauses (mapper_clauses);
   15711              : 
   15712              :           /* Stick the implicitly-expanded mapper clauses at the end of the
   15713              :              clause list.  */
   15714           53 :           tree *tail = list_p;
   15715          163 :           while (*tail)
   15716          110 :             tail = &OMP_CLAUSE_CHAIN (*tail);
   15717           53 :           *tail = mapper_clauses;
   15718              :         }
   15719              : 
   15720        18264 :       vec<omp_mapping_group> *groups;
   15721        18264 :       groups = omp_gather_mapping_groups (list_p);
   15722        18264 :       hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
   15723              : 
   15724        18264 :       if (groups)
   15725              :         {
   15726         9439 :           grpmap = omp_index_mapping_groups (groups);
   15727              : 
   15728         9439 :           omp_resolve_clause_dependencies (code, groups, grpmap);
   15729         9439 :           omp_build_struct_sibling_lists (code, ctx->region_type, groups,
   15730              :                                           &grpmap, list_p);
   15731              : 
   15732         9439 :           omp_mapping_group *outlist = NULL;
   15733              : 
   15734        18878 :           delete grpmap;
   15735         9439 :           delete groups;
   15736              : 
   15737              :           /* Rebuild now we have struct sibling lists.  */
   15738         9439 :           groups = omp_gather_mapping_groups (list_p);
   15739         9439 :           grpmap = omp_index_mapping_groups (groups);
   15740              : 
   15741         9439 :           bool enter_exit = (code == OMP_TARGET_ENTER_DATA
   15742         9439 :                              || code == OMP_TARGET_EXIT_DATA);
   15743              : 
   15744         9439 :           outlist = omp_tsort_mapping_groups (groups, grpmap, enter_exit);
   15745         9439 :           outlist = omp_segregate_mapping_groups (outlist);
   15746         9439 :           list_p = omp_reorder_mapping_groups (groups, outlist, list_p);
   15747              : 
   15748         9439 :           delete grpmap;
   15749         9439 :           delete groups;
   15750              :         }
   15751        18264 :     }
   15752       117238 :   else if (ctx->region_type & ORT_ACC)
   15753              :     {
   15754        29899 :       vec<omp_mapping_group> *groups;
   15755        29899 :       groups = omp_gather_mapping_groups (list_p);
   15756        29899 :       if (groups)
   15757              :         {
   15758         9462 :           hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap;
   15759         9462 :           grpmap = omp_index_mapping_groups (groups);
   15760              : 
   15761         9462 :           oacc_resolve_clause_dependencies (groups, grpmap);
   15762         9462 :           omp_build_struct_sibling_lists (code, ctx->region_type, groups,
   15763              :                                           &grpmap, list_p);
   15764              : 
   15765         9462 :           delete groups;
   15766        18924 :           delete grpmap;
   15767              :         }
   15768              :     }
   15769              : 
   15770       135502 :   tree attach_list = NULL_TREE;
   15771       135502 :   tree *attach_tail = &attach_list;
   15772              : 
   15773       135502 :   tree *grp_start_p = NULL, grp_end = NULL_TREE;
   15774              : 
   15775       397688 :   while ((c = *list_p) != NULL)
   15776              :     {
   15777       262186 :       splay_tree_node n;
   15778       262186 :       bool remove = false;
   15779       262186 :       bool move_attach = false;
   15780              : 
   15781       305335 :       if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
   15782              :         grp_end = NULL_TREE;
   15783              : 
   15784       262186 :       switch (OMP_CLAUSE_CODE (c))
   15785              :         {
   15786         8742 :         case OMP_CLAUSE_FIRSTPRIVATE:
   15787         8742 :           if ((ctx->region_type & ORT_TARGET)
   15788         2434 :               && (ctx->region_type & ORT_ACC) == 0
   15789        10570 :               && TYPE_ATOMIC (strip_array_types
   15790              :                                         (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
   15791              :             {
   15792            4 :               error_at (OMP_CLAUSE_LOCATION (c),
   15793              :                         "%<_Atomic%> %qD in %<firstprivate%> clause on "
   15794            2 :                         "%<target%> construct", OMP_CLAUSE_DECL (c));
   15795            2 :               remove = true;
   15796            2 :               break;
   15797              :             }
   15798         8740 :           if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
   15799              :             {
   15800          516 :               decl = OMP_CLAUSE_DECL (c);
   15801          516 :               n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   15802          516 :               if ((n->value & GOVD_MAP) != 0)
   15803              :                 {
   15804              :                   remove = true;
   15805              :                   break;
   15806              :                 }
   15807          504 :               OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c) = 0;
   15808          504 :               OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) = 0;
   15809              :             }
   15810              :           /* FALLTHRU */
   15811        39289 :         case OMP_CLAUSE_PRIVATE:
   15812        39289 :         case OMP_CLAUSE_SHARED:
   15813        39289 :         case OMP_CLAUSE_LINEAR:
   15814        39289 :           decl = OMP_CLAUSE_DECL (c);
   15815        39289 :           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   15816        39289 :           remove = !(n->value & GOVD_SEEN);
   15817        39289 :           if ((n->value & GOVD_LASTPRIVATE_CONDITIONAL) != 0
   15818           48 :               && code == OMP_PARALLEL
   15819        39311 :               && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
   15820              :             remove = true;
   15821        39267 :           if (! remove)
   15822              :             {
   15823        34656 :               bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
   15824        34656 :               if ((n->value & GOVD_DEBUG_PRIVATE)
   15825        34656 :                   || lang_hooks.decls.omp_private_debug_clause (decl, shared))
   15826              :                 {
   15827           87 :                   gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
   15828              :                               || ((n->value & GOVD_DATA_SHARE_CLASS)
   15829              :                                   == GOVD_SHARED));
   15830           87 :                   OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
   15831           87 :                   OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
   15832              :                 }
   15833        34656 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
   15834         4207 :                   && ctx->has_depend
   15835        35214 :                   && DECL_P (decl))
   15836          558 :                 n->value |= GOVD_WRITTEN;
   15837        34656 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
   15838         4207 :                   && (n->value & GOVD_WRITTEN) == 0
   15839         3088 :                   && DECL_P (decl)
   15840        37744 :                   && omp_shared_to_firstprivate_optimizable_decl_p (decl))
   15841          533 :                 OMP_CLAUSE_SHARED_READONLY (c) = 1;
   15842        34123 :               else if (DECL_P (decl)
   15843        34123 :                        && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
   15844         3674 :                             && (n->value & GOVD_WRITTEN) != 0)
   15845        33004 :                            || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
   15846         9238 :                                && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
   15847        41248 :                        && omp_shared_to_firstprivate_optimizable_decl_p (decl))
   15848         5792 :                 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
   15849              :             }
   15850              :           else
   15851         4633 :             n->value &= ~GOVD_EXPLICIT;
   15852              :           break;
   15853              : 
   15854        13957 :         case OMP_CLAUSE_LASTPRIVATE:
   15855              :           /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
   15856              :              accurately reflect the presence of a FIRSTPRIVATE clause.  */
   15857        13957 :           decl = OMP_CLAUSE_DECL (c);
   15858        13957 :           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   15859        13957 :           OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
   15860        13957 :             = (n->value & GOVD_FIRSTPRIVATE) != 0;
   15861        13957 :           if (code == OMP_DISTRIBUTE
   15862        13957 :               && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
   15863              :             {
   15864            4 :               remove = true;
   15865            4 :               error_at (OMP_CLAUSE_LOCATION (c),
   15866              :                         "same variable used in %<firstprivate%> and "
   15867              :                         "%<lastprivate%> clauses on %<distribute%> "
   15868              :                         "construct");
   15869              :             }
   15870        13957 :           if (!remove
   15871        13953 :               && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
   15872        13953 :               && DECL_P (decl)
   15873        13953 :               && omp_shared_to_firstprivate_optimizable_decl_p (decl))
   15874        11619 :             omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
   15875        13957 :           if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) && code == OMP_PARALLEL)
   15876              :             remove = true;
   15877              :           break;
   15878              : 
   15879          824 :         case OMP_CLAUSE_ALIGNED:
   15880          824 :           decl = OMP_CLAUSE_DECL (c);
   15881          824 :           if (!is_global_var (decl))
   15882              :             {
   15883          738 :               n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   15884          738 :               remove = n == NULL || !(n->value & GOVD_SEEN);
   15885           88 :               if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
   15886              :                 {
   15887           88 :                   struct gimplify_omp_ctx *octx;
   15888           88 :                   if (n != NULL
   15889           88 :                       && (n->value & (GOVD_DATA_SHARE_CLASS
   15890              :                                       & ~GOVD_FIRSTPRIVATE)))
   15891              :                     remove = true;
   15892              :                   else
   15893          100 :                     for (octx = ctx->outer_context; octx;
   15894           12 :                          octx = octx->outer_context)
   15895              :                       {
   15896           24 :                         n = splay_tree_lookup (octx->variables,
   15897              :                                                (splay_tree_key) decl);
   15898           24 :                         if (n == NULL)
   15899           12 :                           continue;
   15900           12 :                         if (n->value & GOVD_LOCAL)
   15901              :                           break;
   15902              :                         /* We have to avoid assigning a shared variable
   15903              :                            to itself when trying to add
   15904              :                            __builtin_assume_aligned.  */
   15905           12 :                         if (n->value & GOVD_SHARED)
   15906              :                           {
   15907              :                             remove = true;
   15908              :                             break;
   15909              :                           }
   15910              :                       }
   15911              :                 }
   15912              :             }
   15913           86 :           else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
   15914              :             {
   15915           86 :               n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   15916           86 :               if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
   15917              :                 remove = true;
   15918              :             }
   15919              :           break;
   15920              : 
   15921          553 :         case OMP_CLAUSE_HAS_DEVICE_ADDR:
   15922          553 :           decl = OMP_CLAUSE_DECL (c);
   15923          553 :           while (INDIRECT_REF_P (decl)
   15924          602 :                  || TREE_CODE (decl) == ARRAY_REF)
   15925           49 :             decl = TREE_OPERAND (decl, 0);
   15926          553 :           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   15927          553 :           remove = n == NULL || !(n->value & GOVD_SEEN);
   15928              :           break;
   15929              : 
   15930          900 :         case OMP_CLAUSE_IS_DEVICE_PTR:
   15931          900 :         case OMP_CLAUSE_NONTEMPORAL:
   15932          900 :           decl = OMP_CLAUSE_DECL (c);
   15933          900 :           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   15934          900 :           remove = n == NULL || !(n->value & GOVD_SEEN);
   15935              :           break;
   15936              : 
   15937        58067 :         case OMP_CLAUSE_MAP:
   15938        58067 :           if (OMP_CLAUSE_MAP_GIMPLE_ONLY (c))
   15939              :             {
   15940           90 :               remove = true;
   15941           90 :               goto end_adjust_omp_map_clause;
   15942              :             }
   15943        57977 :           decl = OMP_CLAUSE_DECL (c);
   15944        57977 :           if (!grp_end)
   15945              :             {
   15946        32942 :               grp_start_p = list_p;
   15947        32942 :               grp_end = *omp_group_last (grp_start_p);
   15948              :             }
   15949        57977 :           switch (OMP_CLAUSE_MAP_KIND (c))
   15950              :             {
   15951          183 :             case GOMP_MAP_PRESENT_ALLOC:
   15952          183 :             case GOMP_MAP_PRESENT_TO:
   15953          183 :             case GOMP_MAP_PRESENT_FROM:
   15954          183 :             case GOMP_MAP_PRESENT_TOFROM:
   15955          183 :               OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_PRESENT);
   15956          183 :               break;
   15957              :             default:
   15958              :               break;
   15959              :             }
   15960        57977 :           switch (code)
   15961              :             {
   15962         4563 :             case OACC_DATA:
   15963         4563 :               if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
   15964              :                 break;
   15965              :               /* Fallthrough.  */
   15966        19317 :             case OACC_HOST_DATA:
   15967        19317 :             case OACC_ENTER_DATA:
   15968        19317 :             case OACC_EXIT_DATA:
   15969        19317 :             case OMP_TARGET_DATA:
   15970        19317 :             case OMP_TARGET_ENTER_DATA:
   15971        19317 :             case OMP_TARGET_EXIT_DATA:
   15972        19317 :               if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
   15973        19317 :                   || (OMP_CLAUSE_MAP_KIND (c)
   15974              :                       == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
   15975              :                 /* For target {,enter ,exit }data only the array slice is
   15976              :                    mapped, but not the pointer to it.  */
   15977              :                 remove = true;
   15978        19317 :               if (code == OMP_TARGET_EXIT_DATA
   15979        19317 :                   && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
   15980         2805 :                       || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER))
   15981              :                 remove = true;
   15982              :               break;
   15983              :             case OMP_TARGET:
   15984              :               break;
   15985              :             default:
   15986              :               break;
   15987              :             }
   15988        19315 :           if (remove)
   15989              :             break;
   15990        57642 :           if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
   15991              :             {
   15992              :               /* Sanity check: attach/detach map kinds use the size as a bias,
   15993              :                  and it's never right to use the decl size for such
   15994              :                  mappings.  */
   15995        18812 :               gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
   15996              :                           && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
   15997              :                           && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH
   15998              :                           && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
   15999              :                           && (OMP_CLAUSE_MAP_KIND (c)
   16000              :                               != GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
   16001        22873 :               OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
   16002         4061 :                                     : TYPE_SIZE_UNIT (TREE_TYPE (decl));
   16003              :             }
   16004        57642 :           gimplify_omp_ctxp = ctx->outer_context;
   16005        57642 :           gimple_seq *seq_p;
   16006        57642 :           seq_p = enter_omp_iterator_loop_context (c, loops_seq_p, pre_p);
   16007        57642 :           if (gimplify_expr (&OMP_CLAUSE_SIZE (c), seq_p, NULL,
   16008              :                                   is_gimple_val, fb_rvalue) == GS_ERROR)
   16009              :             {
   16010            0 :               gimplify_omp_ctxp = ctx;
   16011            0 :               remove = true;
   16012            0 :               goto end_adjust_omp_map_clause;
   16013              :             }
   16014        57642 :           else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
   16015        54669 :                     || (OMP_CLAUSE_MAP_KIND (c)
   16016              :                         == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
   16017        54256 :                     || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
   16018        64337 :                    && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
   16019              :             {
   16020          912 :               OMP_CLAUSE_SIZE (c)
   16021          912 :                 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), seq_p, NULL,
   16022              :                                            false);
   16023          912 :               if ((ctx->region_type & ORT_TARGET) != 0)
   16024          703 :                 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
   16025              :                                   GOVD_FIRSTPRIVATE | GOVD_SEEN);
   16026              :             }
   16027        57642 :           gimplify_omp_ctxp = ctx;
   16028              :           /* Data clauses associated with reductions must be
   16029              :              compatible with present_or_copy.  Warn and adjust the clause
   16030              :              if that is not the case.  */
   16031        57642 :           if (ctx->region_type == ORT_ACC_PARALLEL
   16032        48663 :               || ctx->region_type == ORT_ACC_SERIAL)
   16033              :             {
   16034         9535 :               tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
   16035         9535 :               n = NULL;
   16036              : 
   16037         9535 :               if (DECL_P (t))
   16038         7770 :                 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
   16039              : 
   16040         7770 :               if (n && (n->value & GOVD_REDUCTION))
   16041              :                 {
   16042          809 :                   enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
   16043              : 
   16044          809 :                   OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
   16045          809 :                   if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
   16046          159 :                       && kind != GOMP_MAP_FORCE_PRESENT
   16047          159 :                       && kind != GOMP_MAP_POINTER)
   16048              :                     {
   16049          120 :                       warning_at (OMP_CLAUSE_LOCATION (c), 0,
   16050              :                                   "incompatible data clause with reduction "
   16051              :                                   "on %qE; promoting to %<present_or_copy%>",
   16052          120 :                                   DECL_NAME (t));
   16053          120 :                       OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
   16054              :                     }
   16055              :                 }
   16056              :             }
   16057        57642 :           if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
   16058        54846 :                || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
   16059        57917 :               && (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA))
   16060              :             {
   16061          536 :               remove = true;
   16062          536 :               goto end_adjust_omp_map_clause;
   16063              :             }
   16064              :           /* If we have a DECL_VALUE_EXPR (e.g. this is a class member and/or
   16065              :              a variable captured in a lambda closure), look through that now
   16066              :              before the DECL_P check below.  (A code other than COMPONENT_REF,
   16067              :              i.e. INDIRECT_REF, will be a VLA/variable-length array
   16068              :              section.  A global var may be a variable in a common block.  We
   16069              :              don't want to do this here for either of those.)  */
   16070        57106 :           if ((ctx->region_type & ORT_ACC) == 0
   16071        30899 :               && DECL_P (decl)
   16072        13822 :               && !is_global_var (decl)
   16073        11893 :               && DECL_HAS_VALUE_EXPR_P (decl)
   16074        57296 :               && TREE_CODE (DECL_VALUE_EXPR (decl)) == COMPONENT_REF)
   16075            0 :             decl = OMP_CLAUSE_DECL (c) = DECL_VALUE_EXPR (decl);
   16076        57106 :           if (TREE_CODE (decl) == TARGET_EXPR)
   16077              :             {
   16078            0 :               if (gimplify_expr (&OMP_CLAUSE_DECL (c), seq_p, NULL,
   16079              :                                  is_gimple_lvalue, fb_lvalue) == GS_ERROR)
   16080        27381 :                 remove = true;
   16081              :             }
   16082        57106 :           else if (!DECL_P (decl))
   16083              :             {
   16084        29725 :               if ((ctx->region_type & ORT_TARGET) != 0
   16085        29725 :                   && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
   16086              :                 {
   16087           24 :                   if (INDIRECT_REF_P (decl)
   16088            0 :                       && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
   16089           24 :                       && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
   16090              :                           == REFERENCE_TYPE))
   16091            0 :                     decl = TREE_OPERAND (decl, 0);
   16092           24 :                   if (TREE_CODE (decl) == COMPONENT_REF)
   16093              :                     {
   16094            0 :                       while (TREE_CODE (decl) == COMPONENT_REF)
   16095            0 :                         decl = TREE_OPERAND (decl, 0);
   16096            0 :                       if (DECL_P (decl))
   16097              :                         {
   16098            0 :                           n = splay_tree_lookup (ctx->variables,
   16099              :                                                  (splay_tree_key) decl);
   16100            0 :                           if (!(n->value & GOVD_SEEN))
   16101        29725 :                             remove = true;
   16102              :                         }
   16103              :                     }
   16104              :                 }
   16105              : 
   16106        29725 :               tree d = decl, *pd;
   16107        29725 :               if (TREE_CODE (d) == ARRAY_REF)
   16108              :                 {
   16109         5844 :                   while (TREE_CODE (d) == ARRAY_REF)
   16110         2982 :                     d = TREE_OPERAND (d, 0);
   16111         2862 :                   if (TREE_CODE (d) == COMPONENT_REF
   16112         2862 :                       && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
   16113              :                     decl = d;
   16114              :                 }
   16115        29725 :               pd = &OMP_CLAUSE_DECL (c);
   16116        29725 :               if (d == decl
   16117        27222 :                   && TREE_CODE (decl) == INDIRECT_REF
   16118        15542 :                   && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
   16119         1485 :                   && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
   16120              :                       == REFERENCE_TYPE)
   16121        30539 :                   && (OMP_CLAUSE_MAP_KIND (c)
   16122              :                       != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
   16123              :                 {
   16124          810 :                   pd = &TREE_OPERAND (decl, 0);
   16125          810 :                   decl = TREE_OPERAND (decl, 0);
   16126              :                 }
   16127              : 
   16128        29725 :               if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
   16129         3067 :                 switch (code)
   16130              :                   {
   16131          427 :                   case OACC_ENTER_DATA:
   16132          427 :                   case OACC_EXIT_DATA:
   16133          427 :                     if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
   16134              :                         == ARRAY_TYPE)
   16135              :                       remove = true;
   16136          417 :                     else if (code == OACC_ENTER_DATA)
   16137          263 :                       goto change_to_attach;
   16138              :                     /* Fallthrough.  */
   16139          605 :                   case OMP_TARGET_EXIT_DATA:
   16140          605 :                     OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DETACH);
   16141          605 :                     OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (c) = 0;
   16142          605 :                     break;
   16143           71 :                   case OACC_UPDATE:
   16144              :                     /* An "attach/detach" operation on an update directive
   16145              :                        should behave as a GOMP_MAP_ALWAYS_POINTER.  Note that
   16146              :                        both GOMP_MAP_ATTACH_DETACH and GOMP_MAP_ALWAYS_POINTER
   16147              :                        kinds depend on the previous mapping (for non-TARGET
   16148              :                        regions).  */
   16149           71 :                     OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
   16150           71 :                     break;
   16151         2391 :                   default:
   16152         2391 :                   change_to_attach:
   16153         2391 :                     gcc_assert (!OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (c));
   16154         2391 :                     OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ATTACH);
   16155         2391 :                     if ((ctx->region_type & ORT_TARGET) != 0)
   16156         2113 :                       move_attach = true;
   16157              :                   }
   16158        26658 :               else if ((ctx->region_type & ORT_TARGET) != 0
   16159        26658 :                        && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
   16160        11944 :                            || (OMP_CLAUSE_MAP_KIND (c)
   16161              :                                == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
   16162              :                 move_attach = true;
   16163              : 
   16164              :               /* If we have e.g. map(struct: *var), don't gimplify the
   16165              :                  argument since omp-low.cc wants to see the decl itself.  */
   16166        29725 :               if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
   16167         1136 :                 goto end_adjust_omp_map_clause;
   16168              : 
   16169              :               /* We've already partly gimplified this in
   16170              :                  gimplify_scan_omp_clauses.  Don't do any more.  */
   16171        28589 :               if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
   16172           88 :                 goto end_adjust_omp_map_clause;
   16173              : 
   16174        28501 :               gimplify_omp_ctxp = ctx->outer_context;
   16175        28501 :               if (gimplify_expr (pd, seq_p, NULL, is_gimple_lvalue,
   16176              :                                  fb_lvalue) == GS_ERROR)
   16177            0 :                 remove = true;
   16178        28501 :               gimplify_omp_ctxp = ctx;
   16179        28501 :               goto end_adjust_omp_map_clause;
   16180              :             }
   16181              : 
   16182        27381 :          if ((code == OMP_TARGET
   16183              :               || code == OMP_TARGET_DATA
   16184              :               || code == OMP_TARGET_ENTER_DATA
   16185        14453 :               || code == OMP_TARGET_EXIT_DATA)
   16186        28013 :              && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
   16187              :            {
   16188          242 :              bool firstprivatize = false;
   16189              : 
   16190          247 :              for (struct gimplify_omp_ctx *octx = ctx->outer_context; octx;
   16191            5 :                   octx = octx->outer_context)
   16192              :                {
   16193            6 :                  splay_tree_node n
   16194           12 :                    = splay_tree_lookup (octx->variables,
   16195            6 :                                         (splay_tree_key) OMP_CLAUSE_DECL (c));
   16196              :                  /* If this is contained in an outer OpenMP region as a
   16197              :                     firstprivate value, remove the attach/detach.  */
   16198            6 :                  if (n && (n->value & GOVD_FIRSTPRIVATE))
   16199              :                    {
   16200              :                      firstprivatize = true;
   16201              :                      break;
   16202              :                    }
   16203              :                }
   16204              : 
   16205          242 :              enum gomp_map_kind map_kind;
   16206          242 :              if (firstprivatize)
   16207              :                map_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
   16208          241 :              else if (code == OMP_TARGET_EXIT_DATA)
   16209              :                map_kind = GOMP_MAP_DETACH;
   16210              :              else
   16211          190 :                map_kind = GOMP_MAP_ATTACH;
   16212          242 :              OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
   16213              :            }
   16214        27139 :          else if ((ctx->region_type & ORT_ACC) != 0
   16215        27139 :                   && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
   16216              :            {
   16217            0 :              enum gomp_map_kind map_kind = (code == OACC_EXIT_DATA
   16218            0 :                                             ? GOMP_MAP_DETACH
   16219              :                                             : GOMP_MAP_ATTACH);
   16220            0 :              OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
   16221              :            }
   16222              : 
   16223        27381 :           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   16224        27381 :           if ((ctx->region_type & ORT_TARGET) != 0
   16225        17416 :               && !(n->value & GOVD_SEEN)
   16226         1523 :               && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
   16227        28657 :               && (!is_global_var (decl)
   16228           75 :                   || !lookup_attribute ("omp declare target link",
   16229           75 :                                         DECL_ATTRIBUTES (decl))))
   16230              :             {
   16231         1264 :               remove = true;
   16232              :               /* For struct element mapping, if struct is never referenced
   16233              :                  in target block and none of the mapping has always modifier,
   16234              :                  remove all the struct element mappings, which immediately
   16235              :                  follow the GOMP_MAP_STRUCT map clause.  */
   16236         1264 :               if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
   16237         1264 :                   || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
   16238              :                 {
   16239           30 :                   HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
   16240           80 :                   while (cnt--)
   16241           50 :                     OMP_CLAUSE_CHAIN (c)
   16242           50 :                       = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
   16243              :                 }
   16244              :             }
   16245        26117 :           else if (DECL_SIZE (decl)
   16246        26093 :                    && !poly_int_tree_p (DECL_SIZE (decl))
   16247          199 :                    && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
   16248          199 :                    && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
   16249        26168 :                    && (OMP_CLAUSE_MAP_KIND (c)
   16250              :                        != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
   16251              :             {
   16252              :               /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
   16253              :                  for these, TREE_CODE (DECL_SIZE (decl)) will always be
   16254              :                  INTEGER_CST.  */
   16255           51 :               gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
   16256              : 
   16257           51 :               tree decl2 = DECL_VALUE_EXPR (decl);
   16258           51 :               gcc_assert (INDIRECT_REF_P (decl2));
   16259           51 :               decl2 = TREE_OPERAND (decl2, 0);
   16260           51 :               gcc_assert (DECL_P (decl2));
   16261           51 :               tree mem = build_simple_mem_ref (decl2);
   16262           51 :               OMP_CLAUSE_DECL (c) = mem;
   16263           51 :               OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
   16264           51 :               if (ctx->outer_context)
   16265              :                 {
   16266           15 :                   omp_notice_variable (ctx->outer_context, decl2, true);
   16267           30 :                   omp_notice_variable (ctx->outer_context,
   16268           15 :                                        OMP_CLAUSE_SIZE (c), true);
   16269              :                 }
   16270           51 :               if (((ctx->region_type & ORT_TARGET) != 0
   16271           24 :                    || !ctx->target_firstprivatize_array_bases)
   16272           34 :                   && ((n->value & GOVD_SEEN) == 0
   16273           30 :                       || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
   16274              :                 {
   16275           34 :                   tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   16276              :                                               OMP_CLAUSE_MAP);
   16277           34 :                   OMP_CLAUSE_DECL (nc) = decl;
   16278           34 :                   OMP_CLAUSE_SIZE (nc) = size_zero_node;
   16279           34 :                   if (ctx->target_firstprivatize_array_bases)
   16280           27 :                     OMP_CLAUSE_SET_MAP_KIND (nc,
   16281              :                                              GOMP_MAP_FIRSTPRIVATE_POINTER);
   16282              :                   else
   16283            7 :                     OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
   16284           34 :                   OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
   16285           34 :                   OMP_CLAUSE_CHAIN (c) = nc;
   16286           34 :                   c = nc;
   16287              :                 }
   16288              :             }
   16289              :           else
   16290              :             {
   16291        26066 :               if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
   16292            0 :                 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
   16293        26066 :               gcc_assert ((n->value & GOVD_SEEN) == 0
   16294              :                           || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
   16295              :                               == 0));
   16296              :             }
   16297              : 
   16298              :           /* If we have a target region, we can push all the attaches to the
   16299              :              end of the list (we may have standalone "attach" operations
   16300              :              synthesized for GOMP_MAP_STRUCT nodes that must be processed after
   16301              :              the attachment point AND the pointed-to block have been mapped).
   16302              :              If we have something else, e.g. "enter data", we need to keep
   16303              :              "attach" nodes together with the previous node they attach to so
   16304              :              that separate "exit data" operations work properly (see
   16305              :              libgomp/target.c).  */
   16306        27381 :           if ((ctx->region_type & ORT_TARGET) != 0
   16307        27381 :               && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
   16308        17300 :                   || (OMP_CLAUSE_MAP_KIND (c)
   16309              :                       == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
   16310              :             move_attach = true;
   16311              : 
   16312        57732 : end_adjust_omp_map_clause:
   16313        57732 :           exit_omp_iterator_loop_context (c);
   16314        57732 :           break;
   16315              : 
   16316         9909 :         case OMP_CLAUSE_TO:
   16317         9909 :         case OMP_CLAUSE_FROM:
   16318         9909 :         case OMP_CLAUSE__CACHE_:
   16319         9909 :           decl = OMP_CLAUSE_DECL (c);
   16320         9909 :           if (!DECL_P (decl))
   16321              :             break;
   16322         7729 :           if (DECL_SIZE (decl)
   16323         7729 :               && !poly_int_tree_p (DECL_SIZE (decl)))
   16324              :             {
   16325            4 :               tree decl2 = DECL_VALUE_EXPR (decl);
   16326            4 :               gcc_assert (INDIRECT_REF_P (decl2));
   16327            4 :               decl2 = TREE_OPERAND (decl2, 0);
   16328            4 :               gcc_assert (DECL_P (decl2));
   16329            4 :               tree mem = build_simple_mem_ref (decl2);
   16330            4 :               OMP_CLAUSE_DECL (c) = mem;
   16331            4 :               OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
   16332            4 :               if (ctx->outer_context)
   16333              :                 {
   16334            4 :                   omp_notice_variable (ctx->outer_context, decl2, true);
   16335            4 :                   omp_notice_variable (ctx->outer_context,
   16336            4 :                                        OMP_CLAUSE_SIZE (c), true);
   16337              :                 }
   16338              :             }
   16339         7725 :           else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
   16340            0 :             OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
   16341              :           break;
   16342              : 
   16343        16073 :         case OMP_CLAUSE_REDUCTION:
   16344        16073 :           if (OMP_CLAUSE_REDUCTION_INSCAN (c))
   16345              :             {
   16346          835 :               decl = OMP_CLAUSE_DECL (c);
   16347          835 :               n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   16348          835 :               if ((n->value & GOVD_REDUCTION_INSCAN) == 0)
   16349              :                 {
   16350           69 :                   remove = true;
   16351           69 :                   error_at (OMP_CLAUSE_LOCATION (c),
   16352              :                             "%qD specified in %<inscan%> %<reduction%> clause "
   16353              :                             "but not in %<scan%> directive clause", decl);
   16354           69 :                   break;
   16355              :                 }
   16356              :               has_inscan_reductions = true;
   16357              :             }
   16358              :           /* FALLTHRU */
   16359        18633 :         case OMP_CLAUSE_IN_REDUCTION:
   16360        18633 :         case OMP_CLAUSE_TASK_REDUCTION:
   16361        18633 :           decl = OMP_CLAUSE_DECL (c);
   16362              :           /* OpenACC reductions need a present_or_copy data clause.
   16363              :              Add one if necessary.  Emit error when the reduction is private.  */
   16364        18633 :           if (ctx->region_type == ORT_ACC_PARALLEL
   16365        18101 :               || ctx->region_type == ORT_ACC_SERIAL)
   16366              :             {
   16367          803 :               n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   16368          803 :               if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
   16369              :                 {
   16370           13 :                   remove = true;
   16371           13 :                   error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
   16372           13 :                             "reduction on %qE", DECL_NAME (decl));
   16373              :                 }
   16374          790 :               else if ((n->value & GOVD_MAP) == 0)
   16375              :                 {
   16376          524 :                   tree next = OMP_CLAUSE_CHAIN (c);
   16377          524 :                   tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
   16378          524 :                   OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
   16379          524 :                   OMP_CLAUSE_DECL (nc) = decl;
   16380          524 :                   OMP_CLAUSE_CHAIN (c) = nc;
   16381          524 :                   lang_hooks.decls.omp_finish_clause (nc, pre_p,
   16382          524 :                                                       (ctx->region_type
   16383              :                                                        & ORT_ACC) != 0);
   16384          550 :                   while (1)
   16385              :                     {
   16386          537 :                       OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
   16387          537 :                       if (OMP_CLAUSE_CHAIN (nc) == NULL)
   16388              :                         break;
   16389           13 :                       nc = OMP_CLAUSE_CHAIN (nc);
   16390              :                     }
   16391          524 :                   OMP_CLAUSE_CHAIN (nc) = next;
   16392          524 :                   n->value |= GOVD_MAP;
   16393              :                 }
   16394              :             }
   16395        18633 :           if (DECL_P (decl)
   16396        18633 :               && omp_shared_to_firstprivate_optimizable_decl_p (decl))
   16397         8048 :             omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
   16398              :           break;
   16399              : 
   16400         3682 :         case OMP_CLAUSE_ALLOCATE:
   16401         3682 :           decl = OMP_CLAUSE_DECL (c);
   16402         3682 :           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   16403         3682 :           if (n != NULL && !(n->value & GOVD_SEEN))
   16404              :             {
   16405         1180 :               if ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE | GOVD_LINEAR))
   16406              :                   != 0
   16407         1180 :                   && (n->value & (GOVD_REDUCTION | GOVD_LASTPRIVATE)) == 0)
   16408              :                 remove = true;
   16409              :             }
   16410              :           if (!remove
   16411         2502 :               && OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
   16412         1286 :               && TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)) != INTEGER_CST
   16413          492 :               && ((ctx->region_type & (ORT_PARALLEL | ORT_TARGET)) != 0
   16414          347 :                   || (ctx->region_type & ORT_TASKLOOP) == ORT_TASK
   16415          287 :                   || (ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS))
   16416              :             {
   16417          223 :               tree allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
   16418          223 :               n = splay_tree_lookup (ctx->variables, (splay_tree_key) allocator);
   16419          223 :               if (n == NULL)
   16420              :                 {
   16421           92 :                   enum omp_clause_default_kind default_kind
   16422              :                     = ctx->default_kind;
   16423           92 :                   ctx->default_kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
   16424           92 :                   omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
   16425              :                                        true);
   16426           92 :                   ctx->default_kind = default_kind;
   16427              :                 }
   16428              :               else
   16429          131 :                 omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
   16430              :                                      true);
   16431              :             }
   16432              :           break;
   16433              : 
   16434              :         case OMP_CLAUSE_COPYIN:
   16435              :         case OMP_CLAUSE_COPYPRIVATE:
   16436              :         case OMP_CLAUSE_IF:
   16437              :         case OMP_CLAUSE_SELF:
   16438              :         case OMP_CLAUSE_NUM_THREADS:
   16439              :         case OMP_CLAUSE_NUM_TEAMS:
   16440              :         case OMP_CLAUSE_THREAD_LIMIT:
   16441              :         case OMP_CLAUSE_DIST_SCHEDULE:
   16442              :         case OMP_CLAUSE_DEVICE:
   16443              :         case OMP_CLAUSE_SCHEDULE:
   16444              :         case OMP_CLAUSE_NOWAIT:
   16445              :         case OMP_CLAUSE_ORDERED:
   16446              :         case OMP_CLAUSE_DEFAULT:
   16447              :         case OMP_CLAUSE_UNTIED:
   16448              :         case OMP_CLAUSE_COLLAPSE:
   16449              :         case OMP_CLAUSE_FINAL:
   16450              :         case OMP_CLAUSE_MERGEABLE:
   16451              :         case OMP_CLAUSE_PROC_BIND:
   16452              :         case OMP_CLAUSE_SAFELEN:
   16453              :         case OMP_CLAUSE_SIMDLEN:
   16454              :         case OMP_CLAUSE_DEPEND:
   16455              :         case OMP_CLAUSE_DOACROSS:
   16456              :         case OMP_CLAUSE_PRIORITY:
   16457              :         case OMP_CLAUSE_GRAINSIZE:
   16458              :         case OMP_CLAUSE_NUM_TASKS:
   16459              :         case OMP_CLAUSE_NOGROUP:
   16460              :         case OMP_CLAUSE_THREADS:
   16461              :         case OMP_CLAUSE_SIMD:
   16462              :         case OMP_CLAUSE_FILTER:
   16463              :         case OMP_CLAUSE_HINT:
   16464              :         case OMP_CLAUSE_DEFAULTMAP:
   16465              :         case OMP_CLAUSE_ORDER:
   16466              :         case OMP_CLAUSE_BIND:
   16467              :         case OMP_CLAUSE_DETACH:
   16468              :         case OMP_CLAUSE_USE_DEVICE_PTR:
   16469              :         case OMP_CLAUSE_USE_DEVICE_ADDR:
   16470              :         case OMP_CLAUSE_ASYNC:
   16471              :         case OMP_CLAUSE_WAIT:
   16472              :         case OMP_CLAUSE_INDEPENDENT:
   16473              :         case OMP_CLAUSE_NUM_GANGS:
   16474              :         case OMP_CLAUSE_NUM_WORKERS:
   16475              :         case OMP_CLAUSE_VECTOR_LENGTH:
   16476              :         case OMP_CLAUSE_GANG:
   16477              :         case OMP_CLAUSE_WORKER:
   16478              :         case OMP_CLAUSE_VECTOR:
   16479              :         case OMP_CLAUSE_AUTO:
   16480              :         case OMP_CLAUSE_SEQ:
   16481              :         case OMP_CLAUSE_TILE:
   16482              :         case OMP_CLAUSE_IF_PRESENT:
   16483              :         case OMP_CLAUSE_FINALIZE:
   16484              :         case OMP_CLAUSE_INCLUSIVE:
   16485              :         case OMP_CLAUSE_EXCLUSIVE:
   16486              :         case OMP_CLAUSE_USES_ALLOCATORS:
   16487              :         case OMP_CLAUSE_DEVICE_TYPE:
   16488              :         case OMP_CLAUSE_MESSAGE:
   16489              :           break;
   16490              : 
   16491            0 :         case OMP_CLAUSE_NOHOST:
   16492            0 :         default:
   16493            0 :           gcc_unreachable ();
   16494              :         }
   16495              : 
   16496       133977 :       if (remove)
   16497         9966 :         *list_p = OMP_CLAUSE_CHAIN (c);
   16498       252220 :       else if (move_attach)
   16499              :         {
   16500              :           /* Remove attach node from here, separate out into its own list.  */
   16501         2360 :           *attach_tail = c;
   16502         2360 :           *list_p = OMP_CLAUSE_CHAIN (c);
   16503         2360 :           OMP_CLAUSE_CHAIN (c) = NULL_TREE;
   16504         2360 :           attach_tail = &OMP_CLAUSE_CHAIN (c);
   16505              :         }
   16506              :       else
   16507       249860 :         list_p = &OMP_CLAUSE_CHAIN (c);
   16508              :     }
   16509              : 
   16510              :   /* Splice attach nodes at the end of the list.  */
   16511       135502 :   if (attach_list)
   16512              :     {
   16513         1127 :       *list_p = attach_list;
   16514         1127 :       list_p = attach_tail;
   16515              :     }
   16516              : 
   16517              :   /* Add in any implicit data sharing.  */
   16518       135502 :   struct gimplify_adjust_omp_clauses_data data;
   16519       135502 :   if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
   16520              :     {
   16521              :       /* OpenMP.  Implicit clauses are added at the start of the clause list,
   16522              :          but after any non-map clauses.  */
   16523              :       tree *implicit_add_list_p = orig_list_p;
   16524       270394 :       while (*implicit_add_list_p
   16525       270394 :              && OMP_CLAUSE_CODE (*implicit_add_list_p) != OMP_CLAUSE_MAP)
   16526       164791 :         implicit_add_list_p = &OMP_CLAUSE_CHAIN (*implicit_add_list_p);
   16527       105603 :       data.list_p = implicit_add_list_p;
   16528              :     }
   16529              :   else
   16530              :     /* OpenACC.  */
   16531        29899 :     data.list_p = list_p;
   16532       135502 :   data.pre_p = pre_p;
   16533       135502 :   splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
   16534              : 
   16535       135502 :   if (has_inscan_reductions)
   16536         2525 :     for (c = *orig_list_p; c; c = OMP_CLAUSE_CHAIN (c))
   16537         1892 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
   16538         1892 :           && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
   16539              :         {
   16540            5 :           error_at (OMP_CLAUSE_LOCATION (c),
   16541              :                     "%<inscan%> %<reduction%> clause used together with "
   16542              :                     "%<linear%> clause for a variable other than loop "
   16543              :                     "iterator");
   16544            5 :           break;
   16545              :         }
   16546              : 
   16547       135502 :   gimplify_omp_ctxp = ctx->outer_context;
   16548       135502 :   delete_omp_context (ctx);
   16549       135502 : }
   16550              : 
   16551              : /* Try to evaluate a novariants clause. Return 1 if true, 0 if false or absent,
   16552              :  * -1 if run-time evaluation is needed. */
   16553              : 
   16554              : int
   16555       263081 : omp_has_novariants (void)
   16556              : {
   16557       263081 :   struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   16558       263081 :   if (ctx != NULL && ctx->code == OMP_DISPATCH && !ctx->in_call_args)
   16559              :     {
   16560         3290 :       tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOVARIANTS);
   16561         3290 :       if (c != NULL_TREE)
   16562              :         {
   16563          299 :           if (integer_nonzerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
   16564              :             return 1;
   16565          171 :           else if (integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
   16566              :             return 0;
   16567              :           else
   16568           38 :             return -1;
   16569              :         }
   16570              :       return 0;
   16571              :     }
   16572              :   return 0;
   16573              : }
   16574              : 
   16575              : /* Try to evaluate a nocontext clause. Return 1 if true, 0 if false or absent,
   16576              :  * -1 if run-time evaluation is needed. */
   16577              : 
   16578              : static int
   16579         1903 : omp_has_nocontext (void)
   16580              : {
   16581         1903 :   struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   16582         1903 :   if (ctx != NULL && ctx->code == OMP_DISPATCH)
   16583              :     {
   16584         1903 :       tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOCONTEXT);
   16585         1903 :       if (c != NULL_TREE)
   16586              :         {
   16587          256 :           if (integer_nonzerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
   16588              :             return 1;
   16589           94 :           else if (integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
   16590              :             return 0;
   16591              :           else
   16592           44 :             return -1;
   16593              :         }
   16594              :       return 0;
   16595              :     }
   16596              :   return 0;
   16597              : }
   16598              : 
   16599              : /* Collect a list of traits for enclosing constructs in the current
   16600              :    OpenMP context.  The list is in the same format as the trait selector
   16601              :    list of construct trait sets built by the front ends.
   16602              : 
   16603              :    Per the OpenMP specification, the construct trait set includes constructs
   16604              :    up to an enclosing "target" construct.  If there is no "target" construct,
   16605              :    then additional things may be added to the construct trait set (simd for
   16606              :    simd clones, additional constructs associated with "declare variant",
   16607              :    the target trait for "declare target"); those are not handled here.
   16608              :    In particular simd clones are not known during gimplification so
   16609              :    matching/scoring of context selectors that might involve them needs
   16610              :    to be deferred to the omp_device_lower pass.  */
   16611              : 
   16612              : tree
   16613         2248 : omp_get_construct_context (void)
   16614              : {
   16615         2248 :   tree result = NULL_TREE;
   16616         4247 :   for (struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp; ctx;)
   16617              :     {
   16618         2121 :       if (((ctx->region_type & (ORT_TARGET | ORT_IMPLICIT_TARGET | ORT_ACC))
   16619              :                 == ORT_TARGET)
   16620          122 :                && ctx->code == OMP_TARGET)
   16621              :         {
   16622          122 :           result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TARGET,
   16623              :                                         NULL_TREE, NULL_TREE, result);
   16624              :           /* We're not interested in any outer constructs.  */
   16625          122 :           break;
   16626              :         }
   16627         1999 :       else if ((ctx->region_type & ORT_PARALLEL) && ctx->code == OMP_PARALLEL)
   16628          216 :         result = make_trait_selector (OMP_TRAIT_CONSTRUCT_PARALLEL,
   16629              :                                       NULL_TREE, NULL_TREE, result);
   16630         1783 :       else if ((ctx->region_type & ORT_TEAMS) && ctx->code == OMP_TEAMS)
   16631           75 :         result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TEAMS,
   16632              :                                       NULL_TREE, NULL_TREE, result);
   16633         1708 :       else if (ctx->region_type == ORT_WORKSHARE && ctx->code == OMP_FOR)
   16634          164 :         result = make_trait_selector (OMP_TRAIT_CONSTRUCT_FOR,
   16635              :                                       NULL_TREE, NULL_TREE, result);
   16636         1544 :       else if (ctx->code == OMP_DISPATCH && omp_has_nocontext () != 1)
   16637         1279 :         result = make_trait_selector (OMP_TRAIT_CONSTRUCT_DISPATCH,
   16638              :                                       NULL_TREE, NULL_TREE, result);
   16639          265 :       else if (ctx->region_type == ORT_SIMD
   16640           58 :                && ctx->code == OMP_SIMD
   16641          323 :                && !omp_find_clause (ctx->clauses, OMP_CLAUSE_BIND))
   16642              :         {
   16643           58 :           tree props = NULL_TREE;
   16644           58 :           tree *last = &props;
   16645          120 :           for (tree c = ctx->clauses; c; c = OMP_CLAUSE_CHAIN (c))
   16646           62 :             if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SIMDLEN
   16647           42 :                 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INBRANCH
   16648          104 :                 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOTINBRANCH)
   16649              :               {
   16650           20 :                 *last = unshare_expr (c);
   16651           20 :                 last = &(OMP_CLAUSE_CHAIN (c));
   16652              :               }
   16653           58 :           result = make_trait_selector (OMP_TRAIT_CONSTRUCT_SIMD,
   16654              :                                         NULL_TREE, props, result);
   16655              :         }
   16656          207 :       else if (ctx->region_type == ORT_WORKSHARE
   16657            5 :                && ctx->code == OMP_LOOP
   16658            0 :                && ctx->outer_context
   16659            0 :                && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL
   16660            0 :                && ctx->outer_context->outer_context
   16661            0 :                && ctx->outer_context->outer_context->code == OMP_LOOP
   16662            0 :                && ctx->outer_context->outer_context->distribute)
   16663         1999 :         ctx = ctx->outer_context->outer_context;
   16664         1999 :       ctx = ctx->outer_context;
   16665              :     }
   16666              : 
   16667         2248 :   return result;
   16668              : }
   16669              : 
   16670              : /* Gimplify OACC_CACHE.  */
   16671              : 
   16672              : static void
   16673          717 : gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
   16674              : {
   16675          717 :   tree expr = *expr_p;
   16676              : 
   16677          717 :   gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
   16678              :                              OACC_CACHE);
   16679          717 :   gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
   16680              :                                OACC_CACHE);
   16681              : 
   16682              :   /* TODO: Do something sensible with this information.  */
   16683              : 
   16684          717 :   *expr_p = NULL_TREE;
   16685          717 : }
   16686              : 
   16687              : /* Helper function of gimplify_oacc_declare.  The helper's purpose is to,
   16688              :    if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
   16689              :    kind.  The entry kind will replace the one in CLAUSE, while the exit
   16690              :    kind will be used in a new omp_clause and returned to the caller.  */
   16691              : 
   16692              : static tree
   16693          190 : gimplify_oacc_declare_1 (tree clause)
   16694              : {
   16695          190 :   HOST_WIDE_INT kind, new_op;
   16696          190 :   bool ret = false;
   16697          190 :   tree c = NULL;
   16698              : 
   16699          190 :   kind = OMP_CLAUSE_MAP_KIND (clause);
   16700              : 
   16701          190 :   switch (kind)
   16702              :     {
   16703              :       case GOMP_MAP_ALLOC:
   16704              :         new_op = GOMP_MAP_RELEASE;
   16705              :         ret = true;
   16706              :         break;
   16707              : 
   16708           29 :       case GOMP_MAP_FROM:
   16709           29 :         OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
   16710           29 :         new_op = GOMP_MAP_FROM;
   16711           29 :         ret = true;
   16712           29 :         break;
   16713              : 
   16714           40 :       case GOMP_MAP_TOFROM:
   16715           40 :         OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
   16716           40 :         new_op = GOMP_MAP_FROM;
   16717           40 :         ret = true;
   16718           40 :         break;
   16719              : 
   16720              :       case GOMP_MAP_DEVICE_RESIDENT:
   16721              :       case GOMP_MAP_FORCE_DEVICEPTR:
   16722              :       case GOMP_MAP_FORCE_PRESENT:
   16723              :       case GOMP_MAP_LINK:
   16724              :       case GOMP_MAP_POINTER:
   16725              :       case GOMP_MAP_TO:
   16726              :         break;
   16727              : 
   16728            0 :       default:
   16729            0 :         gcc_unreachable ();
   16730           69 :         break;
   16731              :     }
   16732              : 
   16733           69 :   if (ret)
   16734              :     {
   16735          116 :       c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
   16736          116 :       OMP_CLAUSE_SET_MAP_KIND (c, new_op);
   16737          116 :       OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
   16738              :     }
   16739              : 
   16740          190 :   return c;
   16741              : }
   16742              : 
   16743              : /* Gimplify OACC_DECLARE.  */
   16744              : 
   16745              : static void
   16746          254 : gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
   16747              : {
   16748          254 :   tree expr = *expr_p;
   16749          254 :   gomp_target *stmt;
   16750          254 :   tree clauses, t, decl;
   16751              : 
   16752          254 :   clauses = OACC_DECLARE_CLAUSES (expr);
   16753              : 
   16754          254 :   gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
   16755          254 :   gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
   16756              : 
   16757          516 :   for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
   16758              :     {
   16759          262 :       decl = OMP_CLAUSE_DECL (t);
   16760              : 
   16761          262 :       if (TREE_CODE (decl) == MEM_REF)
   16762            8 :         decl = TREE_OPERAND (decl, 0);
   16763              : 
   16764          262 :       if (VAR_P (decl) && !is_oacc_declared (decl))
   16765              :         {
   16766          262 :           tree attr = get_identifier ("oacc declare target");
   16767          262 :           DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
   16768          262 :                                               DECL_ATTRIBUTES (decl));
   16769              :         }
   16770              : 
   16771          262 :       if (VAR_P (decl)
   16772          262 :           && !is_global_var (decl)
   16773          452 :           && DECL_CONTEXT (decl) == current_function_decl)
   16774              :         {
   16775          190 :           tree c = gimplify_oacc_declare_1 (t);
   16776          190 :           if (c)
   16777              :             {
   16778          116 :               if (oacc_declare_returns == NULL)
   16779           40 :                 oacc_declare_returns = new hash_map<tree, tree>;
   16780              : 
   16781          116 :               oacc_declare_returns->put (decl, c);
   16782              :             }
   16783              :         }
   16784              : 
   16785          262 :       if (gimplify_omp_ctxp)
   16786           72 :         omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
   16787              :     }
   16788              : 
   16789          254 :   stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
   16790              :                                   clauses);
   16791              : 
   16792          254 :   gimplify_seq_add_stmt (pre_p, stmt);
   16793              : 
   16794          254 :   *expr_p = NULL_TREE;
   16795          254 : }
   16796              : 
   16797              : /* Gimplify the contents of an OMP_PARALLEL statement.  This involves
   16798              :    gimplification of the body, as well as scanning the body for used
   16799              :    variables.  We need to do this scan now, because variable-sized
   16800              :    decls will be decomposed during gimplification.  */
   16801              : 
   16802              : static void
   16803        19176 : gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
   16804              : {
   16805        19176 :   tree expr = *expr_p;
   16806        19176 :   gimple *g;
   16807        19176 :   gimple_seq body = NULL;
   16808              : 
   16809        38352 :   gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
   16810        19176 :                              OMP_PARALLEL_COMBINED (expr)
   16811              :                              ? ORT_COMBINED_PARALLEL
   16812              :                              : ORT_PARALLEL, OMP_PARALLEL);
   16813              : 
   16814        19176 :   push_gimplify_context ();
   16815              : 
   16816        19176 :   g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
   16817        19176 :   if (gimple_code (g) == GIMPLE_BIND)
   16818        19176 :     pop_gimplify_context (g);
   16819              :   else
   16820            0 :     pop_gimplify_context (NULL);
   16821              : 
   16822        19176 :   gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
   16823              :                                OMP_PARALLEL);
   16824              : 
   16825        38352 :   g = gimple_build_omp_parallel (body,
   16826        19176 :                                  OMP_PARALLEL_CLAUSES (expr),
   16827              :                                  NULL_TREE, NULL_TREE);
   16828        19176 :   if (OMP_PARALLEL_COMBINED (expr))
   16829        13292 :     gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
   16830        19176 :   gimplify_seq_add_stmt (pre_p, g);
   16831        19176 :   *expr_p = NULL_TREE;
   16832        19176 : }
   16833              : 
   16834              : /* Gimplify the contents of an OMP_TASK statement.  This involves
   16835              :    gimplification of the body, as well as scanning the body for used
   16836              :    variables.  We need to do this scan now, because variable-sized
   16837              :    decls will be decomposed during gimplification.  */
   16838              : 
   16839              : static void
   16840         4133 : gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
   16841              : {
   16842         4133 :   tree expr = *expr_p;
   16843         4133 :   gimple *g;
   16844         4133 :   gimple_seq body = NULL;
   16845         4133 :   bool nowait = false;
   16846         4133 :   bool has_depend = false;
   16847              : 
   16848         4133 :   if (OMP_TASK_BODY (expr) == NULL_TREE)
   16849              :     {
   16850          242 :       for (tree c = OMP_TASK_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
   16851          153 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
   16852              :           {
   16853          111 :             has_depend = true;
   16854          111 :             if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_MUTEXINOUTSET)
   16855              :               {
   16856            9 :                 error_at (OMP_CLAUSE_LOCATION (c),
   16857              :                           "%<mutexinoutset%> kind in %<depend%> clause on a "
   16858              :                           "%<taskwait%> construct");
   16859            9 :                 break;
   16860              :               }
   16861              :           }
   16862           42 :         else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT)
   16863           42 :           nowait = true;
   16864           98 :       if (nowait && !has_depend)
   16865              :         {
   16866            5 :           error_at (EXPR_LOCATION (expr),
   16867              :                     "%<taskwait%> construct with %<nowait%> clause but no "
   16868              :                     "%<depend%> clauses");
   16869            5 :           *expr_p = NULL_TREE;
   16870            5 :           return;
   16871              :         }
   16872              :     }
   16873              : 
   16874        12384 :   gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
   16875         4128 :                              omp_find_clause (OMP_TASK_CLAUSES (expr),
   16876              :                                               OMP_CLAUSE_UNTIED)
   16877              :                              ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
   16878              : 
   16879         4128 :   if (OMP_TASK_BODY (expr))
   16880              :     {
   16881         4035 :       push_gimplify_context ();
   16882              : 
   16883         4035 :       g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
   16884         4035 :       if (gimple_code (g) == GIMPLE_BIND)
   16885         4035 :         pop_gimplify_context (g);
   16886              :       else
   16887            0 :         pop_gimplify_context (NULL);
   16888              :     }
   16889              : 
   16890         4128 :   gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
   16891              :                                OMP_TASK);
   16892              : 
   16893         8256 :   g = gimple_build_omp_task (body,
   16894         4128 :                              OMP_TASK_CLAUSES (expr),
   16895              :                              NULL_TREE, NULL_TREE,
   16896              :                              NULL_TREE, NULL_TREE, NULL_TREE);
   16897         4128 :   if (OMP_TASK_BODY (expr) == NULL_TREE)
   16898           93 :     gimple_omp_task_set_taskwait_p (g, true);
   16899         4128 :   gimplify_seq_add_stmt (pre_p, g);
   16900         4128 :   *expr_p = NULL_TREE;
   16901              : }
   16902              : 
   16903              : /* Helper function for gimplify_omp_for.  If *TP is not a gimple constant,
   16904              :    force it into a temporary initialized in PRE_P and add firstprivate clause
   16905              :    to ORIG_FOR_STMT.  */
   16906              : 
   16907              : static void
   16908         4402 : gimplify_omp_taskloop_expr (tree type, tree *tp, gimple_seq *pre_p,
   16909              :                             tree orig_for_stmt)
   16910              : {
   16911         4402 :   if (*tp == NULL || is_gimple_constant (*tp))
   16912              :     return;
   16913              : 
   16914          780 :   if (TREE_CODE (*tp) == SAVE_EXPR)
   16915          135 :     gimplify_save_expr (tp, pre_p, NULL);
   16916              :   else
   16917          645 :     *tp = get_initialized_tmp_var (*tp, pre_p, NULL, false);
   16918              :   /* Reference to pointer conversion is considered useless,
   16919              :      but is significant for firstprivate clause.  Force it
   16920              :      here.  */
   16921          780 :   if (type
   16922          688 :       && TREE_CODE (type) == POINTER_TYPE
   16923          870 :       && TREE_CODE (TREE_TYPE (*tp)) == REFERENCE_TYPE)
   16924              :     {
   16925            2 :       tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
   16926            2 :       tree m = build2 (INIT_EXPR, TREE_TYPE (v), v, *tp);
   16927            2 :       gimplify_and_add (m, pre_p);
   16928            2 :       *tp = v;
   16929              :     }
   16930              : 
   16931          780 :   tree c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
   16932          780 :   OMP_CLAUSE_DECL (c) = *tp;
   16933          780 :   OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
   16934          780 :   OMP_FOR_CLAUSES (orig_for_stmt) = c;
   16935              : }
   16936              : 
   16937              : /* Helper function of gimplify_omp_for, find OMP_ORDERED with
   16938              :    null OMP_ORDERED_BODY inside of OMP_FOR's body.  */
   16939              : 
   16940              : static tree
   16941        16496 : find_standalone_omp_ordered (tree *tp, int *walk_subtrees, void *)
   16942              : {
   16943        16496 :   switch (TREE_CODE (*tp))
   16944              :     {
   16945          901 :     case OMP_ORDERED:
   16946          901 :       if (OMP_ORDERED_BODY (*tp) == NULL_TREE)
   16947          531 :         return *tp;
   16948              :       break;
   16949           23 :     case OMP_SIMD:
   16950           23 :     case OMP_PARALLEL:
   16951           23 :     case OMP_TARGET:
   16952           23 :       *walk_subtrees = 0;
   16953           23 :       break;
   16954              :     default:
   16955              :       break;
   16956              :     }
   16957              :   return NULL_TREE;
   16958              : }
   16959              : 
   16960              : /* Gimplify standalone loop transforming directive which has the
   16961              :    transformations applied already.  So, all that is needed is gimplify
   16962              :    the remaining loops as normal loops.  */
   16963              : 
   16964              : static enum gimplify_status
   16965         2269 : gimplify_omp_loop_xform (tree *expr_p, gimple_seq *pre_p)
   16966              : {
   16967         2269 :   tree for_stmt = *expr_p;
   16968              : 
   16969         2269 :   if (OMP_FOR_PRE_BODY (for_stmt))
   16970          761 :     gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), pre_p);
   16971              : 
   16972         2269 :   gimple_seq pre_body = NULL, post_body = NULL;
   16973         5213 :   for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
   16974              :     {
   16975         2944 :       if (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i) == NULL_TREE)
   16976         1708 :         continue;
   16977         1287 :       tree iters = NULL_TREE;
   16978         1287 :       if (i == 0
   16979          830 :           && TREE_CODE (for_stmt) == OMP_UNROLL
   16980         1778 :           && !omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_PARTIAL))
   16981              :         {
   16982          313 :           if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_FULL))
   16983          270 :             iters = omp_loop_number_of_iterations (for_stmt, 0, NULL);
   16984              :           else
   16985           43 :             iters = build_int_cst (integer_type_node, 8);
   16986              :         }
   16987         1287 :       tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
   16988         1287 :       gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
   16989         1287 :       tree decl = TREE_OPERAND (t, 0);
   16990         1287 :       gcc_assert (DECL_P (decl));
   16991         1287 :       gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
   16992              :                   || POINTER_TYPE_P (TREE_TYPE (decl)));
   16993         1287 :       if (DECL_ARTIFICIAL (decl)
   16994          878 :           && TREE_PRIVATE (t)
   16995          869 :           && gimplify_omp_ctxp
   16996         1617 :           && gimplify_omp_ctxp->region_type != ORT_NONE)
   16997              :         {
   16998              :           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   16999          504 :           do
   17000              :             {
   17001          504 :               splay_tree_node n
   17002          504 :                 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
   17003          504 :               if (n != NULL)
   17004              :                 break;
   17005          346 :               else if (ctx->region_type != ORT_WORKSHARE
   17006              :                        && ctx->region_type != ORT_TASKGROUP
   17007           62 :                        && ctx->region_type != ORT_SIMD
   17008           38 :                        && ctx->region_type != ORT_ACC
   17009           38 :                        && !(ctx->region_type & ORT_TARGET_DATA))
   17010              :                 {
   17011           38 :                   omp_add_variable (ctx, decl, GOVD_PRIVATE);
   17012           38 :                   break;
   17013              :                 }
   17014          308 :               ctx = ctx->outer_context;
   17015              :             }
   17016          308 :           while (ctx);
   17017              :         }
   17018         1287 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
   17019              :         {
   17020           23 :           gcc_assert (seen_error ());
   17021           23 :           continue;
   17022              :         }
   17023         1264 :       gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
   17024              :                      fb_rvalue);
   17025         1264 :       gimplify_and_add (t, &pre_body);
   17026         1264 :       t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
   17027         1264 :       gcc_assert (TREE_OPERAND (t, 0) == decl);
   17028         1264 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
   17029              :         {
   17030           28 :           gcc_assert (seen_error ());
   17031           28 :           continue;
   17032              :         }
   17033         1236 :       gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
   17034              :                      fb_rvalue);
   17035         1236 :       tree l1 = create_artificial_label (UNKNOWN_LOCATION);
   17036         1236 :       tree l2 = create_artificial_label (UNKNOWN_LOCATION);
   17037         1236 :       tree l3 = create_artificial_label (UNKNOWN_LOCATION);
   17038         1236 :       gimplify_seq_add_stmt (&pre_body, gimple_build_goto (l2));
   17039         1236 :       gimplify_seq_add_stmt (&pre_body, gimple_build_label (l1));
   17040         1236 :       gimple_seq this_post_body = NULL;
   17041         1236 :       t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
   17042         1236 :       if (TREE_CODE (t) == MODIFY_EXPR)
   17043              :         {
   17044          979 :           t = TREE_OPERAND (t, 1);
   17045          979 :           if (TREE_CODE (t) == PLUS_EXPR
   17046          979 :               && TREE_OPERAND (t, 1) == decl)
   17047              :             {
   17048            0 :               TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
   17049            0 :               TREE_OPERAND (t, 0) = decl;
   17050              :             }
   17051          979 :           gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
   17052              :                          fb_rvalue);
   17053              :         }
   17054         1236 :       gimplify_and_add (TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i),
   17055              :                         &this_post_body);
   17056         1236 :       gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l2));
   17057         1236 :       t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
   17058         1236 :       gcond *cond = NULL;
   17059         1236 :       tree d = decl;
   17060         1236 :       gimplify_expr (&d, &this_post_body, NULL, is_gimple_val, fb_rvalue);
   17061         1236 :       if (iters && tree_fits_uhwi_p (iters))
   17062              :         {
   17063          301 :           unsigned HOST_WIDE_INT niters = tree_to_uhwi (iters);
   17064          301 :           if ((unsigned HOST_WIDE_INT) (int) niters == niters
   17065          301 :               && (int) niters > 0)
   17066              :             {
   17067          301 :               t = build2 (TREE_CODE (t), boolean_type_node, d,
   17068          301 :                           TREE_OPERAND (t, 1));
   17069          301 :               t = build3 (ANNOTATE_EXPR, TREE_TYPE (t), t,
   17070              :                           build_int_cst (integer_type_node,
   17071              :                                          annot_expr_unroll_kind),
   17072          301 :                           build_int_cst (integer_type_node, niters));
   17073          301 :               gimplify_expr (&t, &this_post_body, NULL, is_gimple_val,
   17074              :                              fb_rvalue);
   17075          301 :               cond = gimple_build_cond (NE_EXPR, t, boolean_false_node,
   17076              :                                         l1, l3);
   17077              :             }
   17078              :         }
   17079          301 :       if (cond == NULL)
   17080          935 :         cond = gimple_build_cond (TREE_CODE (t), d, TREE_OPERAND (t, 1),
   17081              :                                   l1, l3);
   17082         1236 :       gimplify_seq_add_stmt (&this_post_body, cond);
   17083         1236 :       gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l3));
   17084         1236 :       gimplify_seq_add_seq (&this_post_body, post_body);
   17085         1236 :       post_body = this_post_body;
   17086              :     }
   17087         2269 :   gimplify_seq_add_seq (pre_p, pre_body);
   17088         2269 :   gimplify_and_add (OMP_FOR_BODY (for_stmt), pre_p);
   17089         2269 :   gimplify_seq_add_seq (pre_p, post_body);
   17090              : 
   17091         2269 :   *expr_p = NULL_TREE;
   17092         2269 :   return GS_ALL_DONE;
   17093              : }
   17094              : 
   17095              : /* Gimplify the gross structure of an OMP_FOR statement.  */
   17096              : 
   17097              : static enum gimplify_status
   17098        61296 : gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
   17099              : {
   17100        61296 :   tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
   17101        61296 :   enum gimplify_status ret = GS_ALL_DONE;
   17102        61296 :   enum gimplify_status tret;
   17103        61296 :   gomp_for *gfor;
   17104        61296 :   gimple_seq for_body, for_pre_body;
   17105        61296 :   int i;
   17106        61296 :   bitmap has_decl_expr = NULL;
   17107        61296 :   enum omp_region_type ort = ORT_WORKSHARE;
   17108        61296 :   bool openacc = TREE_CODE (*expr_p) == OACC_LOOP;
   17109              : 
   17110        61296 :   orig_for_stmt = for_stmt = *expr_p;
   17111              : 
   17112        61296 :   bool loop_p = (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_BIND)
   17113        61296 :                  != NULL_TREE);
   17114        61367 :   while (OMP_FOR_INIT (for_stmt) == NULL_TREE)
   17115              :     {
   17116        19635 :       tree *data[4] = { NULL, NULL, NULL, NULL };
   17117        19635 :       gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
   17118        19635 :       inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
   17119              :                                   find_combined_omp_for, data, NULL);
   17120        19635 :       if (inner_for_stmt == NULL_TREE)
   17121              :         {
   17122           36 :           gcc_assert (seen_error ());
   17123           36 :           *expr_p = NULL_TREE;
   17124         4244 :           return GS_ERROR;
   17125              :         }
   17126        19599 :       gcc_assert (inner_for_stmt == *data[3]);
   17127        19599 :       omp_maybe_apply_loop_xforms (data[3],
   17128        19599 :                                    data[2]
   17129         5394 :                                    ? OMP_FOR_CLAUSES (*data[2])
   17130        14205 :                                    : TREE_CODE (for_stmt) == OMP_FOR
   17131        14205 :                                    ? OMP_FOR_CLAUSES (for_stmt)
   17132              :                                    : NULL_TREE);
   17133        19599 :       if (inner_for_stmt != *data[3])
   17134           71 :         continue;
   17135        19528 :       if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
   17136              :         {
   17137            2 :           append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
   17138              :                                           &OMP_FOR_PRE_BODY (for_stmt));
   17139            2 :           OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
   17140              :         }
   17141        19528 :       if (OMP_FOR_PRE_BODY (inner_for_stmt))
   17142              :         {
   17143         7786 :           append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
   17144              :                                           &OMP_FOR_PRE_BODY (for_stmt));
   17145         7786 :           OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
   17146              :         }
   17147              : 
   17148        19528 :       if (data[0])
   17149              :         {
   17150              :           /* We have some statements or variable declarations in between
   17151              :              the composite construct directives.  Move them around the
   17152              :              inner_for_stmt.  */
   17153         4208 :           data[0] = expr_p;
   17154        16832 :           for (i = 0; i < 3; i++)
   17155        12624 :             if (data[i])
   17156              :               {
   17157         8502 :                 tree t = *data[i];
   17158         8502 :                 if (i < 2 && data[i + 1] == &OMP_BODY (t))
   17159         1248 :                   data[i + 1] = data[i];
   17160         8502 :                 *data[i] = OMP_BODY (t);
   17161         8502 :                 tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
   17162              :                                     NULL_TREE, make_node (BLOCK));
   17163         8502 :                 OMP_BODY (t) = body;
   17164         8502 :                 append_to_statement_list_force (inner_for_stmt,
   17165              :                                                 &BIND_EXPR_BODY (body));
   17166         8502 :                 *data[3] = t;
   17167         8502 :                 data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
   17168         8502 :                 gcc_assert (*data[3] == inner_for_stmt);
   17169              :               }
   17170              :           return GS_OK;
   17171              :         }
   17172              : 
   17173        40918 :       for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
   17174        25598 :         if (!loop_p
   17175        24708 :             && OMP_FOR_ORIG_DECLS (inner_for_stmt)
   17176        12384 :             && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
   17177              :                                         i)) == TREE_LIST
   17178        25666 :             && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
   17179              :                                            i)))
   17180              :           {
   17181           40 :             tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
   17182              :             /* Class iterators aren't allowed on OMP_SIMD, so the only
   17183              :                case we need to solve is distribute parallel for.  They are
   17184              :                allowed on the loop construct, but that is already handled
   17185              :                in gimplify_omp_loop.  */
   17186           40 :             gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
   17187              :                         && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
   17188              :                         && data[1]);
   17189           40 :             tree orig_decl = TREE_PURPOSE (orig);
   17190           40 :             tree last = TREE_VALUE (orig);
   17191           40 :             tree *pc;
   17192           40 :             for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
   17193           74 :                  *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
   17194           42 :               if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
   17195           35 :                    || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
   17196           43 :                   && OMP_CLAUSE_DECL (*pc) == orig_decl)
   17197              :                 break;
   17198           40 :             if (*pc == NULL_TREE)
   17199              :               {
   17200           32 :                 tree *spc;
   17201           32 :                 for (spc = &OMP_PARALLEL_CLAUSES (*data[1]);
   17202           98 :                      *spc; spc = &OMP_CLAUSE_CHAIN (*spc))
   17203           67 :                   if (OMP_CLAUSE_CODE (*spc) == OMP_CLAUSE_PRIVATE
   17204           67 :                       && OMP_CLAUSE_DECL (*spc) == orig_decl)
   17205              :                     break;
   17206           32 :                 if (*spc)
   17207              :                   {
   17208            1 :                     tree c = *spc;
   17209            1 :                     *spc = OMP_CLAUSE_CHAIN (c);
   17210            1 :                     OMP_CLAUSE_CHAIN (c) = NULL_TREE;
   17211            1 :                     *pc = c;
   17212              :                   }
   17213              :               }
   17214           40 :             if (*pc == NULL_TREE)
   17215              :               ;
   17216            9 :             else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
   17217              :               {
   17218              :                 /* private clause will appear only on inner_for_stmt.
   17219              :                    Change it into firstprivate, and add private clause
   17220              :                    on for_stmt.  */
   17221            8 :                 tree c = copy_node (*pc);
   17222            8 :                 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
   17223            8 :                 OMP_FOR_CLAUSES (for_stmt) = c;
   17224            8 :                 OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
   17225            8 :                 lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
   17226              :               }
   17227              :             else
   17228              :               {
   17229              :                 /* lastprivate clause will appear on both inner_for_stmt
   17230              :                    and for_stmt.  Add firstprivate clause to
   17231              :                    inner_for_stmt.  */
   17232            1 :                 tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
   17233              :                                            OMP_CLAUSE_FIRSTPRIVATE);
   17234            1 :                 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
   17235            1 :                 OMP_CLAUSE_CHAIN (c) = *pc;
   17236            1 :                 *pc = c;
   17237            1 :                 lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
   17238              :               }
   17239           40 :             tree c = build_omp_clause (UNKNOWN_LOCATION,
   17240              :                                        OMP_CLAUSE_FIRSTPRIVATE);
   17241           40 :             OMP_CLAUSE_DECL (c) = last;
   17242           40 :             OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
   17243           40 :             OMP_PARALLEL_CLAUSES (*data[1]) = c;
   17244           40 :             c = build_omp_clause (UNKNOWN_LOCATION,
   17245           40 :                                   *pc ? OMP_CLAUSE_SHARED
   17246              :                                       : OMP_CLAUSE_FIRSTPRIVATE);
   17247           40 :             OMP_CLAUSE_DECL (c) = orig_decl;
   17248           40 :             OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
   17249           40 :             OMP_PARALLEL_CLAUSES (*data[1]) = c;
   17250              :           }
   17251              :       /* Similarly, take care of C++ range for temporaries, those should
   17252              :          be firstprivate on OMP_PARALLEL if any.  */
   17253        15320 :       if (data[1])
   17254        19500 :         for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
   17255        12436 :           if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
   17256         6381 :               && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
   17257              :                                           i)) == TREE_LIST
   17258        12512 :               && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
   17259              :                                            i)))
   17260              :             {
   17261           49 :               tree orig
   17262           49 :                 = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
   17263           49 :               tree v = TREE_CHAIN (orig);
   17264           49 :               tree c = build_omp_clause (UNKNOWN_LOCATION,
   17265              :                                          OMP_CLAUSE_FIRSTPRIVATE);
   17266              :               /* First add firstprivate clause for the __for_end artificial
   17267              :                  decl.  */
   17268           49 :               OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 1);
   17269           49 :               if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
   17270              :                   == REFERENCE_TYPE)
   17271            0 :                 OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
   17272           49 :               OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
   17273           49 :               OMP_PARALLEL_CLAUSES (*data[1]) = c;
   17274           49 :               if (TREE_VEC_ELT (v, 0))
   17275              :                 {
   17276              :                   /* And now the same for __for_range artificial decl if it
   17277              :                      exists.  */
   17278           49 :                   c = build_omp_clause (UNKNOWN_LOCATION,
   17279              :                                         OMP_CLAUSE_FIRSTPRIVATE);
   17280           49 :                   OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 0);
   17281           49 :                   if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
   17282              :                       == REFERENCE_TYPE)
   17283           49 :                     OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
   17284           49 :                   OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
   17285           49 :                   OMP_PARALLEL_CLAUSES (*data[1]) = c;
   17286              :                 }
   17287              :             }
   17288        15320 :       break;
   17289              :     }
   17290        57052 :   if (OMP_FOR_INIT (for_stmt) != NULL_TREE)
   17291              :     {
   17292        41732 :       omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
   17293        41732 :       if (*expr_p != for_stmt)
   17294              :         return GS_OK;
   17295              :     }
   17296              : 
   17297        56169 :   switch (TREE_CODE (for_stmt))
   17298              :     {
   17299        19007 :     case OMP_FOR:
   17300        31324 :       if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
   17301              :         {
   17302          458 :           if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
   17303              :                                OMP_CLAUSE_SCHEDULE))
   17304           47 :             error_at (EXPR_LOCATION (for_stmt),
   17305              :                       "%qs clause may not appear on non-rectangular %qs",
   17306           27 :                       "schedule", lang_GNU_Fortran () ? "do" : "for");
   17307          458 :           if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED))
   17308           34 :             error_at (EXPR_LOCATION (for_stmt),
   17309              :                       "%qs clause may not appear on non-rectangular %qs",
   17310           18 :                       "ordered", lang_GNU_Fortran () ? "do" : "for");
   17311              :         }
   17312              :       break;
   17313         8929 :     case OMP_DISTRIBUTE:
   17314         9877 :       if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt)
   17315         8929 :           && omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
   17316              :                               OMP_CLAUSE_DIST_SCHEDULE))
   17317           29 :         error_at (EXPR_LOCATION (for_stmt),
   17318              :                   "%qs clause may not appear on non-rectangular %qs",
   17319              :                   "dist_schedule", "distribute");
   17320              :       break;
   17321              :     case OACC_LOOP:
   17322              :       ort = ORT_ACC;
   17323              :       break;
   17324         1587 :     case OMP_TASKLOOP:
   17325         2525 :       if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
   17326              :         {
   17327           36 :           if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
   17328              :                                OMP_CLAUSE_GRAINSIZE))
   17329           11 :             error_at (EXPR_LOCATION (for_stmt),
   17330              :                       "%qs clause may not appear on non-rectangular %qs",
   17331              :                       "grainsize", "taskloop");
   17332           36 :           if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
   17333              :                                OMP_CLAUSE_NUM_TASKS))
   17334            6 :             error_at (EXPR_LOCATION (for_stmt),
   17335              :                       "%qs clause may not appear on non-rectangular %qs",
   17336              :                       "num_tasks", "taskloop");
   17337              :         }
   17338         1587 :       if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
   17339              :         ort = ORT_UNTIED_TASKLOOP;
   17340              :       else
   17341              :         ort = ORT_TASKLOOP;
   17342              :       break;
   17343        11825 :     case OMP_SIMD:
   17344        11825 :       ort = ORT_SIMD;
   17345        11825 :       break;
   17346         2269 :     case OMP_TILE:
   17347         2269 :     case OMP_UNROLL:
   17348         2269 :       gcc_assert (inner_for_stmt == NULL_TREE);
   17349         2269 :       return gimplify_omp_loop_xform (expr_p, pre_p);
   17350            0 :     default:
   17351            0 :       gcc_unreachable ();
   17352              :     }
   17353              : 
   17354              :   /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
   17355              :      clause for the IV.  */
   17356        11872 :   if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
   17357              :     {
   17358         8002 :       t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
   17359         8002 :       gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
   17360         8002 :       decl = TREE_OPERAND (t, 0);
   17361        18867 :       for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
   17362        12058 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
   17363        12058 :             && OMP_CLAUSE_DECL (c) == decl)
   17364              :           {
   17365         1193 :             OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
   17366         1193 :             break;
   17367              :           }
   17368              :     }
   17369              : 
   17370        53900 :   if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
   17371        54251 :     gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
   17372         1938 :                                loop_p && TREE_CODE (for_stmt) != OMP_SIMD
   17373              :                                ? OMP_LOOP : TREE_CODE (for_stmt));
   17374              : 
   17375        53900 :   if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
   17376         8929 :     gimplify_omp_ctxp->distribute = true;
   17377              : 
   17378              :   /* Handle OMP_FOR_INIT.  */
   17379        53900 :   for_pre_body = NULL;
   17380        53900 :   if ((ort == ORT_SIMD
   17381        42075 :        || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
   17382        65994 :       && OMP_FOR_PRE_BODY (for_stmt))
   17383              :     {
   17384         7647 :       has_decl_expr = BITMAP_ALLOC (NULL);
   17385         7647 :       if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
   17386         7647 :           && VAR_P (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt))))
   17387              :         {
   17388           18 :           t = OMP_FOR_PRE_BODY (for_stmt);
   17389           18 :           bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
   17390              :         }
   17391         7629 :       else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
   17392              :         {
   17393         7627 :           tree_stmt_iterator si;
   17394        13584 :           for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
   17395         5957 :                tsi_next (&si))
   17396              :             {
   17397         5957 :               t = tsi_stmt (si);
   17398         5957 :               if (TREE_CODE (t) == DECL_EXPR
   17399         5957 :                   && VAR_P (DECL_EXPR_DECL (t)))
   17400         5829 :                 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
   17401              :             }
   17402              :         }
   17403              :     }
   17404        53900 :   if (OMP_FOR_PRE_BODY (for_stmt))
   17405              :     {
   17406        21947 :       if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
   17407        21222 :         gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
   17408              :       else
   17409              :         {
   17410          725 :           struct gimplify_omp_ctx ctx;
   17411          725 :           memset (&ctx, 0, sizeof (ctx));
   17412          725 :           ctx.region_type = ORT_NONE;
   17413          725 :           gimplify_omp_ctxp = &ctx;
   17414          725 :           gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
   17415          725 :           gimplify_omp_ctxp = NULL;
   17416              :         }
   17417              :     }
   17418        53900 :   OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
   17419              : 
   17420        53900 :   if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
   17421        15320 :     for_stmt = inner_for_stmt;
   17422              : 
   17423              :   /* For taskloop, need to gimplify the start, end and step before the
   17424              :      taskloop, outside of the taskloop omp context.  */
   17425        53900 :   if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
   17426              :     {
   17427         3460 :       for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
   17428              :         {
   17429         1873 :           t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
   17430         1873 :           gimple_seq *for_pre_p = (gimple_seq_empty_p (for_pre_body)
   17431         1873 :                                    ? pre_p : &for_pre_body);
   17432         1873 :               tree type = TREE_TYPE (TREE_OPERAND (t, 0));
   17433         1873 :           if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
   17434              :             {
   17435           34 :               tree v = TREE_OPERAND (t, 1);
   17436           34 :               gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
   17437              :                                           for_pre_p, orig_for_stmt);
   17438           34 :               gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
   17439              :                                           for_pre_p, orig_for_stmt);
   17440              :             }
   17441              :           else
   17442         1839 :             gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
   17443              :                                         orig_for_stmt);
   17444              : 
   17445              :           /* Handle OMP_FOR_COND.  */
   17446         1873 :           t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
   17447         1873 :           if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
   17448              :             {
   17449           31 :               tree v = TREE_OPERAND (t, 1);
   17450           31 :               gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
   17451              :                                           for_pre_p, orig_for_stmt);
   17452           31 :               gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
   17453              :                                           for_pre_p, orig_for_stmt);
   17454              :             }
   17455              :           else
   17456         1842 :             gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
   17457              :                                         orig_for_stmt);
   17458              : 
   17459              :           /* Handle OMP_FOR_INCR.  */
   17460         1873 :           t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
   17461         1873 :           if (TREE_CODE (t) == MODIFY_EXPR)
   17462              :             {
   17463          591 :               decl = TREE_OPERAND (t, 0);
   17464          591 :               t = TREE_OPERAND (t, 1);
   17465          591 :               tree *tp = &TREE_OPERAND (t, 1);
   17466          591 :               if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
   17467           22 :                 tp = &TREE_OPERAND (t, 0);
   17468              : 
   17469          591 :               gimplify_omp_taskloop_expr (NULL_TREE, tp, for_pre_p,
   17470              :                                           orig_for_stmt);
   17471              :             }
   17472              :         }
   17473              : 
   17474         1587 :       gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
   17475              :                                  OMP_TASKLOOP);
   17476              :     }
   17477              : 
   17478        53900 :   if (orig_for_stmt != for_stmt)
   17479        15320 :     gimplify_omp_ctxp->combined_loop = true;
   17480              : 
   17481        53900 :   for_body = NULL;
   17482        53900 :   gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
   17483              :               == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
   17484        53900 :   gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
   17485              :               == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
   17486              : 
   17487        53900 :   tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
   17488        53900 :   bool is_doacross = false;
   17489        53900 :   if (c && walk_tree_without_duplicates (&OMP_FOR_BODY (for_stmt),
   17490              :                                          find_standalone_omp_ordered, NULL))
   17491              :     {
   17492          531 :       OMP_CLAUSE_ORDERED_DOACROSS (c) = 1;
   17493          531 :       is_doacross = true;
   17494          531 :       int len = TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt));
   17495          531 :       gimplify_omp_ctxp->loop_iter_var.create (len * 2);
   17496         2200 :       for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
   17497         1669 :         if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
   17498              :           {
   17499           10 :             error_at (OMP_CLAUSE_LOCATION (*pc),
   17500              :                       "%<linear%> clause may not be specified together "
   17501              :                       "with %<ordered%> clause if stand-alone %<ordered%> "
   17502              :                       "construct is nested in it");
   17503           10 :             *pc = OMP_CLAUSE_CHAIN (*pc);
   17504              :           }
   17505              :         else
   17506         1659 :           pc = &OMP_CLAUSE_CHAIN (*pc);
   17507              :     }
   17508        53900 :   int collapse = 1, tile = 0;
   17509        53900 :   c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
   17510        53900 :   if (c)
   17511        16018 :     collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
   17512        53900 :   c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
   17513        53900 :   if (c)
   17514          384 :     tile = list_length (OMP_CLAUSE_TILE_LIST (c));
   17515        53900 :   c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ALLOCATE);
   17516        53900 :   hash_set<tree> *allocate_uids = NULL;
   17517        53900 :   if (c)
   17518              :     {
   17519          275 :       allocate_uids = new hash_set<tree>;
   17520         1682 :       for (; c; c = OMP_CLAUSE_CHAIN (c))
   17521         1132 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE)
   17522          478 :           allocate_uids->add (OMP_CLAUSE_DECL (c));
   17523              :     }
   17524       132015 :   for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
   17525              :     {
   17526        78115 :       t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
   17527        78115 :       gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
   17528        78115 :       decl = TREE_OPERAND (t, 0);
   17529        78115 :       gcc_assert (DECL_P (decl));
   17530        78115 :       gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
   17531              :                   || POINTER_TYPE_P (TREE_TYPE (decl)));
   17532        78115 :       if (is_doacross)
   17533              :         {
   17534         1654 :           if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
   17535              :             {
   17536          960 :               tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
   17537          960 :               if (TREE_CODE (orig_decl) == TREE_LIST)
   17538              :                 {
   17539           15 :                   orig_decl = TREE_PURPOSE (orig_decl);
   17540           15 :                   if (!orig_decl)
   17541            0 :                     orig_decl = decl;
   17542              :                 }
   17543          960 :               gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
   17544              :             }
   17545              :           else
   17546          694 :             gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
   17547         1654 :           gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
   17548              :         }
   17549              : 
   17550        78115 :       if (for_stmt == orig_for_stmt)
   17551              :         {
   17552        52517 :           tree orig_decl = decl;
   17553        52517 :           if (OMP_FOR_ORIG_DECLS (for_stmt))
   17554              :             {
   17555        24984 :               tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
   17556        24984 :               if (TREE_CODE (orig_decl) == TREE_LIST)
   17557              :                 {
   17558              :                   orig_decl = TREE_PURPOSE (orig_decl);
   17559              :                   if (!orig_decl)
   17560              :                     orig_decl = decl;
   17561              :                 }
   17562              :             }
   17563        52517 :           if (is_global_var (orig_decl) && DECL_THREAD_LOCAL_P (orig_decl))
   17564           36 :             error_at (EXPR_LOCATION (for_stmt),
   17565              :                       "threadprivate iteration variable %qD", orig_decl);
   17566              :         }
   17567              : 
   17568              :       /* Make sure the iteration variable is private.  */
   17569        78115 :       tree c = NULL_TREE;
   17570        78115 :       tree c2 = NULL_TREE;
   17571        78115 :       if (orig_for_stmt != for_stmt)
   17572              :         {
   17573              :           /* Preserve this information until we gimplify the inner simd.  */
   17574        25598 :           if (has_decl_expr
   17575        25598 :               && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
   17576         4437 :             TREE_PRIVATE (t) = 1;
   17577              :         }
   17578        52517 :       else if (ort == ORT_SIMD)
   17579              :         {
   17580        17971 :           splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
   17581              :                                                  (splay_tree_key) decl);
   17582        17971 :           omp_is_private (gimplify_omp_ctxp, decl,
   17583        17971 :                           1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
   17584              :                                != 1));
   17585        17971 :           if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
   17586              :             {
   17587         2824 :               omp_notice_variable (gimplify_omp_ctxp, decl, true);
   17588         2824 :               if (n->value & GOVD_LASTPRIVATE_CONDITIONAL)
   17589            0 :                 for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
   17590              :                                                 OMP_CLAUSE_LASTPRIVATE);
   17591            0 :                      c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
   17592              :                                                OMP_CLAUSE_LASTPRIVATE))
   17593            0 :                   if (OMP_CLAUSE_DECL (c3) == decl)
   17594              :                     {
   17595            0 :                       warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
   17596              :                                   "conditional %<lastprivate%> on loop "
   17597              :                                   "iterator %qD ignored", decl);
   17598            0 :                       OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
   17599            0 :                       n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
   17600              :                     }
   17601              :             }
   17602        15147 :           else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1 && !loop_p)
   17603              :             {
   17604         5728 :               c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
   17605         5728 :               OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
   17606         5728 :               unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
   17607         5728 :               if ((has_decl_expr
   17608         1388 :                    && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
   17609         6412 :                   || TREE_PRIVATE (t))
   17610              :                 {
   17611         2372 :                   OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
   17612         2372 :                   flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
   17613              :                 }
   17614         5728 :               struct gimplify_omp_ctx *outer
   17615         5728 :                 = gimplify_omp_ctxp->outer_context;
   17616         5728 :               if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
   17617              :                 {
   17618         2433 :                   if (outer->region_type == ORT_WORKSHARE
   17619         2193 :                       && outer->combined_loop)
   17620              :                     {
   17621         2142 :                       n = splay_tree_lookup (outer->variables,
   17622              :                                              (splay_tree_key)decl);
   17623         2142 :                       if (n != NULL && (n->value & GOVD_LOCAL) != 0)
   17624              :                         {
   17625            0 :                           OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
   17626            0 :                           flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
   17627              :                         }
   17628              :                       else
   17629              :                         {
   17630         2142 :                           struct gimplify_omp_ctx *octx = outer->outer_context;
   17631         2142 :                           if (octx
   17632         1714 :                               && octx->region_type == ORT_COMBINED_PARALLEL
   17633         1476 :                               && octx->outer_context
   17634         1209 :                               && (octx->outer_context->region_type
   17635              :                                   == ORT_WORKSHARE)
   17636         1079 :                               && octx->outer_context->combined_loop)
   17637              :                             {
   17638         1079 :                               octx = octx->outer_context;
   17639         1079 :                               n = splay_tree_lookup (octx->variables,
   17640              :                                                      (splay_tree_key)decl);
   17641         1079 :                               if (n != NULL && (n->value & GOVD_LOCAL) != 0)
   17642              :                                 {
   17643            0 :                                   OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
   17644            0 :                                   flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
   17645              :                                 }
   17646              :                             }
   17647              :                         }
   17648              :                     }
   17649              :                 }
   17650              : 
   17651         5728 :               OMP_CLAUSE_DECL (c) = decl;
   17652         5728 :               OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
   17653         5728 :               OMP_FOR_CLAUSES (for_stmt) = c;
   17654         5728 :               omp_add_variable (gimplify_omp_ctxp, decl, flags);
   17655         5728 :               if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
   17656         2433 :                 omp_lastprivate_for_combined_outer_constructs (outer, decl,
   17657              :                                                                true);
   17658              :             }
   17659              :           else
   17660              :             {
   17661         9419 :               bool lastprivate
   17662              :                 = (!has_decl_expr
   17663         9419 :                    || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
   17664         9419 :               if (TREE_PRIVATE (t))
   17665         2799 :                 lastprivate = false;
   17666         9419 :               if (loop_p && OMP_FOR_ORIG_DECLS (for_stmt))
   17667              :                 {
   17668          522 :                   tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
   17669          522 :                   if (TREE_CODE (elt) == TREE_LIST && TREE_PURPOSE (elt))
   17670              :                     lastprivate = false;
   17671              :                 }
   17672              : 
   17673         9419 :               struct gimplify_omp_ctx *outer
   17674         9419 :                 = gimplify_omp_ctxp->outer_context;
   17675         9419 :               if (outer && lastprivate)
   17676         5394 :                 omp_lastprivate_for_combined_outer_constructs (outer, decl,
   17677              :                                                                true);
   17678              : 
   17679        12766 :               c = build_omp_clause (input_location,
   17680              :                                     lastprivate ? OMP_CLAUSE_LASTPRIVATE
   17681              :                                                 : OMP_CLAUSE_PRIVATE);
   17682         9419 :               OMP_CLAUSE_DECL (c) = decl;
   17683         9419 :               OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
   17684         9419 :               OMP_FOR_CLAUSES (for_stmt) = c;
   17685         9419 :               omp_add_variable (gimplify_omp_ctxp, decl,
   17686              :                                 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
   17687              :                                 | GOVD_EXPLICIT | GOVD_SEEN);
   17688         9419 :               c = NULL_TREE;
   17689              :             }
   17690              :         }
   17691        34546 :       else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
   17692              :         {
   17693         8752 :           omp_notice_variable (gimplify_omp_ctxp, decl, true);
   17694         8752 :           splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
   17695              :                                                  (splay_tree_key) decl);
   17696         8752 :           if (n && (n->value & GOVD_LASTPRIVATE_CONDITIONAL))
   17697           80 :             for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
   17698              :                                             OMP_CLAUSE_LASTPRIVATE);
   17699           80 :                  c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
   17700              :                                            OMP_CLAUSE_LASTPRIVATE))
   17701           40 :               if (OMP_CLAUSE_DECL (c3) == decl)
   17702              :                 {
   17703           40 :                   warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
   17704              :                               "conditional %<lastprivate%> on loop "
   17705              :                               "iterator %qD ignored", decl);
   17706           40 :                   OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
   17707           40 :                   n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
   17708              :                 }
   17709              :         }
   17710              :       else
   17711        25794 :         omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
   17712              : 
   17713              :       /* If DECL is not a gimple register, create a temporary variable to act
   17714              :          as an iteration counter.  This is valid, since DECL cannot be
   17715              :          modified in the body of the loop.  Similarly for any iteration vars
   17716              :          in simd with collapse > 1 where the iterator vars must be
   17717              :          lastprivate.  And similarly for vars mentioned in allocate clauses.  */
   17718        78115 :       if (orig_for_stmt != for_stmt)
   17719        25598 :         var = decl;
   17720        52517 :       else if (!is_gimple_reg (decl)
   17721        48645 :                || (ort == ORT_SIMD
   17722        15766 :                    && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
   17723        92600 :                || (allocate_uids && allocate_uids->contains (decl)))
   17724              :         {
   17725        12466 :           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   17726              :           /* Make sure omp_add_variable is not called on it prematurely.
   17727              :              We call it ourselves a few lines later.  */
   17728        12466 :           gimplify_omp_ctxp = NULL;
   17729        12466 :           var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
   17730        12466 :           gimplify_omp_ctxp = ctx;
   17731        12466 :           TREE_OPERAND (t, 0) = var;
   17732              : 
   17733        12466 :           gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
   17734              : 
   17735        12466 :           if (ort == ORT_SIMD
   17736        12466 :               && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
   17737              :             {
   17738          798 :               c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
   17739          798 :               OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
   17740          798 :               OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
   17741          798 :               OMP_CLAUSE_DECL (c2) = var;
   17742          798 :               OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
   17743          798 :               OMP_FOR_CLAUSES (for_stmt) = c2;
   17744          798 :               omp_add_variable (gimplify_omp_ctxp, var,
   17745              :                                 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
   17746          798 :               if (c == NULL_TREE)
   17747              :                 {
   17748          546 :                   c = c2;
   17749          546 :                   c2 = NULL_TREE;
   17750              :                 }
   17751              :             }
   17752              :           else
   17753        11668 :             omp_add_variable (gimplify_omp_ctxp, var,
   17754              :                               GOVD_PRIVATE | GOVD_SEEN);
   17755              :         }
   17756              :       else
   17757              :         var = decl;
   17758              : 
   17759        78115 :       gimplify_omp_ctxp->in_for_exprs = true;
   17760        78115 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
   17761              :         {
   17762          725 :           tree lb = TREE_OPERAND (t, 1);
   17763          725 :           tret = gimplify_expr (&TREE_VEC_ELT (lb, 1), &for_pre_body, NULL,
   17764              :                                 is_gimple_val, fb_rvalue, false);
   17765          725 :           ret = MIN (ret, tret);
   17766          725 :           tret = gimplify_expr (&TREE_VEC_ELT (lb, 2), &for_pre_body, NULL,
   17767              :                                 is_gimple_val, fb_rvalue, false);
   17768              :         }
   17769              :       else
   17770        77390 :         tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
   17771              :                               is_gimple_val, fb_rvalue, false);
   17772        78115 :       gimplify_omp_ctxp->in_for_exprs = false;
   17773        78115 :       ret = MIN (ret, tret);
   17774        78115 :       if (ret == GS_ERROR)
   17775              :         return ret;
   17776              : 
   17777              :       /* Handle OMP_FOR_COND.  */
   17778        78115 :       t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
   17779        78115 :       gcc_assert (COMPARISON_CLASS_P (t));
   17780        78115 :       gcc_assert (TREE_OPERAND (t, 0) == decl);
   17781              : 
   17782        78115 :       gimplify_omp_ctxp->in_for_exprs = true;
   17783        78115 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
   17784              :         {
   17785          599 :           tree ub = TREE_OPERAND (t, 1);
   17786          599 :           tret = gimplify_expr (&TREE_VEC_ELT (ub, 1), &for_pre_body, NULL,
   17787              :                                 is_gimple_val, fb_rvalue, false);
   17788          599 :           ret = MIN (ret, tret);
   17789          599 :           tret = gimplify_expr (&TREE_VEC_ELT (ub, 2), &for_pre_body, NULL,
   17790              :                                 is_gimple_val, fb_rvalue, false);
   17791              :         }
   17792              :       else
   17793        77516 :         tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
   17794              :                               is_gimple_val, fb_rvalue, false);
   17795        78115 :       gimplify_omp_ctxp->in_for_exprs = false;
   17796        78115 :       ret = MIN (ret, tret);
   17797              : 
   17798              :       /* Handle OMP_FOR_INCR.  */
   17799        78115 :       t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
   17800        78115 :       switch (TREE_CODE (t))
   17801              :         {
   17802        39880 :         case PREINCREMENT_EXPR:
   17803        39880 :         case POSTINCREMENT_EXPR:
   17804        39880 :           {
   17805        39880 :             tree decl = TREE_OPERAND (t, 0);
   17806              :             /* c_omp_for_incr_canonicalize_ptr() should have been
   17807              :                called to massage things appropriately.  */
   17808        39880 :             gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
   17809              : 
   17810        39880 :             if (orig_for_stmt != for_stmt)
   17811              :               break;
   17812        27145 :             t = build_int_cst (TREE_TYPE (decl), 1);
   17813        27145 :             if (c)
   17814         4559 :               OMP_CLAUSE_LINEAR_STEP (c) = t;
   17815        27145 :             t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
   17816        27145 :             t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
   17817        27145 :             TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
   17818        27145 :             break;
   17819              :           }
   17820              : 
   17821         3932 :         case PREDECREMENT_EXPR:
   17822         3932 :         case POSTDECREMENT_EXPR:
   17823              :           /* c_omp_for_incr_canonicalize_ptr() should have been
   17824              :              called to massage things appropriately.  */
   17825         3932 :           gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
   17826         3932 :           if (orig_for_stmt != for_stmt)
   17827              :             break;
   17828         2046 :           t = build_int_cst (TREE_TYPE (decl), -1);
   17829         2046 :           if (c)
   17830          112 :             OMP_CLAUSE_LINEAR_STEP (c) = t;
   17831         2046 :           t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
   17832         2046 :           t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
   17833         2046 :           TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
   17834         2046 :           break;
   17835              : 
   17836        34303 :         case MODIFY_EXPR:
   17837        34303 :           gcc_assert (TREE_OPERAND (t, 0) == decl);
   17838        34303 :           TREE_OPERAND (t, 0) = var;
   17839              : 
   17840        34303 :           t = TREE_OPERAND (t, 1);
   17841        34303 :           switch (TREE_CODE (t))
   17842              :             {
   17843        27333 :             case PLUS_EXPR:
   17844        27333 :               if (TREE_OPERAND (t, 1) == decl)
   17845              :                 {
   17846          223 :                   TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
   17847          223 :                   TREE_OPERAND (t, 0) = var;
   17848          223 :                   break;
   17849              :                 }
   17850              : 
   17851              :               /* Fallthru.  */
   17852        34080 :             case MINUS_EXPR:
   17853        34080 :             case POINTER_PLUS_EXPR:
   17854        34080 :               gcc_assert (TREE_OPERAND (t, 0) == decl);
   17855        34080 :               TREE_OPERAND (t, 0) = var;
   17856        34080 :               break;
   17857            0 :             default:
   17858            0 :               gcc_unreachable ();
   17859              :             }
   17860              : 
   17861        34303 :           gimplify_omp_ctxp->in_for_exprs = true;
   17862        34303 :           tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
   17863              :                                 is_gimple_val, fb_rvalue, false);
   17864        34303 :           ret = MIN (ret, tret);
   17865        34303 :           if (c)
   17866              :             {
   17867         1603 :               tree step = TREE_OPERAND (t, 1);
   17868         1603 :               tree stept = TREE_TYPE (decl);
   17869         1603 :               if (POINTER_TYPE_P (stept))
   17870          280 :                 stept = sizetype;
   17871         1603 :               step = fold_convert (stept, step);
   17872         1603 :               if (TREE_CODE (t) == MINUS_EXPR)
   17873          295 :                 step = fold_build1 (NEGATE_EXPR, stept, step);
   17874         1603 :               OMP_CLAUSE_LINEAR_STEP (c) = step;
   17875         1603 :               if (step != TREE_OPERAND (t, 1))
   17876              :                 {
   17877          295 :                   tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
   17878              :                                         &for_pre_body, NULL,
   17879              :                                         is_gimple_val, fb_rvalue, false);
   17880          295 :                   ret = MIN (ret, tret);
   17881              :                 }
   17882              :             }
   17883        34303 :           gimplify_omp_ctxp->in_for_exprs = false;
   17884        34303 :           break;
   17885              : 
   17886            0 :         default:
   17887            0 :           gcc_unreachable ();
   17888              :         }
   17889              : 
   17890        78115 :       if (c2)
   17891              :         {
   17892          252 :           gcc_assert (c);
   17893          252 :           OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
   17894              :         }
   17895              : 
   17896        78115 :       if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
   17897              :         {
   17898        95287 :           for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
   17899        71628 :             if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
   17900        17679 :                   && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
   17901        61783 :                  || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
   17902         2227 :                      && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
   17903         1413 :                      && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
   17904        82864 :                 && OMP_CLAUSE_DECL (c) == decl)
   17905              :               {
   17906         8231 :                 if (is_doacross && (collapse == 1 || i >= collapse))
   17907              :                   t = var;
   17908              :                 else
   17909              :                   {
   17910         8158 :                     t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
   17911         8158 :                     gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
   17912         8158 :                     gcc_assert (TREE_OPERAND (t, 0) == var);
   17913         8158 :                     t = TREE_OPERAND (t, 1);
   17914         8158 :                     gcc_assert (TREE_CODE (t) == PLUS_EXPR
   17915              :                                 || TREE_CODE (t) == MINUS_EXPR
   17916              :                                 || TREE_CODE (t) == POINTER_PLUS_EXPR);
   17917         8158 :                     gcc_assert (TREE_OPERAND (t, 0) == var);
   17918        16214 :                     t = build2 (TREE_CODE (t), TREE_TYPE (decl),
   17919              :                                 is_doacross ? var : decl,
   17920         8158 :                                 TREE_OPERAND (t, 1));
   17921              :                   }
   17922         8231 :                 gimple_seq *seq;
   17923         8231 :                 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
   17924         7607 :                   seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
   17925              :                 else
   17926          624 :                   seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
   17927         8231 :                 push_gimplify_context ();
   17928         8231 :                 gimplify_assign (decl, t, seq);
   17929         8231 :                 gimple *bind = NULL;
   17930         8231 :                 if (gimplify_ctxp->temps)
   17931              :                   {
   17932         2616 :                     bind = gimple_build_bind (NULL_TREE, *seq, NULL_TREE);
   17933         2616 :                     *seq = NULL;
   17934         2616 :                     gimplify_seq_add_stmt (seq, bind);
   17935              :                   }
   17936         8231 :                 pop_gimplify_context (bind);
   17937              :               }
   17938              :         }
   17939        78115 :       if (OMP_FOR_NON_RECTANGULAR (for_stmt) && var != decl)
   17940         2627 :         for (int j = i + 1; j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
   17941              :           {
   17942         1203 :             t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
   17943         1203 :             gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
   17944         1203 :             if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
   17945         1203 :                 && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
   17946          342 :               TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
   17947         1203 :             t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
   17948         1203 :             gcc_assert (COMPARISON_CLASS_P (t));
   17949         1203 :             if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
   17950         1203 :                 && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
   17951          343 :               TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
   17952              :           }
   17953              :     }
   17954              : 
   17955        53900 :   BITMAP_FREE (has_decl_expr);
   17956        54175 :   delete allocate_uids;
   17957              : 
   17958        53900 :   if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
   17959        52313 :       || (loop_p && orig_for_stmt == for_stmt))
   17960              :     {
   17961         2662 :       push_gimplify_context ();
   17962         2662 :       if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
   17963              :         {
   17964         1906 :           OMP_FOR_BODY (orig_for_stmt)
   17965         1906 :             = build3 (BIND_EXPR, void_type_node, NULL,
   17966         1906 :                       OMP_FOR_BODY (orig_for_stmt), NULL);
   17967         1906 :           TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
   17968              :         }
   17969              :     }
   17970              : 
   17971        53900 :   gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
   17972              :                                          &for_body);
   17973              : 
   17974        53900 :   if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
   17975        52313 :       || (loop_p && orig_for_stmt == for_stmt))
   17976              :     {
   17977         2662 :       if (gimple_code (g) == GIMPLE_BIND)
   17978         2662 :         pop_gimplify_context (g);
   17979              :       else
   17980            0 :         pop_gimplify_context (NULL);
   17981              :     }
   17982              : 
   17983        53900 :   if (orig_for_stmt != for_stmt)
   17984        40918 :     for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
   17985              :       {
   17986        25598 :         t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
   17987        25598 :         decl = TREE_OPERAND (t, 0);
   17988        25598 :         struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   17989        25598 :         if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
   17990          772 :           gimplify_omp_ctxp = ctx->outer_context;
   17991        25598 :         var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
   17992        25598 :         gimplify_omp_ctxp = ctx;
   17993        25598 :         omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
   17994        25598 :         TREE_OPERAND (t, 0) = var;
   17995        25598 :         t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
   17996        25598 :         TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
   17997        25598 :         TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
   17998        25598 :         if (OMP_FOR_NON_RECTANGULAR (for_stmt))
   17999          791 :           for (int j = i + 1;
   18000          791 :                j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
   18001              :             {
   18002          377 :               t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
   18003          377 :               gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
   18004          377 :               if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
   18005          377 :                   && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
   18006              :                 {
   18007          133 :                   TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
   18008          133 :                   TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
   18009              :                 }
   18010          377 :               t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
   18011          377 :               gcc_assert (COMPARISON_CLASS_P (t));
   18012          377 :               if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
   18013          377 :                   && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
   18014              :                 {
   18015           67 :                   TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
   18016           67 :                   TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
   18017              :                 }
   18018              :           }
   18019              :       }
   18020              : 
   18021        53900 :   gimplify_adjust_omp_clauses (pre_p, for_body,
   18022              :                                &OMP_FOR_CLAUSES (orig_for_stmt),
   18023        53900 :                                TREE_CODE (orig_for_stmt));
   18024              : 
   18025        53900 :   int kind;
   18026        53900 :   switch (TREE_CODE (orig_for_stmt))
   18027              :     {
   18028              :     case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
   18029        11825 :     case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
   18030         8929 :     case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
   18031         1587 :     case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
   18032        12552 :     case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
   18033            0 :     default:
   18034            0 :       gcc_unreachable ();
   18035              :     }
   18036        53900 :   if (loop_p && kind == GF_OMP_FOR_KIND_SIMD)
   18037              :     {
   18038         1075 :       gimplify_seq_add_seq (pre_p, for_pre_body);
   18039         1075 :       for_pre_body = NULL;
   18040              :     }
   18041        53900 :   gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
   18042        53900 :                                TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
   18043              :                                for_pre_body);
   18044        53900 :   if (orig_for_stmt != for_stmt)
   18045        15320 :     gimple_omp_for_set_combined_p (gfor, true);
   18046        53900 :   if (gimplify_omp_ctxp
   18047        45095 :       && (gimplify_omp_ctxp->combined_loop
   18048        36839 :           || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
   18049        13055 :               && gimplify_omp_ctxp->outer_context
   18050         8554 :               && gimplify_omp_ctxp->outer_context->combined_loop)))
   18051              :     {
   18052        15320 :       gimple_omp_for_set_combined_into_p (gfor, true);
   18053        15320 :       if (gimplify_omp_ctxp->combined_loop)
   18054         8256 :         gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
   18055              :       else
   18056         7064 :         gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
   18057              :     }
   18058              : 
   18059       132015 :   for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
   18060              :     {
   18061        78115 :       t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
   18062        78115 :       gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
   18063        78115 :       gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
   18064        78115 :       t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
   18065        78115 :       gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
   18066        78115 :       gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
   18067        78115 :       t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
   18068        78115 :       gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
   18069              :     }
   18070              : 
   18071              :   /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
   18072              :      constructs with GIMPLE_OMP_TASK sandwiched in between them.
   18073              :      The outer taskloop stands for computing the number of iterations,
   18074              :      counts for collapsed loops and holding taskloop specific clauses.
   18075              :      The task construct stands for the effect of data sharing on the
   18076              :      explicit task it creates and the inner taskloop stands for expansion
   18077              :      of the static loop inside of the explicit task construct.  */
   18078        53900 :   if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
   18079              :     {
   18080         1587 :       tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
   18081         1587 :       tree task_clauses = NULL_TREE;
   18082         1587 :       tree c = *gfor_clauses_ptr;
   18083         1587 :       tree *gtask_clauses_ptr = &task_clauses;
   18084         1587 :       tree outer_for_clauses = NULL_TREE;
   18085         1587 :       tree *gforo_clauses_ptr = &outer_for_clauses;
   18086         1587 :       bitmap lastprivate_uids = NULL;
   18087         1587 :       if (omp_find_clause (c, OMP_CLAUSE_ALLOCATE))
   18088              :         {
   18089           36 :           c = omp_find_clause (c, OMP_CLAUSE_LASTPRIVATE);
   18090           36 :           if (c)
   18091              :             {
   18092           18 :               lastprivate_uids = BITMAP_ALLOC (NULL);
   18093           54 :               for (; c; c = omp_find_clause (OMP_CLAUSE_CHAIN (c),
   18094              :                                              OMP_CLAUSE_LASTPRIVATE))
   18095           18 :                 bitmap_set_bit (lastprivate_uids,
   18096           18 :                                 DECL_UID (OMP_CLAUSE_DECL (c)));
   18097              :             }
   18098           36 :           c = *gfor_clauses_ptr;
   18099              :         }
   18100        12259 :       for (; c; c = OMP_CLAUSE_CHAIN (c))
   18101        10672 :         switch (OMP_CLAUSE_CODE (c))
   18102              :           {
   18103              :           /* These clauses are allowed on task, move them there.  */
   18104         6232 :           case OMP_CLAUSE_SHARED:
   18105         6232 :           case OMP_CLAUSE_FIRSTPRIVATE:
   18106         6232 :           case OMP_CLAUSE_DEFAULT:
   18107         6232 :           case OMP_CLAUSE_IF:
   18108         6232 :           case OMP_CLAUSE_UNTIED:
   18109         6232 :           case OMP_CLAUSE_FINAL:
   18110         6232 :           case OMP_CLAUSE_MERGEABLE:
   18111         6232 :           case OMP_CLAUSE_PRIORITY:
   18112         6232 :           case OMP_CLAUSE_REDUCTION:
   18113         6232 :           case OMP_CLAUSE_IN_REDUCTION:
   18114         6232 :             *gtask_clauses_ptr = c;
   18115         6232 :             gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
   18116         6232 :             break;
   18117         2070 :           case OMP_CLAUSE_PRIVATE:
   18118         2070 :             if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
   18119              :               {
   18120              :                 /* We want private on outer for and firstprivate
   18121              :                    on task.  */
   18122           26 :                 *gtask_clauses_ptr
   18123           26 :                   = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   18124              :                                       OMP_CLAUSE_FIRSTPRIVATE);
   18125           26 :                 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
   18126           26 :                 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
   18127              :                                                     openacc);
   18128           26 :                 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
   18129           26 :                 *gforo_clauses_ptr = c;
   18130           26 :                 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
   18131              :               }
   18132              :             else
   18133              :               {
   18134         2044 :                 *gtask_clauses_ptr = c;
   18135         2044 :                 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
   18136              :               }
   18137              :             break;
   18138              :           /* These clauses go into outer taskloop clauses.  */
   18139          533 :           case OMP_CLAUSE_GRAINSIZE:
   18140          533 :           case OMP_CLAUSE_NUM_TASKS:
   18141          533 :           case OMP_CLAUSE_NOGROUP:
   18142          533 :             *gforo_clauses_ptr = c;
   18143          533 :             gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
   18144          533 :             break;
   18145              :           /* Collapse clause we duplicate on both taskloops.  */
   18146          584 :           case OMP_CLAUSE_COLLAPSE:
   18147          584 :             *gfor_clauses_ptr = c;
   18148          584 :             gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
   18149          584 :             *gforo_clauses_ptr = copy_node (c);
   18150          584 :             gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
   18151          584 :             break;
   18152              :           /* For lastprivate, keep the clause on inner taskloop, and add
   18153              :              a shared clause on task.  If the same decl is also firstprivate,
   18154              :              add also firstprivate clause on the inner taskloop.  */
   18155         1191 :           case OMP_CLAUSE_LASTPRIVATE:
   18156         1191 :             if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
   18157              :               {
   18158              :                 /* For taskloop C++ lastprivate IVs, we want:
   18159              :                    1) private on outer taskloop
   18160              :                    2) firstprivate and shared on task
   18161              :                    3) lastprivate on inner taskloop  */
   18162           38 :                 *gtask_clauses_ptr
   18163           38 :                   = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   18164              :                                       OMP_CLAUSE_FIRSTPRIVATE);
   18165           38 :                 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
   18166           38 :                 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
   18167              :                                                     openacc);
   18168           38 :                 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
   18169           38 :                 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
   18170           38 :                 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   18171              :                                                        OMP_CLAUSE_PRIVATE);
   18172           38 :                 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
   18173           38 :                 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
   18174           38 :                 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
   18175           38 :                 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
   18176              :               }
   18177         1191 :             *gfor_clauses_ptr = c;
   18178         1191 :             gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
   18179         1191 :             *gtask_clauses_ptr
   18180         1191 :               = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
   18181         1191 :             OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
   18182         1191 :             if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
   18183          312 :               OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
   18184         1191 :             gtask_clauses_ptr
   18185         1191 :               = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
   18186         1191 :             break;
   18187              :           /* Allocate clause we duplicate on task and inner taskloop
   18188              :              if the decl is lastprivate, otherwise just put on task.  */
   18189           62 :           case OMP_CLAUSE_ALLOCATE:
   18190           62 :             if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
   18191           62 :                 && DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
   18192              :               {
   18193              :                 /* Additionally, put firstprivate clause on task
   18194              :                    for the allocator if it is not constant.  */
   18195           34 :                 *gtask_clauses_ptr
   18196           34 :                   = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   18197              :                                       OMP_CLAUSE_FIRSTPRIVATE);
   18198           34 :                 OMP_CLAUSE_DECL (*gtask_clauses_ptr)
   18199           34 :                   = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
   18200           34 :                 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
   18201              :               }
   18202           62 :             if (lastprivate_uids
   18203           97 :                 && bitmap_bit_p (lastprivate_uids,
   18204           35 :                                  DECL_UID (OMP_CLAUSE_DECL (c))))
   18205              :               {
   18206           17 :                 *gfor_clauses_ptr = c;
   18207           17 :                 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
   18208           17 :                 *gtask_clauses_ptr = copy_node (c);
   18209           17 :                 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
   18210              :               }
   18211              :             else
   18212              :               {
   18213           45 :                 *gtask_clauses_ptr = c;
   18214           45 :                 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
   18215              :               }
   18216              :             break;
   18217            0 :           default:
   18218            0 :             gcc_unreachable ();
   18219              :           }
   18220         1587 :       *gfor_clauses_ptr = NULL_TREE;
   18221         1587 :       *gtask_clauses_ptr = NULL_TREE;
   18222         1587 :       *gforo_clauses_ptr = NULL_TREE;
   18223         1587 :       BITMAP_FREE (lastprivate_uids);
   18224         1587 :       gimple_set_location (gfor, input_location);
   18225         1587 :       g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
   18226         1587 :       g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
   18227              :                                  NULL_TREE, NULL_TREE, NULL_TREE);
   18228         1587 :       gimple_set_location (g, input_location);
   18229         1587 :       gimple_omp_task_set_taskloop_p (g, true);
   18230         1587 :       g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
   18231         1587 :       gomp_for *gforo
   18232         1587 :         = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
   18233              :                                 gimple_omp_for_collapse (gfor),
   18234              :                                 gimple_omp_for_pre_body (gfor));
   18235         1587 :       gimple_omp_for_set_pre_body (gfor, NULL);
   18236         1587 :       gimple_omp_for_set_combined_p (gforo, true);
   18237         1587 :       gimple_omp_for_set_combined_into_p (gfor, true);
   18238         3460 :       for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
   18239              :         {
   18240         1873 :           tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
   18241         1873 :           tree v = create_tmp_var (type);
   18242         1873 :           gimple_omp_for_set_index (gforo, i, v);
   18243         1873 :           t = unshare_expr (gimple_omp_for_initial (gfor, i));
   18244         1873 :           gimple_omp_for_set_initial (gforo, i, t);
   18245         1873 :           gimple_omp_for_set_cond (gforo, i,
   18246              :                                    gimple_omp_for_cond (gfor, i));
   18247         1873 :           t = unshare_expr (gimple_omp_for_final (gfor, i));
   18248         1873 :           gimple_omp_for_set_final (gforo, i, t);
   18249         1873 :           t = unshare_expr (gimple_omp_for_incr (gfor, i));
   18250         1873 :           gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
   18251         1873 :           TREE_OPERAND (t, 0) = v;
   18252         1873 :           gimple_omp_for_set_incr (gforo, i, t);
   18253         1873 :           t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
   18254         1873 :           OMP_CLAUSE_DECL (t) = v;
   18255         1873 :           OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
   18256         1873 :           gimple_omp_for_set_clauses (gforo, t);
   18257         1873 :           if (OMP_FOR_NON_RECTANGULAR (for_stmt))
   18258              :             {
   18259           90 :               tree *p1 = NULL, *p2 = NULL;
   18260           90 :               t = gimple_omp_for_initial (gforo, i);
   18261           90 :               if (TREE_CODE (t) == TREE_VEC)
   18262           34 :                 p1 = &TREE_VEC_ELT (t, 0);
   18263           90 :               t = gimple_omp_for_final (gforo, i);
   18264           90 :               if (TREE_CODE (t) == TREE_VEC)
   18265              :                 {
   18266           31 :                   if (p1)
   18267           23 :                     p2 = &TREE_VEC_ELT (t, 0);
   18268              :                   else
   18269            8 :                     p1 = &TREE_VEC_ELT (t, 0);
   18270              :                 }
   18271           90 :               if (p1)
   18272              :                 {
   18273              :                   int j;
   18274           58 :                   for (j = 0; j < i; j++)
   18275           58 :                     if (*p1 == gimple_omp_for_index (gfor, j))
   18276              :                       {
   18277           42 :                         *p1 = gimple_omp_for_index (gforo, j);
   18278           42 :                         if (p2)
   18279           23 :                           *p2 = *p1;
   18280              :                         break;
   18281              :                       }
   18282           42 :                   gcc_assert (j < i);
   18283              :                 }
   18284              :             }
   18285              :         }
   18286         1587 :       gimplify_seq_add_stmt (pre_p, gforo);
   18287              :     }
   18288              :   else
   18289        52313 :     gimplify_seq_add_stmt (pre_p, gfor);
   18290              : 
   18291        53900 :   if (TREE_CODE (orig_for_stmt) == OMP_FOR)
   18292              :     {
   18293        19007 :       struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   18294        19007 :       unsigned lastprivate_conditional = 0;
   18295        19007 :       while (ctx
   18296        19043 :              && (ctx->region_type == ORT_TARGET_DATA
   18297        14957 :                  || ctx->region_type == ORT_TASKGROUP))
   18298           36 :         ctx = ctx->outer_context;
   18299        19007 :       if (ctx && (ctx->region_type & ORT_PARALLEL) != 0)
   18300        14697 :         for (tree c = gimple_omp_for_clauses (gfor);
   18301        67054 :              c; c = OMP_CLAUSE_CHAIN (c))
   18302        52357 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
   18303        52357 :               && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
   18304          162 :             ++lastprivate_conditional;
   18305        14697 :       if (lastprivate_conditional)
   18306              :         {
   18307          122 :           struct omp_for_data fd;
   18308          122 :           omp_extract_for_data (gfor, &fd, NULL);
   18309          122 :           tree type = build_array_type_nelts (unsigned_type_for (fd.iter_type),
   18310          122 :                                               lastprivate_conditional);
   18311          122 :           tree var = create_tmp_var_raw (type);
   18312          122 :           tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
   18313          122 :           OMP_CLAUSE_DECL (c) = var;
   18314          122 :           OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
   18315          122 :           gimple_omp_for_set_clauses (gfor, c);
   18316          122 :           omp_add_variable (ctx, var, GOVD_CONDTEMP | GOVD_SEEN);
   18317              :         }
   18318              :     }
   18319        34893 :   else if (TREE_CODE (orig_for_stmt) == OMP_SIMD)
   18320              :     {
   18321        11825 :       unsigned lastprivate_conditional = 0;
   18322        56276 :       for (tree c = gimple_omp_for_clauses (gfor); c; c = OMP_CLAUSE_CHAIN (c))
   18323        44451 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
   18324        44451 :             && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
   18325          143 :           ++lastprivate_conditional;
   18326        11825 :       if (lastprivate_conditional)
   18327              :         {
   18328          115 :           struct omp_for_data fd;
   18329          115 :           omp_extract_for_data (gfor, &fd, NULL);
   18330          115 :           tree type = unsigned_type_for (fd.iter_type);
   18331          373 :           while (lastprivate_conditional--)
   18332              :             {
   18333          143 :               tree c = build_omp_clause (UNKNOWN_LOCATION,
   18334              :                                          OMP_CLAUSE__CONDTEMP_);
   18335          143 :               OMP_CLAUSE_DECL (c) = create_tmp_var (type);
   18336          143 :               OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
   18337          143 :               gimple_omp_for_set_clauses (gfor, c);
   18338              :             }
   18339              :         }
   18340              :     }
   18341              : 
   18342        53900 :   if (ret != GS_ALL_DONE)
   18343              :     return GS_ERROR;
   18344        53900 :   *expr_p = NULL_TREE;
   18345        53900 :   return GS_ALL_DONE;
   18346              : }
   18347              : 
   18348              : /* Helper for gimplify_omp_loop, called through walk_tree.  */
   18349              : 
   18350              : static tree
   18351          294 : note_no_context_vars (tree *tp, int *, void *data)
   18352              : {
   18353          294 :   if (VAR_P (*tp)
   18354           56 :       && DECL_CONTEXT (*tp) == NULL_TREE
   18355          309 :       && !is_global_var (*tp))
   18356              :     {
   18357           15 :       vec<tree> *d = (vec<tree> *) data;
   18358           15 :       d->safe_push (*tp);
   18359           15 :       DECL_CONTEXT (*tp) = current_function_decl;
   18360              :     }
   18361          294 :   return NULL_TREE;
   18362              : }
   18363              : 
   18364              : /* Gimplify the gross structure of an OMP_LOOP statement.  */
   18365              : 
   18366              : static enum gimplify_status
   18367         1075 : gimplify_omp_loop (tree *expr_p, gimple_seq *pre_p)
   18368              : {
   18369         1075 :   tree for_stmt = *expr_p;
   18370         1075 :   tree clauses = OMP_FOR_CLAUSES (for_stmt);
   18371         1075 :   struct gimplify_omp_ctx *octx = gimplify_omp_ctxp;
   18372         1075 :   enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
   18373         1075 :   int i;
   18374              : 
   18375         1075 :   omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
   18376         1075 :   if (*expr_p != for_stmt)
   18377              :     return GS_OK;
   18378              : 
   18379              :   /* If order is not present, the behavior is as if order(concurrent)
   18380              :      appeared.  */
   18381         1075 :   tree order = omp_find_clause (clauses, OMP_CLAUSE_ORDER);
   18382         1075 :   if (order == NULL_TREE)
   18383              :     {
   18384          818 :       order = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_ORDER);
   18385          818 :       OMP_CLAUSE_CHAIN (order) = clauses;
   18386          818 :       OMP_FOR_CLAUSES (for_stmt) = clauses = order;
   18387              :     }
   18388              : 
   18389         1075 :   tree bind = omp_find_clause (clauses, OMP_CLAUSE_BIND);
   18390         1075 :   if (bind == NULL_TREE)
   18391              :     {
   18392          580 :       if (!flag_openmp) /* flag_openmp_simd */
   18393              :         ;
   18394          563 :       else if (octx && (octx->region_type & ORT_TEAMS) != 0)
   18395              :         kind = OMP_CLAUSE_BIND_TEAMS;
   18396          329 :       else if (octx && (octx->region_type & ORT_PARALLEL) != 0)
   18397              :         kind = OMP_CLAUSE_BIND_PARALLEL;
   18398              :       else
   18399              :         {
   18400          188 :           for (; octx; octx = octx->outer_context)
   18401              :             {
   18402           80 :               if ((octx->region_type & ORT_ACC) != 0
   18403           80 :                   || octx->region_type == ORT_NONE
   18404           80 :                   || octx->region_type == ORT_IMPLICIT_TARGET)
   18405            0 :                 continue;
   18406              :               break;
   18407              :             }
   18408          188 :           if (octx == NULL && !in_omp_construct)
   18409            4 :             error_at (EXPR_LOCATION (for_stmt),
   18410              :                       "%<bind%> clause not specified on a %<loop%> "
   18411              :                       "construct not nested inside another OpenMP construct");
   18412              :         }
   18413          580 :       bind = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_BIND);
   18414          580 :       OMP_CLAUSE_CHAIN (bind) = clauses;
   18415          580 :       OMP_CLAUSE_BIND_KIND (bind) = kind;
   18416          580 :       OMP_FOR_CLAUSES (for_stmt) = bind;
   18417              :     }
   18418              :   else
   18419          495 :     switch (OMP_CLAUSE_BIND_KIND (bind))
   18420              :       {
   18421              :       case OMP_CLAUSE_BIND_THREAD:
   18422              :         break;
   18423          166 :       case OMP_CLAUSE_BIND_PARALLEL:
   18424          166 :         if (!flag_openmp) /* flag_openmp_simd */
   18425              :           {
   18426            0 :             OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
   18427            0 :             break;
   18428              :           }
   18429          240 :         for (; octx; octx = octx->outer_context)
   18430           78 :           if (octx->region_type == ORT_SIMD
   18431           78 :               && omp_find_clause (octx->clauses, OMP_CLAUSE_BIND) == NULL_TREE)
   18432              :             {
   18433            4 :               error_at (EXPR_LOCATION (for_stmt),
   18434              :                         "%<bind(parallel)%> on a %<loop%> construct nested "
   18435              :                         "inside %<simd%> construct");
   18436            4 :               OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
   18437            4 :               break;
   18438              :             }
   18439              :         kind = OMP_CLAUSE_BIND_PARALLEL;
   18440              :         break;
   18441          162 :       case OMP_CLAUSE_BIND_TEAMS:
   18442          162 :         if (!flag_openmp) /* flag_openmp_simd */
   18443              :           {
   18444            0 :             OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
   18445            0 :             break;
   18446              :           }
   18447          162 :         if ((octx
   18448          128 :              && octx->region_type != ORT_IMPLICIT_TARGET
   18449          111 :              && octx->region_type != ORT_NONE
   18450          111 :              && (octx->region_type & ORT_TEAMS) == 0)
   18451          110 :             || in_omp_construct)
   18452              :           {
   18453           64 :             error_at (EXPR_LOCATION (for_stmt),
   18454              :                       "%<bind(teams)%> on a %<loop%> region not strictly "
   18455              :                       "nested inside of a %<teams%> region");
   18456           64 :             OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
   18457           64 :             break;
   18458              :           }
   18459              :         kind = OMP_CLAUSE_BIND_TEAMS;
   18460              :         break;
   18461            0 :       default:
   18462            0 :         gcc_unreachable ();
   18463              :       }
   18464              : 
   18465         4352 :   for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
   18466         3277 :     switch (OMP_CLAUSE_CODE (*pc))
   18467              :       {
   18468          344 :       case OMP_CLAUSE_REDUCTION:
   18469          344 :         if (OMP_CLAUSE_REDUCTION_INSCAN (*pc))
   18470              :           {
   18471            4 :             error_at (OMP_CLAUSE_LOCATION (*pc),
   18472              :                       "%<inscan%> %<reduction%> clause on "
   18473              :                       "%qs construct", "loop");
   18474            4 :             OMP_CLAUSE_REDUCTION_INSCAN (*pc) = 0;
   18475              :           }
   18476          344 :         if (OMP_CLAUSE_REDUCTION_TASK (*pc))
   18477              :           {
   18478            8 :             error_at (OMP_CLAUSE_LOCATION (*pc),
   18479              :                       "invalid %<task%> reduction modifier on construct "
   18480              :                       "other than %<parallel%>, %qs or %<sections%>",
   18481            4 :                       lang_GNU_Fortran () ? "do" : "for");
   18482            4 :             OMP_CLAUSE_REDUCTION_TASK (*pc) = 0;
   18483              :           }
   18484          344 :         pc = &OMP_CLAUSE_CHAIN (*pc);
   18485          344 :         break;
   18486              :       case OMP_CLAUSE_LASTPRIVATE:
   18487          293 :         for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
   18488              :           {
   18489          289 :             tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
   18490          289 :             gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
   18491          289 :             if (OMP_CLAUSE_DECL (*pc) == TREE_OPERAND (t, 0))
   18492              :               break;
   18493           26 :             if (OMP_FOR_ORIG_DECLS (for_stmt)
   18494           25 :                 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
   18495              :                                             i)) == TREE_LIST
   18496           48 :                 && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
   18497              :                                                i)))
   18498              :               {
   18499           22 :                 tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
   18500           22 :                 if (OMP_CLAUSE_DECL (*pc) == TREE_PURPOSE (orig))
   18501              :                   break;
   18502              :               }
   18503              :           }
   18504          289 :         if (i == TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)))
   18505              :           {
   18506            4 :             error_at (OMP_CLAUSE_LOCATION (*pc),
   18507              :                       "%<lastprivate%> clause on a %<loop%> construct refers "
   18508              :                       "to a variable %qD which is not the loop iterator",
   18509            4 :                       OMP_CLAUSE_DECL (*pc));
   18510            4 :             *pc = OMP_CLAUSE_CHAIN (*pc);
   18511            4 :             break;
   18512              :           }
   18513          285 :         pc = &OMP_CLAUSE_CHAIN (*pc);
   18514          285 :         break;
   18515         2644 :       default:
   18516         2644 :         pc = &OMP_CLAUSE_CHAIN (*pc);
   18517         2644 :         break;
   18518              :     }
   18519              : 
   18520         1075 :   TREE_SET_CODE (for_stmt, OMP_SIMD);
   18521              : 
   18522         1075 :   int last;
   18523         1075 :   switch (kind)
   18524              :     {
   18525              :     case OMP_CLAUSE_BIND_THREAD: last = 0; break;
   18526          415 :     case OMP_CLAUSE_BIND_PARALLEL: last = 1; break;
   18527          224 :     case OMP_CLAUSE_BIND_TEAMS: last = 2; break;
   18528              :     }
   18529         1938 :   for (int pass = 1; pass <= last; pass++)
   18530              :     {
   18531          863 :       if (pass == 2)
   18532              :         {
   18533          224 :           tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL,
   18534              :                               make_node (BLOCK));
   18535          224 :           append_to_statement_list (*expr_p, &BIND_EXPR_BODY (bind));
   18536          224 :           *expr_p = make_node (OMP_PARALLEL);
   18537          224 :           TREE_TYPE (*expr_p) = void_type_node;
   18538          224 :           OMP_PARALLEL_BODY (*expr_p) = bind;
   18539          224 :           OMP_PARALLEL_COMBINED (*expr_p) = 1;
   18540          224 :           SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (for_stmt));
   18541          224 :           tree *pc = &OMP_PARALLEL_CLAUSES (*expr_p);
   18542          684 :           for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
   18543          236 :             if (OMP_FOR_ORIG_DECLS (for_stmt)
   18544          236 :                 && (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i))
   18545              :                     == TREE_LIST))
   18546              :               {
   18547           16 :                 tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
   18548           16 :                 if (TREE_PURPOSE (elt) && TREE_VALUE (elt))
   18549              :                   {
   18550           13 :                     *pc = build_omp_clause (UNKNOWN_LOCATION,
   18551              :                                             OMP_CLAUSE_FIRSTPRIVATE);
   18552           13 :                     OMP_CLAUSE_DECL (*pc) = TREE_VALUE (elt);
   18553           13 :                     pc = &OMP_CLAUSE_CHAIN (*pc);
   18554              :                   }
   18555              :               }
   18556              :         }
   18557          863 :       tree t = make_node (pass == 2 ? OMP_DISTRIBUTE : OMP_FOR);
   18558          863 :       tree *pc = &OMP_FOR_CLAUSES (t);
   18559          863 :       TREE_TYPE (t) = void_type_node;
   18560          863 :       OMP_FOR_BODY (t) = *expr_p;
   18561          863 :       SET_EXPR_LOCATION (t, EXPR_LOCATION (for_stmt));
   18562         3995 :       for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
   18563         3132 :         switch (OMP_CLAUSE_CODE (c))
   18564              :           {
   18565         2010 :           case OMP_CLAUSE_BIND:
   18566         2010 :           case OMP_CLAUSE_ORDER:
   18567         2010 :           case OMP_CLAUSE_COLLAPSE:
   18568         2010 :             *pc = copy_node (c);
   18569         2010 :             pc = &OMP_CLAUSE_CHAIN (*pc);
   18570         2010 :             break;
   18571              :           case OMP_CLAUSE_PRIVATE:
   18572              :           case OMP_CLAUSE_FIRSTPRIVATE:
   18573              :             /* Only needed on innermost.  */
   18574              :             break;
   18575          351 :           case OMP_CLAUSE_LASTPRIVATE:
   18576          351 :             if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) && pass != last)
   18577              :               {
   18578            7 :                 *pc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   18579              :                                         OMP_CLAUSE_FIRSTPRIVATE);
   18580            7 :                 OMP_CLAUSE_DECL (*pc) = OMP_CLAUSE_DECL (c);
   18581            7 :                 lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
   18582            7 :                 pc = &OMP_CLAUSE_CHAIN (*pc);
   18583              :               }
   18584          351 :             *pc = copy_node (c);
   18585          351 :             OMP_CLAUSE_LASTPRIVATE_STMT (*pc) = NULL_TREE;
   18586          351 :             TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
   18587          351 :             if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
   18588              :               {
   18589           22 :                 if (pass != last)
   18590            7 :                   OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (*pc) = 1;
   18591              :                 else
   18592           15 :                   lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
   18593           22 :                 OMP_CLAUSE_LASTPRIVATE_LOOP_IV (*pc) = 0;
   18594              :               }
   18595          351 :             pc = &OMP_CLAUSE_CHAIN (*pc);
   18596          351 :             break;
   18597          463 :           case OMP_CLAUSE_REDUCTION:
   18598          463 :             *pc = copy_node (c);
   18599          463 :             OMP_CLAUSE_DECL (*pc) = unshare_expr (OMP_CLAUSE_DECL (c));
   18600          463 :             TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
   18601          463 :             if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc))
   18602              :               {
   18603           15 :                 auto_vec<tree> no_context_vars;
   18604           15 :                 int walk_subtrees = 0;
   18605           15 :                 note_no_context_vars (&OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
   18606              :                                       &walk_subtrees, &no_context_vars);
   18607           15 :                 if (tree p = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c))
   18608            0 :                   note_no_context_vars (&p, &walk_subtrees, &no_context_vars);
   18609           15 :                 walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_INIT (c),
   18610              :                                               note_no_context_vars,
   18611              :                                               &no_context_vars);
   18612           15 :                 walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_MERGE (c),
   18613              :                                               note_no_context_vars,
   18614              :                                               &no_context_vars);
   18615              : 
   18616           15 :                 OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc)
   18617           15 :                   = copy_node (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c));
   18618           15 :                 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
   18619            0 :                   OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc)
   18620            0 :                     = copy_node (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c));
   18621              : 
   18622           15 :                 hash_map<tree, tree> decl_map;
   18623           15 :                 decl_map.put (OMP_CLAUSE_DECL (c), OMP_CLAUSE_DECL (c));
   18624           15 :                 decl_map.put (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
   18625           15 :                               OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc));
   18626           15 :                 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
   18627            0 :                   decl_map.put (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
   18628            0 :                                 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc));
   18629              : 
   18630           15 :                 copy_body_data id;
   18631           15 :                 memset (&id, 0, sizeof (id));
   18632           15 :                 id.src_fn = current_function_decl;
   18633           15 :                 id.dst_fn = current_function_decl;
   18634           15 :                 id.src_cfun = cfun;
   18635           15 :                 id.decl_map = &decl_map;
   18636           15 :                 id.copy_decl = copy_decl_no_change;
   18637           15 :                 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
   18638           15 :                 id.transform_new_cfg = true;
   18639           15 :                 id.transform_return_to_modify = false;
   18640           15 :                 id.eh_lp_nr = 0;
   18641           15 :                 walk_tree (&OMP_CLAUSE_REDUCTION_INIT (*pc), copy_tree_body_r,
   18642              :                            &id, NULL);
   18643           15 :                 walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (*pc), copy_tree_body_r,
   18644              :                            &id, NULL);
   18645              : 
   18646           60 :                 for (tree d : no_context_vars)
   18647              :                   {
   18648           15 :                     DECL_CONTEXT (d) = NULL_TREE;
   18649           15 :                     DECL_CONTEXT (*decl_map.get (d)) = NULL_TREE;
   18650              :                   }
   18651           15 :               }
   18652              :             else
   18653              :               {
   18654          448 :                 OMP_CLAUSE_REDUCTION_INIT (*pc)
   18655          448 :                   = unshare_expr (OMP_CLAUSE_REDUCTION_INIT (c));
   18656          448 :                 OMP_CLAUSE_REDUCTION_MERGE (*pc)
   18657          896 :                   = unshare_expr (OMP_CLAUSE_REDUCTION_MERGE (c));
   18658              :               }
   18659          463 :             pc = &OMP_CLAUSE_CHAIN (*pc);
   18660          463 :             break;
   18661            0 :           default:
   18662            0 :             gcc_unreachable ();
   18663              :           }
   18664          863 :       *pc = NULL_TREE;
   18665          863 :       *expr_p = t;
   18666              :     }
   18667         1075 :   return gimplify_expr (expr_p, pre_p, NULL, is_gimple_stmt, fb_none);
   18668              : }
   18669              : 
   18670              : 
   18671              : /* Helper function of optimize_target_teams, find OMP_TEAMS inside
   18672              :    of OMP_TARGET's body.  */
   18673              : 
   18674              : static tree
   18675       102328 : find_omp_teams (tree *tp, int *walk_subtrees, void *)
   18676              : {
   18677       102328 :   *walk_subtrees = 0;
   18678       102328 :   switch (TREE_CODE (*tp))
   18679              :     {
   18680              :     case OMP_TEAMS:
   18681              :       return *tp;
   18682        28736 :     case BIND_EXPR:
   18683        28736 :     case STATEMENT_LIST:
   18684        28736 :       *walk_subtrees = 1;
   18685        28736 :       break;
   18686              :     default:
   18687              :       break;
   18688              :     }
   18689              :   return NULL_TREE;
   18690              : }
   18691              : 
   18692              : /* Helper function of optimize_target_teams, determine if the expression
   18693              :    can be computed safely before the target construct on the host.  */
   18694              : 
   18695              : static tree
   18696         1194 : computable_teams_clause (tree *tp, int *walk_subtrees, void *)
   18697              : {
   18698         1722 :   splay_tree_node n;
   18699              : 
   18700         1722 :   if (TYPE_P (*tp))
   18701              :     {
   18702            0 :       *walk_subtrees = 0;
   18703            0 :       return NULL_TREE;
   18704              :     }
   18705         1722 :   switch (TREE_CODE (*tp))
   18706              :     {
   18707          866 :     case VAR_DECL:
   18708          866 :     case PARM_DECL:
   18709          866 :     case RESULT_DECL:
   18710          866 :       *walk_subtrees = 0;
   18711          866 :       if (error_operand_p (*tp)
   18712          866 :           || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
   18713          866 :           || DECL_HAS_VALUE_EXPR_P (*tp)
   18714          866 :           || DECL_THREAD_LOCAL_P (*tp)
   18715          866 :           || TREE_SIDE_EFFECTS (*tp)
   18716         1732 :           || TREE_THIS_VOLATILE (*tp))
   18717            0 :         return *tp;
   18718          866 :       if (is_global_var (*tp)
   18719          866 :           && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
   18720            0 :               || lookup_attribute ("omp declare target link",
   18721            0 :                                    DECL_ATTRIBUTES (*tp))))
   18722           16 :         return *tp;
   18723          850 :       if (VAR_P (*tp)
   18724          544 :           && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
   18725            0 :           && !is_global_var (*tp)
   18726          850 :           && decl_function_context (*tp) == current_function_decl)
   18727            0 :         return *tp;
   18728         1700 :       n = splay_tree_lookup (gimplify_omp_ctxp->variables,
   18729          850 :                              (splay_tree_key) *tp);
   18730          850 :       if (n == NULL)
   18731              :         {
   18732          210 :           if (gimplify_omp_ctxp->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
   18733              :             return NULL_TREE;
   18734            0 :           return *tp;
   18735              :         }
   18736          640 :       else if (n->value & GOVD_LOCAL)
   18737            0 :         return *tp;
   18738          640 :       else if (n->value & GOVD_FIRSTPRIVATE)
   18739              :         return NULL_TREE;
   18740          112 :       else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
   18741              :                == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
   18742              :         return NULL_TREE;
   18743           96 :       return *tp;
   18744           76 :     case INTEGER_CST:
   18745           76 :       if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
   18746            0 :         return *tp;
   18747              :       return NULL_TREE;
   18748          528 :     case TARGET_EXPR:
   18749          528 :       if (TARGET_EXPR_INITIAL (*tp)
   18750          528 :           || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
   18751              :         return *tp;
   18752          528 :       return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
   18753          528 :                                       walk_subtrees, NULL);
   18754              :     /* Allow some reasonable subset of integral arithmetics.  */
   18755          193 :     case PLUS_EXPR:
   18756          193 :     case MINUS_EXPR:
   18757          193 :     case MULT_EXPR:
   18758          193 :     case TRUNC_DIV_EXPR:
   18759          193 :     case CEIL_DIV_EXPR:
   18760          193 :     case FLOOR_DIV_EXPR:
   18761          193 :     case ROUND_DIV_EXPR:
   18762          193 :     case TRUNC_MOD_EXPR:
   18763          193 :     case CEIL_MOD_EXPR:
   18764          193 :     case FLOOR_MOD_EXPR:
   18765          193 :     case ROUND_MOD_EXPR:
   18766          193 :     case RDIV_EXPR:
   18767          193 :     case EXACT_DIV_EXPR:
   18768          193 :     case MIN_EXPR:
   18769          193 :     case MAX_EXPR:
   18770          193 :     case LSHIFT_EXPR:
   18771          193 :     case RSHIFT_EXPR:
   18772          193 :     case BIT_IOR_EXPR:
   18773          193 :     case BIT_XOR_EXPR:
   18774          193 :     case BIT_AND_EXPR:
   18775          193 :     case NEGATE_EXPR:
   18776          193 :     case ABS_EXPR:
   18777          193 :     case BIT_NOT_EXPR:
   18778          193 :     case NON_LVALUE_EXPR:
   18779          193 :     CASE_CONVERT:
   18780          193 :       if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
   18781            0 :         return *tp;
   18782              :       return NULL_TREE;
   18783              :     /* And disallow anything else, except for comparisons.  */
   18784           59 :     default:
   18785           59 :       if (COMPARISON_CLASS_P (*tp))
   18786            0 :         return NULL_TREE;
   18787              :       return *tp;
   18788              :     }
   18789              : }
   18790              : 
   18791              : /* Try to determine if the num_teams and/or thread_limit expressions
   18792              :    can have their values determined already before entering the
   18793              :    target construct.
   18794              :    INTEGER_CSTs trivially are,
   18795              :    integral decls that are firstprivate (explicitly or implicitly)
   18796              :    or explicitly map(always, to:) or map(always, tofrom:) on the target
   18797              :    region too, and expressions involving simple arithmetics on those
   18798              :    too, function calls are not ok, dereferencing something neither etc.
   18799              :    Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
   18800              :    EXPR based on what we find:
   18801              :    0 stands for clause not specified at all, use implementation default
   18802              :    -1 stands for value that can't be determined easily before entering
   18803              :       the target construct.
   18804              :    -2 means that no explicit teams construct was specified
   18805              :    If teams construct is not present at all, use 1 for num_teams
   18806              :    and 0 for thread_limit (only one team is involved, and the thread
   18807              :    limit is implementation defined.  */
   18808              : 
   18809              : static void
   18810        14596 : optimize_target_teams (tree target, gimple_seq *pre_p)
   18811              : {
   18812              :   /* If the NUM_TEAMS clause is on TARGET, assume that the front end already
   18813              :      took care of the target-consistent clauses.  Note that user code cannot
   18814              :      specify 'num_teams' on 'target', only on 'teams'.  */
   18815        14596 :   if (omp_find_clause (OMP_TARGET_CLAUSES (target), OMP_CLAUSE_NUM_TEAMS))
   18816          114 :     return;
   18817              : 
   18818        14482 :   tree body = OMP_BODY (target);
   18819        14482 :   tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
   18820        14482 :   tree num_teams_lower = NULL_TREE;
   18821        14482 :   tree num_teams_upper = integer_zero_node;
   18822        14482 :   tree thread_limit = integer_zero_node;
   18823        14482 :   location_t num_teams_loc = EXPR_LOCATION (target);
   18824        14482 :   location_t thread_limit_loc = EXPR_LOCATION (target);
   18825        14482 :   tree c, *p, expr;
   18826        14482 :   struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
   18827              : 
   18828        14482 :   if (teams == NULL_TREE)
   18829         7817 :     num_teams_upper = build_int_cst (integer_type_node, -2);
   18830              :   else
   18831        11089 :     for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
   18832              :       {
   18833         4424 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
   18834              :           {
   18835          560 :             p = &num_teams_upper;
   18836          560 :             num_teams_loc = OMP_CLAUSE_LOCATION (c);
   18837          560 :             if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c))
   18838              :               {
   18839          136 :                 expr = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
   18840          136 :                 if (TREE_CODE (expr) == INTEGER_CST)
   18841           18 :                   num_teams_lower = expr;
   18842          118 :                 else if (walk_tree (&expr, computable_teams_clause,
   18843              :                                     NULL, NULL))
   18844           12 :                   num_teams_lower = integer_minus_one_node;
   18845              :                 else
   18846              :                   {
   18847          106 :                     num_teams_lower = expr;
   18848          106 :                     gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
   18849          106 :                     if (gimplify_expr (&num_teams_lower, pre_p, NULL,
   18850              :                                        is_gimple_val, fb_rvalue, false)
   18851              :                         == GS_ERROR)
   18852              :                       {
   18853            0 :                         gimplify_omp_ctxp = target_ctx;
   18854            0 :                         num_teams_lower = integer_minus_one_node;
   18855              :                       }
   18856              :                     else
   18857              :                       {
   18858          106 :                         gimplify_omp_ctxp = target_ctx;
   18859          106 :                         if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
   18860           28 :                           OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
   18861           56 :                             = num_teams_lower;
   18862              :                       }
   18863              :                   }
   18864              :               }
   18865              :           }
   18866         3864 :         else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
   18867              :           {
   18868          419 :             p = &thread_limit;
   18869          419 :             thread_limit_loc = OMP_CLAUSE_LOCATION (c);
   18870              :           }
   18871              :         else
   18872         3445 :           continue;
   18873          979 :         expr = OMP_CLAUSE_OPERAND (c, 0);
   18874          979 :         if (TREE_CODE (expr) == INTEGER_CST)
   18875              :           {
   18876          172 :             *p = expr;
   18877          172 :             continue;
   18878              :           }
   18879          807 :         if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
   18880              :           {
   18881          159 :             *p = integer_minus_one_node;
   18882          159 :             continue;
   18883              :           }
   18884          648 :         *p = expr;
   18885          648 :         gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
   18886          648 :         if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
   18887              :             == GS_ERROR)
   18888              :           {
   18889            0 :             gimplify_omp_ctxp = target_ctx;
   18890            0 :             *p = integer_minus_one_node;
   18891            0 :             continue;
   18892              :           }
   18893          648 :         gimplify_omp_ctxp = target_ctx;
   18894          648 :         if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
   18895           48 :           OMP_CLAUSE_OPERAND (c, 0) = *p;
   18896              :       }
   18897        14482 :   if (!omp_find_clause (OMP_TARGET_CLAUSES (target), OMP_CLAUSE_THREAD_LIMIT))
   18898              :     {
   18899        14228 :       c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
   18900        14228 :       OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
   18901        14228 :       OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
   18902        14228 :       OMP_TARGET_CLAUSES (target) = c;
   18903              :     }
   18904        14482 :   c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
   18905        14482 :   OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = num_teams_upper;
   18906        14482 :   OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = num_teams_lower;
   18907        14482 :   OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
   18908        14482 :   OMP_TARGET_CLAUSES (target) = c;
   18909              : }
   18910              : 
   18911              : /* Gimplify the gross structure of several OMP constructs.  */
   18912              : 
   18913              : static void
   18914        41760 : gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
   18915              : {
   18916        41760 :   tree expr = *expr_p;
   18917        41760 :   gimple *stmt;
   18918        41760 :   gimple_seq body = NULL;
   18919        41760 :   enum omp_region_type ort;
   18920              : 
   18921        41760 :   switch (TREE_CODE (expr))
   18922              :     {
   18923              :     case OMP_SECTIONS:
   18924              :     case OMP_SINGLE:
   18925              :       ort = ORT_WORKSHARE;
   18926              :       break;
   18927          242 :     case OMP_SCOPE:
   18928          242 :       ort = ORT_TASKGROUP;
   18929          242 :       break;
   18930        14596 :     case OMP_TARGET:
   18931        14596 :       ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
   18932              :       break;
   18933         2543 :     case OACC_KERNELS:
   18934         2543 :       ort = ORT_ACC_KERNELS;
   18935         2543 :       break;
   18936         7901 :     case OACC_PARALLEL:
   18937         7901 :       ort = ORT_ACC_PARALLEL;
   18938         7901 :       break;
   18939         1040 :     case OACC_SERIAL:
   18940         1040 :       ort = ORT_ACC_SERIAL;
   18941         1040 :       break;
   18942         1950 :     case OACC_DATA:
   18943         1950 :       ort = ORT_ACC_DATA;
   18944         1950 :       break;
   18945         1875 :     case OMP_TARGET_DATA:
   18946         1875 :       ort = ORT_TARGET_DATA;
   18947         1875 :       break;
   18948         9556 :     case OMP_TEAMS:
   18949         9556 :       ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
   18950         9556 :       if (gimplify_omp_ctxp == NULL
   18951         6820 :           || gimplify_omp_ctxp->region_type == ORT_IMPLICIT_TARGET)
   18952         2736 :         ort = (enum omp_region_type) (ort | ORT_HOST_TEAMS);
   18953              :       break;
   18954          118 :     case OACC_HOST_DATA:
   18955          118 :       ort = ORT_ACC_HOST_DATA;
   18956          118 :       break;
   18957            0 :     default:
   18958            0 :       gcc_unreachable ();
   18959              :     }
   18960              : 
   18961        41760 :   gimple_seq iterator_loops_seq = NULL;
   18962        41760 :   if (TREE_CODE (expr) == OMP_TARGET || TREE_CODE (expr) == OMP_TARGET_DATA)
   18963              :     {
   18964        16471 :       remove_unused_omp_iterator_vars (&OMP_CLAUSES (expr));
   18965        16471 :       build_omp_iterators_loops (&OMP_CLAUSES (expr), &iterator_loops_seq);
   18966              :     }
   18967              : 
   18968        41760 :   bool save_in_omp_construct = in_omp_construct;
   18969        41760 :   if ((ort & ORT_ACC) == 0)
   18970        28208 :     in_omp_construct = false;
   18971        41760 :   gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
   18972        41760 :                              TREE_CODE (expr), &iterator_loops_seq);
   18973        41760 :   if (TREE_CODE (expr) == OMP_TARGET)
   18974        14596 :     optimize_target_teams (expr, pre_p);
   18975        41760 :   if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0
   18976        11737 :       || (ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
   18977              :     {
   18978        32759 :       push_gimplify_context ();
   18979        32759 :       gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
   18980        32759 :       if (gimple_code (g) == GIMPLE_BIND)
   18981        32759 :         pop_gimplify_context (g);
   18982              :       else
   18983            0 :         pop_gimplify_context (NULL);
   18984        32759 :       if ((ort & ORT_TARGET_DATA) != 0)
   18985              :         {
   18986         3943 :           enum built_in_function end_ix;
   18987         3943 :           switch (TREE_CODE (expr))
   18988              :             {
   18989              :             case OACC_DATA:
   18990              :             case OACC_HOST_DATA:
   18991              :               end_ix = BUILT_IN_GOACC_DATA_END;
   18992              :               break;
   18993         1875 :             case OMP_TARGET_DATA:
   18994         1875 :               end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
   18995         1875 :               break;
   18996            0 :             default:
   18997            0 :               gcc_unreachable ();
   18998              :             }
   18999         3943 :           tree fn = builtin_decl_explicit (end_ix);
   19000         3943 :           g = gimple_build_call (fn, 0);
   19001         3943 :           gimple_seq cleanup = NULL;
   19002         3943 :           gimple_seq_add_stmt (&cleanup, g);
   19003         3943 :           g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
   19004         3943 :           body = NULL;
   19005         3943 :           gimple_seq_add_stmt (&body, g);
   19006              :         }
   19007              :     }
   19008              :   else
   19009         9001 :     gimplify_and_add (OMP_BODY (expr), &body);
   19010        41760 :   gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
   19011        41760 :                                TREE_CODE (expr), &iterator_loops_seq);
   19012        41760 :   in_omp_construct = save_in_omp_construct;
   19013              : 
   19014        41760 :   switch (TREE_CODE (expr))
   19015              :     {
   19016         1950 :     case OACC_DATA:
   19017         3900 :       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
   19018         1950 :                                       OMP_CLAUSES (expr));
   19019         1950 :       break;
   19020          118 :     case OACC_HOST_DATA:
   19021          118 :       if (omp_find_clause (OMP_CLAUSES (expr), OMP_CLAUSE_IF_PRESENT))
   19022              :         {
   19023          128 :           for (tree c = OMP_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
   19024           95 :             if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR)
   19025           57 :               OMP_CLAUSE_USE_DEVICE_PTR_IF_PRESENT (c) = 1;
   19026              :         }
   19027              : 
   19028          236 :       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
   19029          118 :                                       OMP_CLAUSES (expr));
   19030          118 :       break;
   19031         2543 :     case OACC_KERNELS:
   19032         5086 :       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
   19033         2543 :                                       OMP_CLAUSES (expr));
   19034         2543 :       break;
   19035         7901 :     case OACC_PARALLEL:
   19036        15802 :       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
   19037         7901 :                                       OMP_CLAUSES (expr));
   19038         7901 :       break;
   19039         1040 :     case OACC_SERIAL:
   19040         2080 :       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_SERIAL,
   19041         1040 :                                       OMP_CLAUSES (expr));
   19042         1040 :       break;
   19043          634 :     case OMP_SECTIONS:
   19044          634 :       stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
   19045          634 :       break;
   19046         1305 :     case OMP_SINGLE:
   19047         1305 :       stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
   19048         1305 :       break;
   19049          242 :     case OMP_SCOPE:
   19050          242 :       stmt = gimple_build_omp_scope (body, OMP_CLAUSES (expr));
   19051          242 :       break;
   19052        14596 :     case OMP_TARGET:
   19053              :       /* Handle 'device_type(nohost)'.  */
   19054        14596 :       if (tree c = omp_find_clause (OMP_CLAUSES (expr), OMP_CLAUSE_DEVICE_TYPE))
   19055           20 :         if (OMP_CLAUSE_DEVICE_TYPE_KIND (c) == OMP_CLAUSE_DEVICE_TYPE_NOHOST)
   19056              :           {
   19057            9 :             location_t loc = gimple_location (body);
   19058            9 :             tree fn = builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
   19059            9 :             fn = build_call_expr_loc (loc, fn, 0);
   19060            9 :             tree err = builtin_decl_explicit (BUILT_IN_GOMP_ERROR);
   19061            9 :             const char *str = "Executing device-type 'nohost' target region "
   19062              :                               "on the host";
   19063            9 :             tree msg = build_string_literal (strlen (str) + 1, str);
   19064            9 :             tree msglen = build_int_cst (size_type_node, -2);
   19065            9 :             err = build_call_expr_loc (loc, err, 2, msg, msglen);
   19066            9 :             tree l1 = create_artificial_label (UNKNOWN_LOCATION);
   19067            9 :             tree l2 = create_artificial_label (UNKNOWN_LOCATION);
   19068            9 :             tree l3 = create_artificial_label (UNKNOWN_LOCATION);
   19069              : 
   19070            9 :             gimple_seq new_body = NULL;
   19071              : 
   19072            9 :             gimplify_expr (&fn, &new_body, NULL, is_gimple_val, fb_rvalue);
   19073            9 :             gcond *cond = gimple_build_cond (NE_EXPR, fn,
   19074            9 :                                              build_int_cst (TREE_TYPE (fn), 0),
   19075              :                                              l1, l2);
   19076            9 :             gimplify_seq_add_stmt (&new_body, cond);
   19077            9 :             gimplify_seq_add_stmt (&new_body, gimple_build_label (l1));
   19078            9 :             gimplify_and_add (err, &new_body);
   19079            9 :             gimplify_seq_add_stmt (&new_body, gimple_build_goto (l3));
   19080            9 :             gimplify_seq_add_stmt (&new_body, gimple_build_label (l2));
   19081            9 :             gimple_seq_add_seq (&new_body, body);
   19082            9 :             gimplify_seq_add_stmt (&new_body, gimple_build_label (l3));
   19083            9 :             body = gimple_build_bind (NULL_TREE, new_body, make_node (BLOCK));
   19084            9 :             gimple_set_location (body, loc);
   19085              :           }
   19086        29192 :       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
   19087        14596 :                                       OMP_CLAUSES (expr), iterator_loops_seq);
   19088        14596 :       break;
   19089         1875 :     case OMP_TARGET_DATA:
   19090              :       /* Put use_device_{ptr,addr} clauses last, as map clauses are supposed
   19091              :          to be evaluated before the use_device_{ptr,addr} clauses if they
   19092              :          refer to the same variables.  */
   19093         1875 :       {
   19094         1875 :         tree use_device_clauses;
   19095         1875 :         tree *pc, *uc = &use_device_clauses;
   19096         9974 :         for (pc = &OMP_CLAUSES (expr); *pc; )
   19097         8099 :           if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
   19098         8099 :               || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
   19099              :             {
   19100         1959 :               *uc = *pc;
   19101         1959 :               *pc = OMP_CLAUSE_CHAIN (*pc);
   19102         1959 :               uc = &OMP_CLAUSE_CHAIN (*uc);
   19103              :             }
   19104              :           else
   19105         6140 :             pc = &OMP_CLAUSE_CHAIN (*pc);
   19106         1875 :         *uc = NULL_TREE;
   19107         1875 :         *pc = use_device_clauses;
   19108         1875 :         stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
   19109         1875 :                                         OMP_CLAUSES (expr), iterator_loops_seq);
   19110              :       }
   19111         1875 :       break;
   19112         9556 :     case OMP_TEAMS:
   19113         9556 :       stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
   19114         9556 :       if ((ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
   19115         2736 :         gimple_omp_teams_set_host (as_a <gomp_teams *> (stmt), true);
   19116              :       break;
   19117            0 :     default:
   19118            0 :       gcc_unreachable ();
   19119              :     }
   19120              : 
   19121        41760 :   gimplify_seq_add_stmt (pre_p, stmt);
   19122        41760 :   *expr_p = NULL_TREE;
   19123        41760 : }
   19124              : 
   19125              : /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
   19126              :    target update constructs.  */
   19127              : 
   19128              : static void
   19129        12758 : gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
   19130              : {
   19131        12758 :   tree expr = *expr_p;
   19132        12758 :   int kind;
   19133        12758 :   gomp_target *stmt;
   19134        12758 :   enum omp_region_type ort = ORT_WORKSHARE;
   19135              : 
   19136        12758 :   switch (TREE_CODE (expr))
   19137              :     {
   19138              :     case OACC_ENTER_DATA:
   19139              :       kind = GF_OMP_TARGET_KIND_OACC_ENTER_DATA;
   19140              :       ort = ORT_ACC;
   19141              :       break;
   19142              :     case OACC_EXIT_DATA:
   19143              :       kind = GF_OMP_TARGET_KIND_OACC_EXIT_DATA;
   19144              :       ort = ORT_ACC;
   19145              :       break;
   19146              :     case OACC_UPDATE:
   19147              :       kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
   19148              :       ort = ORT_ACC;
   19149              :       break;
   19150              :     case OMP_TARGET_UPDATE:
   19151              :       kind = GF_OMP_TARGET_KIND_UPDATE;
   19152              :       break;
   19153              :     case OMP_TARGET_ENTER_DATA:
   19154              :       kind = GF_OMP_TARGET_KIND_ENTER_DATA;
   19155              :       break;
   19156              :     case OMP_TARGET_EXIT_DATA:
   19157              :       kind = GF_OMP_TARGET_KIND_EXIT_DATA;
   19158              :       break;
   19159            0 :     default:
   19160            0 :       gcc_unreachable ();
   19161              :     }
   19162              : 
   19163        12758 :   gimple_seq iterator_loops_seq = NULL;
   19164        12758 :   remove_unused_omp_iterator_vars (&OMP_STANDALONE_CLAUSES (expr));
   19165        12758 :   build_omp_iterators_loops (&OMP_STANDALONE_CLAUSES (expr),
   19166              :                              &iterator_loops_seq);
   19167              : 
   19168        12758 :   gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
   19169        12758 :                              ort, TREE_CODE (expr), &iterator_loops_seq);
   19170        12758 :   gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
   19171        12758 :                                TREE_CODE (expr), &iterator_loops_seq);
   19172        12758 :   if (TREE_CODE (expr) == OACC_UPDATE
   19173        12758 :       && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
   19174              :                           OMP_CLAUSE_IF_PRESENT))
   19175              :     {
   19176              :       /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
   19177              :          clause.  */
   19178          123 :       for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
   19179           97 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
   19180           47 :           switch (OMP_CLAUSE_MAP_KIND (c))
   19181              :             {
   19182           14 :             case GOMP_MAP_FORCE_TO:
   19183           14 :               OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
   19184           14 :               break;
   19185           24 :             case GOMP_MAP_FORCE_FROM:
   19186           24 :               OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
   19187           24 :               break;
   19188              :             default:
   19189              :               break;
   19190              :             }
   19191              :     }
   19192        12732 :   else if (TREE_CODE (expr) == OACC_EXIT_DATA
   19193        12732 :            && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
   19194              :                                OMP_CLAUSE_FINALIZE))
   19195              :     {
   19196              :       /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote "finalize"
   19197              :          semantics.  */
   19198           75 :       bool have_clause = false;
   19199          275 :       for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
   19200          200 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
   19201          117 :           switch (OMP_CLAUSE_MAP_KIND (c))
   19202              :             {
   19203           45 :             case GOMP_MAP_FROM:
   19204           45 :               OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
   19205           45 :               have_clause = true;
   19206           45 :               break;
   19207           47 :             case GOMP_MAP_RELEASE:
   19208           47 :               OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
   19209           47 :               have_clause = true;
   19210           47 :               break;
   19211              :             case GOMP_MAP_TO_PSET:
   19212              :               /* Fortran arrays with descriptors must map that descriptor when
   19213              :                  doing standalone "attach" operations (in OpenACC).  In that
   19214              :                  case GOMP_MAP_TO_PSET appears by itself with no preceding
   19215              :                  clause (see trans-openmp.cc:gfc_trans_omp_clauses).  */
   19216              :               break;
   19217            2 :             case GOMP_MAP_POINTER:
   19218              :               /* TODO PR92929: we may see these here, but they'll always follow
   19219              :                  one of the clauses above, and will be handled by libgomp as
   19220              :                  one group, so no handling required here.  */
   19221            2 :               gcc_assert (have_clause);
   19222              :               break;
   19223           22 :             case GOMP_MAP_DETACH:
   19224           22 :               OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_DETACH);
   19225           22 :               have_clause = false;
   19226           22 :               break;
   19227              :             case GOMP_MAP_STRUCT:
   19228              :             case GOMP_MAP_STRUCT_UNORD:
   19229          200 :               have_clause = false;
   19230              :               break;
   19231            0 :             default:
   19232            0 :               gcc_unreachable ();
   19233              :             }
   19234              :     }
   19235        12758 :   stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr),
   19236              :                                   iterator_loops_seq);
   19237              : 
   19238        12758 :   gimplify_seq_add_stmt (pre_p, stmt);
   19239        12758 :   *expr_p = NULL_TREE;
   19240        12758 : }
   19241              : 
   19242              : /* A subroutine of gimplify_omp_atomic.  The front end is supposed to have
   19243              :    stabilized the lhs of the atomic operation as *ADDR.  Return true if
   19244              :    EXPR is this stabilized form.  */
   19245              : 
   19246              : static bool
   19247        38617 : goa_lhs_expr_p (tree expr, tree addr)
   19248              : {
   19249              :   /* Also include casts to other type variants.  The C front end is fond
   19250              :      of adding these for e.g. volatile variables.  This is like
   19251              :      STRIP_TYPE_NOPS but includes the main variant lookup.  */
   19252        38617 :   STRIP_USELESS_TYPE_CONVERSION (expr);
   19253              : 
   19254        38617 :   if (INDIRECT_REF_P (expr))
   19255              :     {
   19256         5458 :       expr = TREE_OPERAND (expr, 0);
   19257         5458 :       while (expr != addr
   19258           92 :              && (CONVERT_EXPR_P (expr)
   19259           92 :                  || TREE_CODE (expr) == NON_LVALUE_EXPR)
   19260            0 :              && TREE_CODE (expr) == TREE_CODE (addr)
   19261         5458 :              && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
   19262              :         {
   19263            0 :           expr = TREE_OPERAND (expr, 0);
   19264            0 :           addr = TREE_OPERAND (addr, 0);
   19265              :         }
   19266         5458 :       if (expr == addr)
   19267              :         return true;
   19268           92 :       return (TREE_CODE (addr) == ADDR_EXPR
   19269           62 :               && TREE_CODE (expr) == ADDR_EXPR
   19270           92 :               && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
   19271              :     }
   19272        33159 :   if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
   19273              :     return true;
   19274              :   return false;
   19275              : }
   19276              : 
   19277              : /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR.  If an
   19278              :    expression does not involve the lhs, evaluate it into a temporary.
   19279              :    Return 1 if the lhs appeared as a subexpression, 0 if it did not,
   19280              :    or -1 if an error was encountered.  */
   19281              : 
   19282              : static int
   19283        38617 : goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
   19284              :                     tree lhs_var, tree &target_expr, bool rhs, int depth)
   19285              : {
   19286        38617 :   tree expr = *expr_p;
   19287        38617 :   int saw_lhs = 0;
   19288              : 
   19289        38617 :   if (goa_lhs_expr_p (expr, lhs_addr))
   19290              :     {
   19291         9653 :       if (pre_p)
   19292         8994 :         *expr_p = lhs_var;
   19293              :       return 1;
   19294              :     }
   19295        28964 :   if (is_gimple_val (expr))
   19296              :     return 0;
   19297              : 
   19298              :   /* Maximum depth of lhs in expression is for the
   19299              :      __builtin_clear_padding (...), __builtin_clear_padding (...),
   19300              :      __builtin_memcmp (&TARGET_EXPR <lhs, >, ...) == 0 ? ... : lhs;  */
   19301        18403 :   if (++depth > 7)
   19302           16 :     goto finish;
   19303              : 
   19304        18387 :   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
   19305              :     {
   19306        10199 :     case tcc_binary:
   19307        10199 :     case tcc_comparison:
   19308        10199 :       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
   19309              :                                      lhs_var, target_expr, true, depth);
   19310              :       /* FALLTHRU */
   19311        12322 :     case tcc_unary:
   19312        12322 :       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
   19313              :                                      lhs_var, target_expr, true, depth);
   19314        12322 :       break;
   19315         3063 :     case tcc_expression:
   19316         3063 :       switch (TREE_CODE (expr))
   19317              :         {
   19318          916 :         case TRUTH_ANDIF_EXPR:
   19319          916 :         case TRUTH_ORIF_EXPR:
   19320          916 :         case TRUTH_AND_EXPR:
   19321          916 :         case TRUTH_OR_EXPR:
   19322          916 :         case TRUTH_XOR_EXPR:
   19323          916 :         case BIT_INSERT_EXPR:
   19324          916 :           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
   19325              :                                          lhs_addr, lhs_var, target_expr, true,
   19326              :                                          depth);
   19327              :           /* FALLTHRU */
   19328          968 :         case TRUTH_NOT_EXPR:
   19329          968 :           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
   19330              :                                          lhs_addr, lhs_var, target_expr, true,
   19331              :                                          depth);
   19332          968 :           break;
   19333          550 :         case MODIFY_EXPR:
   19334          550 :           if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
   19335              :                                             target_expr, true, depth))
   19336              :             break;
   19337          544 :           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
   19338              :                                          lhs_addr, lhs_var, target_expr, true,
   19339              :                                          depth);
   19340          544 :           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
   19341              :                                          lhs_addr, lhs_var, target_expr, false,
   19342              :                                          depth);
   19343          544 :           break;
   19344              :           /* FALLTHRU */
   19345          164 :         case ADDR_EXPR:
   19346          164 :           if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
   19347              :                                             target_expr, true, depth))
   19348              :             break;
   19349          138 :           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
   19350              :                                          lhs_addr, lhs_var, target_expr, false,
   19351              :                                          depth);
   19352          138 :           break;
   19353              :         case COMPOUND_EXPR:
   19354              :           /* Break out any preevaluations from cp_build_modify_expr.  */
   19355           76 :           for (; TREE_CODE (expr) == COMPOUND_EXPR;
   19356           38 :                expr = TREE_OPERAND (expr, 1))
   19357              :             {
   19358              :               /* Special-case __builtin_clear_padding call before
   19359              :                  __builtin_memcmp.  */
   19360           38 :               if (TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR)
   19361              :                 {
   19362            2 :                   tree fndecl = get_callee_fndecl (TREE_OPERAND (expr, 0));
   19363            2 :                   if (fndecl
   19364            2 :                       && fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING)
   19365            0 :                       && VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
   19366            2 :                       && (!pre_p
   19367            0 :                           || goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL,
   19368              :                                                  lhs_addr, lhs_var,
   19369              :                                                  target_expr, true, depth)))
   19370              :                     {
   19371            0 :                       if (pre_p)
   19372            0 :                         *expr_p = expr;
   19373            0 :                       saw_lhs = goa_stabilize_expr (&TREE_OPERAND (expr, 0),
   19374              :                                                     pre_p, lhs_addr, lhs_var,
   19375              :                                                     target_expr, true, depth);
   19376            0 :                       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1),
   19377              :                                                      pre_p, lhs_addr, lhs_var,
   19378              :                                                      target_expr, rhs, depth);
   19379            0 :                       return saw_lhs;
   19380              :                     }
   19381              :                 }
   19382              : 
   19383           38 :               if (pre_p)
   19384           35 :                 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
   19385              :             }
   19386           38 :           if (!pre_p)
   19387            3 :             return goa_stabilize_expr (&expr, pre_p, lhs_addr, lhs_var,
   19388            3 :                                        target_expr, rhs, depth);
   19389           35 :           *expr_p = expr;
   19390           35 :           return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var,
   19391           35 :                                      target_expr, rhs, depth);
   19392          442 :         case COND_EXPR:
   19393          442 :           if (!goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL, lhs_addr,
   19394              :                                    lhs_var, target_expr, true, depth))
   19395              :             break;
   19396          429 :           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
   19397              :                                          lhs_addr, lhs_var, target_expr, true,
   19398              :                                          depth);
   19399          429 :           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
   19400              :                                          lhs_addr, lhs_var, target_expr, true,
   19401              :                                          depth);
   19402          429 :           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 2), pre_p,
   19403              :                                          lhs_addr, lhs_var, target_expr, true,
   19404              :                                          depth);
   19405          429 :           break;
   19406          880 :         case TARGET_EXPR:
   19407          880 :           if (TARGET_EXPR_INITIAL (expr))
   19408              :             {
   19409          880 :               if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr,
   19410              :                                                 lhs_var, target_expr, true,
   19411              :                                                 depth))
   19412              :                 break;
   19413          702 :               if (expr == target_expr)
   19414              :                 saw_lhs = 1;
   19415              :               else
   19416              :                 {
   19417          702 :                   saw_lhs = goa_stabilize_expr (&TARGET_EXPR_INITIAL (expr),
   19418              :                                                 pre_p, lhs_addr, lhs_var,
   19419              :                                                 target_expr, true, depth);
   19420          702 :                   if (saw_lhs && target_expr == NULL_TREE && pre_p)
   19421           26 :                     target_expr = expr;
   19422              :                 }
   19423              :             }
   19424              :           break;
   19425              :         default:
   19426              :           break;
   19427              :         }
   19428              :       break;
   19429          565 :     case tcc_reference:
   19430          565 :       if (TREE_CODE (expr) == BIT_FIELD_REF
   19431          499 :           || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
   19432          362 :         saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
   19433              :                                        lhs_addr, lhs_var, target_expr, true,
   19434              :                                        depth);
   19435              :       break;
   19436           58 :     case tcc_vl_exp:
   19437           58 :       if (TREE_CODE (expr) == CALL_EXPR)
   19438              :         {
   19439           58 :           if (tree fndecl = get_callee_fndecl (expr))
   19440           58 :             if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING,
   19441              :                                            BUILT_IN_MEMCMP))
   19442              :               {
   19443           56 :                 int nargs = call_expr_nargs (expr);
   19444          224 :                 for (int i = 0; i < nargs; i++)
   19445          168 :                   saw_lhs |= goa_stabilize_expr (&CALL_EXPR_ARG (expr, i),
   19446              :                                                  pre_p, lhs_addr, lhs_var,
   19447              :                                                  target_expr, true, depth);
   19448              :               }
   19449              :         }
   19450              :       break;
   19451              :     default:
   19452              :       break;
   19453              :     }
   19454              : 
   19455        18365 :  finish:
   19456        18365 :   if (saw_lhs == 0 && pre_p)
   19457              :     {
   19458         3986 :       enum gimplify_status gs;
   19459         3986 :       if (TREE_CODE (expr) == CALL_EXPR && VOID_TYPE_P (TREE_TYPE (expr)))
   19460              :         {
   19461            0 :           gimplify_stmt (&expr, pre_p);
   19462            0 :           return saw_lhs;
   19463              :         }
   19464         3986 :       else if (rhs)
   19465         3808 :         gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
   19466              :       else
   19467          178 :         gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_lvalue, fb_lvalue);
   19468         3986 :       if (gs != GS_ALL_DONE)
   19469        38617 :         saw_lhs = -1;
   19470              :     }
   19471              : 
   19472              :   return saw_lhs;
   19473              : }
   19474              : 
   19475              : /* Gimplify an OMP_ATOMIC statement.  */
   19476              : 
   19477              : static enum gimplify_status
   19478        10744 : gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
   19479              : {
   19480        10744 :   tree addr = TREE_OPERAND (*expr_p, 0);
   19481        10744 :   tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
   19482        10744 :              ? NULL : TREE_OPERAND (*expr_p, 1);
   19483        10744 :   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
   19484        10744 :   tree tmp_load;
   19485        10744 :   gomp_atomic_load *loadstmt;
   19486        10744 :   gomp_atomic_store *storestmt;
   19487        10744 :   tree target_expr = NULL_TREE;
   19488              : 
   19489        10744 :   tmp_load = create_tmp_reg (type);
   19490        10744 :   if (rhs
   19491        10744 :       && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load, target_expr,
   19492              :                              true, 0) < 0)
   19493              :     return GS_ERROR;
   19494              : 
   19495        10744 :   if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
   19496              :       != GS_ALL_DONE)
   19497              :     return GS_ERROR;
   19498              : 
   19499        10744 :   loadstmt = gimple_build_omp_atomic_load (tmp_load, addr,
   19500        10744 :                                            OMP_ATOMIC_MEMORY_ORDER (*expr_p));
   19501        10744 :   gimplify_seq_add_stmt (pre_p, loadstmt);
   19502        10744 :   if (rhs)
   19503              :     {
   19504              :       /* BIT_INSERT_EXPR is not valid for non-integral bitfield
   19505              :          representatives.  Use BIT_FIELD_REF on the lhs instead.  */
   19506         9547 :       tree rhsarg = rhs;
   19507         9547 :       if (TREE_CODE (rhs) == COND_EXPR)
   19508          429 :         rhsarg = TREE_OPERAND (rhs, 1);
   19509         9547 :       if (TREE_CODE (rhsarg) == BIT_INSERT_EXPR
   19510         9547 :           && !INTEGRAL_TYPE_P (TREE_TYPE (tmp_load)))
   19511              :         {
   19512           32 :           tree bitpos = TREE_OPERAND (rhsarg, 2);
   19513           32 :           tree op1 = TREE_OPERAND (rhsarg, 1);
   19514           32 :           tree bitsize;
   19515           32 :           tree tmp_store = tmp_load;
   19516           32 :           if (TREE_CODE (*expr_p) == OMP_ATOMIC_CAPTURE_OLD)
   19517           12 :             tmp_store = get_initialized_tmp_var (tmp_load, pre_p);
   19518           32 :           if (INTEGRAL_TYPE_P (TREE_TYPE (op1)))
   19519           32 :             bitsize = bitsize_int (TYPE_PRECISION (TREE_TYPE (op1)));
   19520              :           else
   19521            0 :             bitsize = TYPE_SIZE (TREE_TYPE (op1));
   19522           32 :           gcc_assert (TREE_OPERAND (rhsarg, 0) == tmp_load);
   19523           32 :           tree t = build2_loc (EXPR_LOCATION (rhsarg),
   19524              :                                MODIFY_EXPR, void_type_node,
   19525           32 :                                build3_loc (EXPR_LOCATION (rhsarg),
   19526           32 :                                            BIT_FIELD_REF, TREE_TYPE (op1),
   19527              :                                            tmp_store, bitsize, bitpos), op1);
   19528           32 :           if (TREE_CODE (rhs) == COND_EXPR)
   19529           16 :             t = build3_loc (EXPR_LOCATION (rhs), COND_EXPR, void_type_node,
   19530           16 :                             TREE_OPERAND (rhs, 0), t, void_node);
   19531           32 :           gimplify_and_add (t, pre_p);
   19532           32 :           rhs = tmp_store;
   19533              :         }
   19534         9547 :       bool save_allow_rhs_cond_expr = gimplify_ctxp->allow_rhs_cond_expr;
   19535         9547 :       if (TREE_CODE (rhs) == COND_EXPR)
   19536          413 :         gimplify_ctxp->allow_rhs_cond_expr = true;
   19537         9547 :       enum gimplify_status gs = gimplify_expr (&rhs, pre_p, NULL,
   19538              :                                                is_gimple_val, fb_rvalue);
   19539         9547 :       gimplify_ctxp->allow_rhs_cond_expr = save_allow_rhs_cond_expr;
   19540         9547 :       if (gs != GS_ALL_DONE)
   19541              :         return GS_ERROR;
   19542              :     }
   19543              : 
   19544        10744 :   if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
   19545         1197 :     rhs = tmp_load;
   19546        10744 :   storestmt
   19547        10744 :     = gimple_build_omp_atomic_store (rhs, OMP_ATOMIC_MEMORY_ORDER (*expr_p));
   19548        10744 :   if (TREE_CODE (*expr_p) != OMP_ATOMIC_READ && OMP_ATOMIC_WEAK (*expr_p))
   19549              :     {
   19550           38 :       gimple_omp_atomic_set_weak (loadstmt);
   19551           38 :       gimple_omp_atomic_set_weak (storestmt);
   19552              :     }
   19553        10744 :   gimplify_seq_add_stmt (pre_p, storestmt);
   19554        10744 :   switch (TREE_CODE (*expr_p))
   19555              :     {
   19556         2085 :     case OMP_ATOMIC_READ:
   19557         2085 :     case OMP_ATOMIC_CAPTURE_OLD:
   19558         2085 :       *expr_p = tmp_load;
   19559         2085 :       gimple_omp_atomic_set_need_value (loadstmt);
   19560         2085 :       break;
   19561          868 :     case OMP_ATOMIC_CAPTURE_NEW:
   19562          868 :       *expr_p = rhs;
   19563          868 :       gimple_omp_atomic_set_need_value (storestmt);
   19564          868 :       break;
   19565         7791 :     default:
   19566         7791 :       *expr_p = NULL;
   19567         7791 :       break;
   19568              :     }
   19569              : 
   19570              :   return GS_ALL_DONE;
   19571              : }
   19572              : 
   19573              : /* Gimplify a TRANSACTION_EXPR.  This involves gimplification of the
   19574              :    body, and adding some EH bits.  */
   19575              : 
   19576              : static enum gimplify_status
   19577          478 : gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
   19578              : {
   19579          478 :   tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
   19580          478 :   gimple *body_stmt;
   19581          478 :   gtransaction *trans_stmt;
   19582          478 :   gimple_seq body = NULL;
   19583          478 :   int subcode = 0;
   19584              : 
   19585              :   /* Wrap the transaction body in a BIND_EXPR so we have a context
   19586              :      where to put decls for OMP.  */
   19587          478 :   if (TREE_CODE (tbody) != BIND_EXPR)
   19588              :     {
   19589          439 :       tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
   19590          439 :       TREE_SIDE_EFFECTS (bind) = 1;
   19591          439 :       SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
   19592          439 :       TRANSACTION_EXPR_BODY (expr) = bind;
   19593              :     }
   19594              : 
   19595          478 :   push_gimplify_context ();
   19596          478 :   temp = voidify_wrapper_expr (*expr_p, NULL);
   19597              : 
   19598          478 :   body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
   19599          478 :   pop_gimplify_context (body_stmt);
   19600              : 
   19601          478 :   trans_stmt = gimple_build_transaction (body);
   19602          478 :   if (TRANSACTION_EXPR_OUTER (expr))
   19603              :     subcode = GTMA_IS_OUTER;
   19604          447 :   else if (TRANSACTION_EXPR_RELAXED (expr))
   19605           81 :     subcode = GTMA_IS_RELAXED;
   19606          478 :   gimple_transaction_set_subcode (trans_stmt, subcode);
   19607              : 
   19608          478 :   gimplify_seq_add_stmt (pre_p, trans_stmt);
   19609              : 
   19610          478 :   if (temp)
   19611              :     {
   19612           76 :       *expr_p = temp;
   19613           76 :       return GS_OK;
   19614              :     }
   19615              : 
   19616              :   *expr_p = NULL_TREE;
   19617              :   return GS_ALL_DONE;
   19618              : }
   19619              : 
   19620              : /* Gimplify an OMP_ORDERED construct.  EXPR is the tree version.  BODY
   19621              :    is the OMP_BODY of the original EXPR (which has already been
   19622              :    gimplified so it's not present in the EXPR).
   19623              : 
   19624              :    Return the gimplified GIMPLE_OMP_ORDERED tuple.  */
   19625              : 
   19626              : static gimple *
   19627         1954 : gimplify_omp_ordered (tree expr, gimple_seq body)
   19628              : {
   19629         1954 :   tree c, decls;
   19630         1954 :   int failures = 0;
   19631         1954 :   unsigned int i;
   19632         1954 :   tree source_c = NULL_TREE;
   19633         1954 :   tree sink_c = NULL_TREE;
   19634              : 
   19635         1954 :   if (gimplify_omp_ctxp)
   19636              :     {
   19637         3291 :       for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
   19638         1515 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
   19639         1515 :             && gimplify_omp_ctxp->loop_iter_var.is_empty ())
   19640              :           {
   19641           74 :             error_at (OMP_CLAUSE_LOCATION (c),
   19642              :                       "%<ordered%> construct with %qs clause must be "
   19643              :                       "closely nested inside a loop with %<ordered%> clause",
   19644           74 :                       OMP_CLAUSE_DOACROSS_DEPEND (c) ? "depend" : "doacross");
   19645           74 :             failures++;
   19646              :           }
   19647         1441 :         else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
   19648         1441 :                  && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
   19649              :           {
   19650          642 :             bool fail = false;
   19651          642 :             sink_c = c;
   19652          642 :             if (OMP_CLAUSE_DECL (c) == NULL_TREE)
   19653           72 :               continue;  /* omp_cur_iteration - 1 */
   19654          570 :             for (decls = OMP_CLAUSE_DECL (c), i = 0;
   19655         3201 :                  decls && TREE_CODE (decls) == TREE_LIST;
   19656         2631 :                  decls = TREE_CHAIN (decls), ++i)
   19657         2631 :               if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
   19658            4 :                 continue;
   19659         5254 :               else if (TREE_VALUE (decls)
   19660         2627 :                        != gimplify_omp_ctxp->loop_iter_var[2 * i])
   19661              :                 {
   19662            8 :                   error_at (OMP_CLAUSE_LOCATION (c),
   19663              :                             "variable %qE is not an iteration "
   19664              :                             "of outermost loop %d, expected %qE",
   19665            8 :                             TREE_VALUE (decls), i + 1,
   19666            8 :                             gimplify_omp_ctxp->loop_iter_var[2 * i]);
   19667            8 :                   fail = true;
   19668            8 :                   failures++;
   19669              :                 }
   19670              :               else
   19671         5238 :                 TREE_VALUE (decls)
   19672         2619 :                   = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
   19673         1136 :             if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
   19674              :               {
   19675           16 :                 error_at (OMP_CLAUSE_LOCATION (c),
   19676              :                           "number of variables in %qs clause with "
   19677              :                           "%<sink%> modifier does not match number of "
   19678              :                           "iteration variables",
   19679           16 :                           OMP_CLAUSE_DOACROSS_DEPEND (c)
   19680              :                           ? "depend" : "doacross");
   19681           16 :                 failures++;
   19682              :               }
   19683              :           }
   19684          799 :         else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
   19685          799 :                  && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SOURCE)
   19686              :           {
   19687          475 :             if (source_c)
   19688              :               {
   19689            4 :                 error_at (OMP_CLAUSE_LOCATION (c),
   19690              :                           "more than one %qs clause with %<source%> "
   19691              :                           "modifier on an %<ordered%> construct",
   19692            4 :                           OMP_CLAUSE_DOACROSS_DEPEND (source_c)
   19693              :                           ? "depend" : "doacross");
   19694            4 :                 failures++;
   19695              :               }
   19696              :             else
   19697              :               source_c = c;
   19698              :           }
   19699              :     }
   19700         1954 :   if (source_c && sink_c)
   19701              :     {
   19702            4 :       error_at (OMP_CLAUSE_LOCATION (source_c),
   19703              :                 "%qs clause with %<source%> modifier specified "
   19704              :                 "together with %qs clauses with %<sink%> modifier "
   19705              :                 "on the same construct",
   19706            4 :                 OMP_CLAUSE_DOACROSS_DEPEND (source_c) ? "depend" : "doacross",
   19707            4 :                 OMP_CLAUSE_DOACROSS_DEPEND (sink_c) ? "depend" : "doacross");
   19708            4 :       failures++;
   19709              :     }
   19710              : 
   19711         1954 :   if (failures)
   19712          102 :     return gimple_build_nop ();
   19713         1852 :   return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
   19714              : }
   19715              : 
   19716              : /* Gimplify an OMP_INTEROP statement.  */
   19717              : 
   19718              : static enum gimplify_status
   19719          612 : gimplify_omp_interop (tree *expr_p, gimple_seq *pre_p)
   19720              : {
   19721          612 :   tree expr = *expr_p;
   19722              : 
   19723          612 :   gimplify_scan_omp_clauses (&OMP_INTEROP_CLAUSES (expr), pre_p, ORT_TASK,
   19724              :                              OMP_INTEROP);
   19725          612 :   gimple *stmt = gimple_build_omp_interop (OMP_INTEROP_CLAUSES (expr));
   19726          612 :   gimplify_seq_add_stmt (pre_p, stmt);
   19727          612 :   *expr_p = NULL_TREE;
   19728          612 :   return GS_ALL_DONE;
   19729              : }
   19730              : 
   19731              : /* Callback for walk_tree to find an IFN_GOMP_DISPATCH.  */
   19732              : 
   19733              : static tree
   19734         3100 : find_ifn_gomp_dispatch (tree *tp, int *, void *modify)
   19735              : {
   19736         3100 :   tree t = *tp;
   19737              : 
   19738         3100 :   if (TREE_CODE (t) == CALL_EXPR && CALL_EXPR_IFN (t) == IFN_GOMP_DISPATCH)
   19739          852 :     return *(tree *) modify ? *(tree *) modify : *tp;
   19740              : 
   19741         2248 :   if (TREE_CODE (t) == MODIFY_EXPR)
   19742          513 :     *(tree *) modify = *tp;
   19743              : 
   19744              :   return NULL_TREE;
   19745              : }
   19746              : 
   19747              : /* Gimplify an OMP_DISPATCH construct.  */
   19748              : 
   19749              : static enum gimplify_status
   19750          852 : gimplify_omp_dispatch (tree *expr_p, gimple_seq *pre_p)
   19751              : {
   19752          852 :   tree expr = *expr_p;
   19753          852 :   gimple_seq body = NULL;
   19754              : 
   19755          852 :   gimplify_scan_omp_clauses (&OMP_DISPATCH_CLAUSES (expr), pre_p, ORT_DISPATCH,
   19756              :                              OMP_DISPATCH);
   19757          852 :   push_gimplify_context ();
   19758              : 
   19759              :   // If device clause, adjust ICV
   19760          852 :   tree device
   19761          852 :     = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_DEVICE);
   19762              :   // If no device clause exists but an interop clause with a single list
   19763              :   // item, use it to obtain the device number.
   19764          852 :   if (device)
   19765          272 :     device = OMP_CLAUSE_DEVICE_ID (device);
   19766              :   else
   19767              :     {
   19768          580 :       tree first_interop_obj
   19769          580 :         = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_INTEROP);
   19770          580 :       if (first_interop_obj)
   19771           96 :         for (tree c = TREE_CHAIN (first_interop_obj); c; c = TREE_CHAIN (c))
   19772            8 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INTEROP)
   19773              :             {
   19774              :               first_interop_obj = NULL_TREE;
   19775              :               break;
   19776              :             }
   19777           95 :       if (first_interop_obj)
   19778              :         {
   19779           88 :           device = create_tmp_var (integer_type_node);
   19780           88 :           tree c = build_omp_clause (OMP_CLAUSE_LOCATION (first_interop_obj),
   19781              :                                      OMP_CLAUSE_DEVICE);
   19782           88 :           OMP_CLAUSE_DEVICE_ID (c) = device;
   19783           88 :           TREE_CHAIN (c) = TREE_CHAIN (first_interop_obj);
   19784           88 :           TREE_CHAIN (first_interop_obj) = c;
   19785           88 :           first_interop_obj = OMP_CLAUSE_DECL (first_interop_obj);
   19786              :           /* device = omp_get_interop_int (obj, omp_ipr_device_num, NULL);  */
   19787           88 :           tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_INTEROP_INT);
   19788           88 :           fn = build_call_expr (fn, 3, first_interop_obj,
   19789              :                                 build_int_cst (integer_type_node, -5),
   19790              :                                 null_pointer_node);
   19791           88 :           gimplify_assign (device, fold_convert (integer_type_node, fn), &body);
   19792              :         }
   19793              :     }
   19794          852 :   tree saved_device_icv = NULL_TREE;
   19795          852 :   if (device
   19796          852 :       && (TREE_CODE (device) != INTEGER_CST
   19797          852 :           || !wi::eq_p (wi::to_wide (device), -1 /* omp_initial_device */)))
   19798              :     {
   19799              :       // Save current default-device-var ICV
   19800          360 :       saved_device_icv = create_tmp_var (integer_type_node);
   19801          360 :       tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
   19802          360 :       gcall *call = gimple_build_call (fn, 0);
   19803          360 :       gimple_call_set_lhs (call, saved_device_icv);
   19804          360 :       gimplify_seq_add_stmt (&body, call);
   19805              : 
   19806              :       // Set default device
   19807          360 :       fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
   19808          360 :       call = gimple_build_call (fn, 1, device);
   19809          360 :       gimplify_seq_add_stmt (&body, call);
   19810              :     }
   19811              : 
   19812              :   // If the novariants and nocontext clauses are not compile-time constants,
   19813              :   // we need to generate code for all possible cases:
   19814              :   //   if (novariants) // implies nocontext
   19815              :   //       base()
   19816              :   //   else if (nocontext)
   19817              :   //       variant1()
   19818              :   //   else
   19819              :   //       variant2()
   19820          852 :   tree *dispatch_body_p = &OMP_DISPATCH_BODY (expr);
   19821          852 :   if (TREE_CODE (*dispatch_body_p) == BIND_EXPR)
   19822           28 :     dispatch_body_p = &BIND_EXPR_BODY (*dispatch_body_p);
   19823          852 :   tree dispatch_body = *dispatch_body_p;
   19824              : 
   19825              :   // Look for IFN_GOMP_DISPATCH and extract the base function call
   19826          852 :   tree base_call_expr = NULL_TREE;
   19827          852 :   if (TREE_CODE (dispatch_body) == STATEMENT_LIST)
   19828          243 :     for (tree_stmt_iterator tsi = tsi_start (dispatch_body); !tsi_end_p (tsi);
   19829          187 :          tsi_next (&tsi))
   19830              :       {
   19831          243 :         tree modify = NULL_TREE;
   19832          243 :         tree stmt = tsi_stmt (tsi);
   19833          243 :         base_call_expr
   19834          243 :           = walk_tree (&stmt, find_ifn_gomp_dispatch, &modify, NULL);
   19835          243 :         if (base_call_expr != NULL_TREE)
   19836              :           break;
   19837              :       }
   19838              :   else
   19839              :     {
   19840          796 :       tree modify = NULL_TREE;
   19841          796 :       base_call_expr
   19842          796 :         = walk_tree (dispatch_body_p, find_ifn_gomp_dispatch, &modify, NULL);
   19843              :     }
   19844          852 :   gcc_assert (base_call_expr != NULL_TREE);
   19845              : 
   19846          852 :   tree dst = NULL_TREE;
   19847          852 :   if (TREE_CODE (base_call_expr) == MODIFY_EXPR)
   19848              :     {
   19849          290 :       dst = TREE_OPERAND (base_call_expr, 0);
   19850          290 :       base_call_expr = TREE_OPERAND (base_call_expr, 1);
   19851              :     }
   19852              : 
   19853          880 :   while (TREE_CODE (base_call_expr) == FLOAT_EXPR
   19854              :          || TREE_CODE (base_call_expr) == CONVERT_EXPR
   19855              :          || TREE_CODE (base_call_expr) == COMPLEX_EXPR
   19856              :          || TREE_CODE (base_call_expr) == INDIRECT_REF
   19857          880 :          || TREE_CODE (base_call_expr) == NOP_EXPR)
   19858           28 :     base_call_expr = TREE_OPERAND (base_call_expr, 0);
   19859              : 
   19860          852 :   gcc_assert (CALL_EXPR_IFN (base_call_expr) == IFN_GOMP_DISPATCH);
   19861          852 :   base_call_expr = CALL_EXPR_ARG (base_call_expr, 0);
   19862              : 
   19863          852 :   tree base_fndecl = get_callee_fndecl (base_call_expr);
   19864          852 :   if (base_fndecl != NULL_TREE)
   19865              :     {
   19866          844 :       if (DECL_VIRTUAL_P (base_fndecl))
   19867              :         {
   19868            6 :           error_at (
   19869            3 :             EXPR_LOCATION (base_call_expr),
   19870              :             "%qD is a virtual function but only a direct call is allowed "
   19871              :             "in a dispatch construct",
   19872            3 :             DECL_NAME (base_fndecl));
   19873              :         }
   19874              : 
   19875              :       /* We are not actually going to expand the variant call or use
   19876              :          the result of omp_get_dynamic candidates here; only check that
   19877              :          it does not trivially resolve to a call to the base function
   19878              :          so that we can avoid some extra work in building code that's
   19879              :          not needed in that case.  */
   19880          844 :       tree construct_context = omp_get_construct_context ();
   19881          844 :       vec<struct omp_variant> all_candidates
   19882          844 :         = omp_declare_variant_candidates (base_fndecl, construct_context);
   19883          844 :       gcc_assert (!all_candidates.is_empty ());
   19884          844 :       vec<struct omp_variant> candidates
   19885          844 :         = omp_get_dynamic_candidates (all_candidates, construct_context);
   19886          844 :       tree variant_fndecl
   19887         1676 :         = (candidates.length () == 1 ? candidates[0].alternative : NULL_TREE);
   19888              : 
   19889          844 :       if (base_fndecl != variant_fndecl
   19890          844 :           && (omp_has_novariants () == -1 || omp_has_nocontext () == -1))
   19891              :         {
   19892           22 :           tree novariants_clause = NULL_TREE, nocontext_clause = NULL_TREE,
   19893           22 :                novariants_cond = NULL_TREE, nocontext_cond = NULL_TREE;
   19894           59 :           for (tree c = OMP_DISPATCH_CLAUSES (expr); c; c = TREE_CHAIN (c))
   19895              :             {
   19896           37 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOVARIANTS
   19897           37 :                   && !integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
   19898              :                 {
   19899           16 :                   gcc_assert (novariants_cond == NULL_TREE);
   19900           16 :                   novariants_clause = c;
   19901           16 :                   novariants_cond = OMP_CLAUSE_NOVARIANTS_EXPR (c);
   19902              :                 }
   19903           21 :               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOCONTEXT
   19904           21 :                        && !integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
   19905              :                 {
   19906           16 :                   gcc_assert (nocontext_cond == NULL_TREE);
   19907           16 :                   nocontext_clause = c;
   19908           16 :                   nocontext_cond = OMP_CLAUSE_NOCONTEXT_EXPR (c);
   19909              :                 }
   19910              :             }
   19911           22 :           gcc_assert (novariants_cond != NULL_TREE
   19912              :                       || nocontext_cond != NULL_TREE);
   19913              : 
   19914           22 :           enum gimplify_status ret
   19915           22 :             = gimplify_expr (&novariants_cond, &body, NULL, is_gimple_val,
   19916              :                              fb_rvalue);
   19917           22 :           if (ret == GS_ERROR || ret == GS_UNHANDLED)
   19918            0 :             return ret;
   19919           22 :           ret = gimplify_expr (&nocontext_cond, &body, NULL, is_gimple_val,
   19920              :                                fb_rvalue);
   19921           22 :           if (ret == GS_ERROR || ret == GS_UNHANDLED)
   19922              :             return ret;
   19923              : 
   19924           22 :           tree end_label = create_artificial_label (UNKNOWN_LOCATION);
   19925              : 
   19926           22 :           if (novariants_cond != NULL_TREE)
   19927              :             {
   19928           16 :               tree base_label = create_artificial_label (UNKNOWN_LOCATION);
   19929           16 :               tree cond_label = create_artificial_label (UNKNOWN_LOCATION);
   19930           16 :               gcond *novariants_cond_stmt
   19931           16 :                 = gimple_build_cond_from_tree (novariants_cond, base_label,
   19932              :                                                cond_label);
   19933           16 :               gimplify_seq_add_stmt (&body, novariants_cond_stmt);
   19934              : 
   19935           16 :               gimplify_seq_add_stmt (&body, gimple_build_label (base_label));
   19936           16 :               tree base_call_expr2 = copy_node (base_call_expr);
   19937           16 :               base_call_expr2
   19938           16 :                 = build_call_expr_internal_loc (EXPR_LOCATION (base_call_expr2),
   19939              :                                                 IFN_GOMP_DISPATCH,
   19940           16 :                                                 TREE_TYPE (base_call_expr2), 1,
   19941              :                                                 base_call_expr2);
   19942           16 :               if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
   19943              :                 {
   19944           16 :                   base_call_expr2 = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
   19945              :                                             base_call_expr2);
   19946              :                 }
   19947           16 :               OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
   19948           16 :                 = boolean_true_node;
   19949           16 :               gimplify_and_add (base_call_expr2, &body);
   19950           16 :               gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
   19951              : 
   19952           16 :               OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
   19953           16 :                 = boolean_false_node;
   19954           16 :               gimplify_seq_add_stmt (&body, gimple_build_label (cond_label));
   19955              :             }
   19956              : 
   19957           22 :           if (nocontext_cond != NULL_TREE)
   19958              :             {
   19959           16 :               tree variant1_label = create_artificial_label (UNKNOWN_LOCATION);
   19960           16 :               tree variant2_label = create_artificial_label (UNKNOWN_LOCATION);
   19961           16 :               gcond *nocontext_cond_stmt
   19962           16 :                 = gimple_build_cond_from_tree (nocontext_cond, variant1_label,
   19963              :                                                variant2_label);
   19964           16 :               gimplify_seq_add_stmt (&body, nocontext_cond_stmt);
   19965              : 
   19966           32 :               gimplify_seq_add_stmt (&body,
   19967           16 :                                      gimple_build_label (variant1_label));
   19968           16 :               tree variant_call_expr = copy_node (base_call_expr);
   19969           32 :               variant_call_expr = build_call_expr_internal_loc (
   19970           16 :                 EXPR_LOCATION (variant_call_expr), IFN_GOMP_DISPATCH,
   19971           16 :                 TREE_TYPE (variant_call_expr), 1, variant_call_expr);
   19972           16 :               if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
   19973              :                 {
   19974           16 :                   variant_call_expr = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
   19975              :                                               variant_call_expr);
   19976              :                 }
   19977           16 :               OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_true_node;
   19978           16 :               gimplify_and_add (variant_call_expr, &body);
   19979           16 :               gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
   19980           16 :               OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_false_node;
   19981           32 :               gimplify_seq_add_stmt (&body,
   19982           16 :                                      gimple_build_label (variant2_label));
   19983              :             }
   19984              : 
   19985           22 :           tree variant_call_expr = base_call_expr;
   19986           22 :           variant_call_expr
   19987           22 :             = build_call_expr_internal_loc (EXPR_LOCATION (variant_call_expr),
   19988              :                                             IFN_GOMP_DISPATCH,
   19989           22 :                                             TREE_TYPE (variant_call_expr), 1,
   19990              :                                             variant_call_expr);
   19991           22 :           if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
   19992              :             {
   19993           22 :               variant_call_expr
   19994           22 :                 = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, variant_call_expr);
   19995              :             }
   19996           22 :           gimplify_and_add (variant_call_expr, &body);
   19997           22 :           gimplify_seq_add_stmt (&body, gimple_build_label (end_label));
   19998              :         }
   19999              :       else
   20000          822 :         gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
   20001              :     }
   20002              :   else
   20003            8 :     gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
   20004              : 
   20005              :   // Restore default-device-var ICV
   20006          852 :   if (saved_device_icv != NULL_TREE)
   20007              :     {
   20008          360 :       tree fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
   20009          360 :       gcall *call = gimple_build_call (fn, 1, saved_device_icv);
   20010          360 :       gimplify_seq_add_stmt (&body, call);
   20011              :     }
   20012              : 
   20013              :   // Wrap dispatch body into a bind
   20014          852 :   gimple *bind = gimple_build_bind (NULL_TREE, body, NULL_TREE);
   20015          852 :   pop_gimplify_context (bind);
   20016              : 
   20017              :   // Manually tear down context created by gimplify_scan_omp_clauses to avoid a
   20018              :   // call to gimplify_adjust_omp_clauses
   20019          852 :   gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
   20020          852 :   if (ctx != NULL)
   20021              :     {
   20022          852 :       gcc_assert (ctx->code == OMP_DISPATCH);
   20023          852 :       gimplify_omp_ctxp = ctx->outer_context;
   20024          852 :       delete_omp_context (ctx);
   20025              :     }
   20026              : 
   20027              :   // Remove nowait as it has no effect on dispatch (OpenMP 5.2), device as it
   20028              :   // has been handled above, and depend as the front end handled it by inserting
   20029              :   // taskwait.
   20030          852 :   tree *dispatch_clauses_ptr = &OMP_DISPATCH_CLAUSES (expr);
   20031         1445 :   for (tree c = *dispatch_clauses_ptr; c; c = *dispatch_clauses_ptr)
   20032              :     {
   20033          991 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT
   20034              :           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
   20035              :           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE)
   20036              :         {
   20037          398 :           *dispatch_clauses_ptr = OMP_CLAUSE_CHAIN (c);
   20038          398 :           break;
   20039              :         }
   20040              :       else
   20041          593 :         dispatch_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
   20042              :     }
   20043              : 
   20044          852 :   gimple *stmt = gimple_build_omp_dispatch (bind, OMP_DISPATCH_CLAUSES (expr));
   20045          852 :   gimplify_seq_add_stmt (pre_p, stmt);
   20046          852 :   *expr_p = NULL_TREE;
   20047          852 :   return GS_ALL_DONE;
   20048              : }
   20049              : 
   20050              : /* Expand a metadirective that has been resolved at gimplification time
   20051              :    into the candidate directive variants in CANDIDATES.  */
   20052              : 
   20053              : static enum gimplify_status
   20054          176 : expand_omp_metadirective (vec<struct omp_variant> &candidates,
   20055              :                           gimple_seq *pre_p)
   20056              : {
   20057          176 :   auto_vec<tree> selectors;
   20058          176 :   auto_vec<tree> directive_labels;
   20059          176 :   auto_vec<gimple_seq> directive_bodies;
   20060          176 :   tree body_label = NULL_TREE;
   20061          176 :   tree end_label = create_artificial_label (UNKNOWN_LOCATION);
   20062              : 
   20063              :   /* Construct bodies for each candidate.  */
   20064          608 :   for (unsigned i = 0; i < candidates.length(); i++)
   20065              :     {
   20066          256 :       struct omp_variant &candidate = candidates[i];
   20067          256 :       gimple_seq body = NULL;
   20068              : 
   20069          256 :       selectors.safe_push (omp_dynamic_cond (candidate.selector,
   20070              :                                              find_supercontext ()));
   20071          256 :       directive_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
   20072              : 
   20073          256 :       gimplify_seq_add_stmt (&body,
   20074          256 :                              gimple_build_label (directive_labels.last ()));
   20075          256 :       if (candidate.alternative != NULL_TREE)
   20076          256 :         gimplify_stmt (&candidate.alternative, &body);
   20077          256 :       if (candidate.body != NULL_TREE)
   20078              :         {
   20079           36 :           if (body_label != NULL_TREE)
   20080            0 :             gimplify_seq_add_stmt (&body, gimple_build_goto (body_label));
   20081              :           else
   20082              :             {
   20083           36 :               body_label = create_artificial_label (UNKNOWN_LOCATION);
   20084           36 :               gimplify_seq_add_stmt (&body, gimple_build_label (body_label));
   20085           36 :               gimplify_stmt (&candidate.body, &body);
   20086              :             }
   20087              :         }
   20088              : 
   20089          256 :       directive_bodies.safe_push (body);
   20090              :     }
   20091              : 
   20092          176 :   auto_vec<tree> cond_labels;
   20093              : 
   20094          176 :   cond_labels.safe_push (NULL_TREE);
   20095          388 :   for (unsigned i = 1; i < candidates.length () - 1; i++)
   20096           18 :     cond_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
   20097          176 :   if (candidates.length () > 1)
   20098           62 :     cond_labels.safe_push (directive_labels.last ());
   20099              : 
   20100              :   /* Generate conditionals to test each dynamic selector in turn, executing
   20101              :      the directive candidate if successful.  */
   20102          512 :   for (unsigned i = 0; i < candidates.length () - 1; i++)
   20103              :     {
   20104           80 :       if (i != 0)
   20105           18 :         gimplify_seq_add_stmt (pre_p, gimple_build_label (cond_labels [i]));
   20106              : 
   20107           80 :       enum gimplify_status ret = gimplify_expr (&selectors[i], pre_p, NULL,
   20108              :                                                 is_gimple_val, fb_rvalue);
   20109           80 :       if (ret == GS_ERROR || ret == GS_UNHANDLED)
   20110              :         return ret;
   20111              : 
   20112           80 :       gcond *cond_stmt
   20113           80 :         = gimple_build_cond_from_tree (selectors[i], directive_labels[i],
   20114           80 :                                        cond_labels[i + 1]);
   20115              : 
   20116           80 :       gimplify_seq_add_stmt (pre_p, cond_stmt);
   20117           80 :       gimplify_seq_add_seq (pre_p, directive_bodies[i]);
   20118           80 :       gimplify_seq_add_stmt (pre_p, gimple_build_goto (end_label));
   20119              :     }
   20120              : 
   20121          176 :   gimplify_seq_add_seq (pre_p, directive_bodies.last ());
   20122          176 :   gimplify_seq_add_stmt (pre_p, gimple_build_label (end_label));
   20123              : 
   20124          176 :   return GS_ALL_DONE;
   20125          176 : }
   20126              : 
   20127              : /* Expand a variant construct that requires late resolution in the ompdevlow
   20128              :    pass.  It's a bit easier to do this in tree form and then gimplify that,
   20129              :    than to emit gimple.  The output is going to look something like:
   20130              : 
   20131              :      switch_var = OMP_NEXT_VARIANT (0, state);
   20132              :      loop_label:
   20133              :      switch (switch_var)
   20134              :        {
   20135              :        case 1:
   20136              :          if (dynamic_selector_predicate_1)
   20137              :            {
   20138              :              alternative_1;
   20139              :              goto end_label;
   20140              :            }
   20141              :          else
   20142              :            {
   20143              :              switch_var = OMP_NEXT_VARIANT (1, state);
   20144              :              goto loop_label;
   20145              :            }
   20146              :         case 2:
   20147              :           ...
   20148              :         }
   20149              :       end_label:
   20150              : 
   20151              :   OMP_NEXT_VARIANT is a magic cookie that is replaced with the switch variable
   20152              :   index of the next variant to try, after late resolution.  */
   20153              : 
   20154              : static tree
   20155           16 : expand_late_variant_directive (vec<struct omp_variant> all_candidates,
   20156              :                                tree construct_context)
   20157              : {
   20158           16 :   tree body_label = NULL_TREE;
   20159           16 :   tree standalone_body = NULL_TREE;
   20160           16 :   tree loop_label = create_artificial_label (UNKNOWN_LOCATION);
   20161           16 :   tree end_label = create_artificial_label (UNKNOWN_LOCATION);
   20162           32 :   tree selectors = make_tree_vec (all_candidates.length ());
   20163           16 :   tree switch_body = NULL_TREE;
   20164           16 :   tree switch_var = create_tmp_var (integer_type_node, "variant");
   20165           16 :   tree state = tree_cons (NULL_TREE, construct_context, selectors);
   20166              : 
   20167          108 :   for (unsigned int i = 0; i < all_candidates.length (); i++)
   20168              :     {
   20169           76 :       tree selector = all_candidates[i].selector;
   20170           76 :       tree alternative = all_candidates[i].alternative;
   20171           76 :       tree body = all_candidates[i].body;
   20172           76 :       TREE_VEC_ELT (selectors, i) = selector;
   20173              : 
   20174              :       /* Case label.  Numbering is 1-based.  */
   20175           76 :       tree case_val = build_int_cst (integer_type_node, i + 1);
   20176           76 :       tree case_label
   20177           76 :         = build_case_label (case_val, NULL_TREE,
   20178              :                             create_artificial_label (UNKNOWN_LOCATION));
   20179           76 :       append_to_statement_list (case_label, &switch_body);
   20180              : 
   20181              :       /* The actual body of the variant.  */
   20182           76 :       tree variant_body = NULL_TREE;
   20183           76 :       append_to_statement_list (alternative, &variant_body);
   20184              : 
   20185           76 :       if (body != NULL_TREE)
   20186              :         {
   20187            0 :           if (standalone_body == NULL)
   20188              :             {
   20189            0 :               standalone_body = body;
   20190            0 :               body_label = create_artificial_label (UNKNOWN_LOCATION);
   20191              :             }
   20192            0 :           append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
   20193              :                                             body_label),
   20194              :                                     &variant_body);
   20195              :         }
   20196              :       else
   20197           76 :         append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
   20198              :                                           end_label),
   20199              :                                   &variant_body);
   20200              : 
   20201              :       /* If this is a dynamic selector, wrap variant_body with a conditional.
   20202              :          If the predicate doesn't match, the else clause sets switch_var and
   20203              :          jumps to loop_var to try again.  */
   20204           76 :       tree dynamic_selector = omp_dynamic_cond (selector, find_supercontext ());
   20205           76 :       if (dynamic_selector)
   20206              :         {
   20207           20 :           tree else_stmt = NULL_TREE;
   20208           20 :           tree next = build2 (OMP_NEXT_VARIANT, integer_type_node,
   20209              :                               case_val, state);
   20210           20 :           append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
   20211              :                                             switch_var, next),
   20212              :                                     &else_stmt);
   20213           20 :           append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
   20214              :                                             loop_label),
   20215              :                                     &else_stmt);
   20216           20 :           variant_body = build3 (COND_EXPR, void_type_node, dynamic_selector,
   20217              :                                  variant_body, else_stmt);
   20218              :         }
   20219           76 :       append_to_statement_list (variant_body, &switch_body);
   20220              :     }
   20221              : 
   20222              :   /* Put it all together.  */
   20223           16 :   tree result = NULL_TREE;
   20224           16 :   tree first = build2 (OMP_NEXT_VARIANT, integer_type_node, integer_zero_node,
   20225              :                        state);
   20226           16 :   append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
   20227              :                                     switch_var, first),
   20228              :                             &result);
   20229           16 :   append_to_statement_list (build1 (LABEL_EXPR, void_type_node, loop_label),
   20230              :                             &result);
   20231           16 :   append_to_statement_list (build2 (SWITCH_EXPR, integer_type_node,
   20232              :                                     switch_var, switch_body),
   20233              :                             &result);
   20234           16 :   if (standalone_body)
   20235              :     {
   20236            0 :       append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
   20237              :                                         body_label),
   20238              :                                 &result);
   20239            0 :       append_to_statement_list (standalone_body, &result);
   20240              :     }
   20241           16 :   append_to_statement_list (build1 (LABEL_EXPR, void_type_node, end_label),
   20242              :                             &result);
   20243           16 :   cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
   20244           16 :   return result;
   20245              : }
   20246              : 
   20247              : 
   20248              : /* Gimplify an OMP_METADIRECTIVE construct.   EXPR is the tree version.
   20249              :    The metadirective will be resolved at this point if possible, otherwise
   20250              :    a GIMPLE_OMP_VARIANT_CONSTRUCT is created.  */
   20251              : 
   20252              : static enum gimplify_status
   20253          180 : gimplify_omp_metadirective (tree *expr_p, gimple_seq *pre_p, gimple_seq *,
   20254              :                             bool (*) (tree), fallback_t)
   20255              : {
   20256              :   /* Try to resolve the metadirective.  */
   20257          180 :   tree construct_context = omp_get_construct_context ();
   20258          180 :   vec<struct omp_variant> all_candidates
   20259          180 :     = omp_metadirective_candidates (*expr_p, construct_context);
   20260          180 :   vec<struct omp_variant> candidates
   20261          180 :     = omp_get_dynamic_candidates (all_candidates, construct_context);
   20262          180 :   if (!candidates.is_empty ())
   20263          176 :     return expand_omp_metadirective (candidates, pre_p);
   20264              : 
   20265              :   /* The metadirective cannot be resolved yet.  Turn it into a loop with
   20266              :      a nested switch statement, using OMP_NEXT_VARIANT to set the control
   20267              :      variable for the switch.  */
   20268            4 :   *expr_p = expand_late_variant_directive (all_candidates, construct_context);
   20269            4 :   return GS_OK;
   20270              : }
   20271              : 
   20272              : /* Gimplify an OMP_DECLARE_MAPPER node (by just removing it).  */
   20273              : 
   20274              : static enum gimplify_status
   20275            0 : gimplify_omp_declare_mapper (tree *expr_p)
   20276              : {
   20277            0 :   *expr_p = NULL_TREE;
   20278            0 :   return GS_ALL_DONE;
   20279              : }
   20280              : 
   20281              : /* Convert the GENERIC expression tree *EXPR_P to GIMPLE.  If the
   20282              :    expression produces a value to be used as an operand inside a GIMPLE
   20283              :    statement, the value will be stored back in *EXPR_P.  This value will
   20284              :    be a tree of class tcc_declaration, tcc_constant, tcc_reference or
   20285              :    an SSA_NAME.  The corresponding sequence of GIMPLE statements is
   20286              :    emitted in PRE_P and POST_P.
   20287              : 
   20288              :    Additionally, this process may overwrite parts of the input
   20289              :    expression during gimplification.  Ideally, it should be
   20290              :    possible to do non-destructive gimplification.
   20291              : 
   20292              :    EXPR_P points to the GENERIC expression to convert to GIMPLE.  If
   20293              :       the expression needs to evaluate to a value to be used as
   20294              :       an operand in a GIMPLE statement, this value will be stored in
   20295              :       *EXPR_P on exit.  This happens when the caller specifies one
   20296              :       of fb_lvalue or fb_rvalue fallback flags.
   20297              : 
   20298              :    PRE_P will contain the sequence of GIMPLE statements corresponding
   20299              :        to the evaluation of EXPR and all the side-effects that must
   20300              :        be executed before the main expression.  On exit, the last
   20301              :        statement of PRE_P is the core statement being gimplified.  For
   20302              :        instance, when gimplifying 'if (++a)' the last statement in
   20303              :        PRE_P will be 'if (t.1)' where t.1 is the result of
   20304              :        pre-incrementing 'a'.
   20305              : 
   20306              :    POST_P will contain the sequence of GIMPLE statements corresponding
   20307              :        to the evaluation of all the side-effects that must be executed
   20308              :        after the main expression.  If this is NULL, the post
   20309              :        side-effects are stored at the end of PRE_P.
   20310              : 
   20311              :        The reason why the output is split in two is to handle post
   20312              :        side-effects explicitly.  In some cases, an expression may have
   20313              :        inner and outer post side-effects which need to be emitted in
   20314              :        an order different from the one given by the recursive
   20315              :        traversal.  For instance, for the expression (*p--)++ the post
   20316              :        side-effects of '--' must actually occur *after* the post
   20317              :        side-effects of '++'.  However, gimplification will first visit
   20318              :        the inner expression, so if a separate POST sequence was not
   20319              :        used, the resulting sequence would be:
   20320              : 
   20321              :             1   t.1 = *p
   20322              :             2   p = p - 1
   20323              :             3   t.2 = t.1 + 1
   20324              :             4   *p = t.2
   20325              : 
   20326              :        However, the post-decrement operation in line #2 must not be
   20327              :        evaluated until after the store to *p at line #4, so the
   20328              :        correct sequence should be:
   20329              : 
   20330              :             1   t.1 = *p
   20331              :             2   t.2 = t.1 + 1
   20332              :             3   *p = t.2
   20333              :             4   p = p - 1
   20334              : 
   20335              :        So, by specifying a separate post queue, it is possible
   20336              :        to emit the post side-effects in the correct order.
   20337              :        If POST_P is NULL, an internal queue will be used.  Before
   20338              :        returning to the caller, the sequence POST_P is appended to
   20339              :        the main output sequence PRE_P.
   20340              : 
   20341              :    GIMPLE_TEST_F points to a function that takes a tree T and
   20342              :        returns nonzero if T is in the GIMPLE form requested by the
   20343              :        caller.  The GIMPLE predicates are in gimple.cc.
   20344              : 
   20345              :    FALLBACK tells the function what sort of a temporary we want if
   20346              :        gimplification cannot produce an expression that complies with
   20347              :        GIMPLE_TEST_F.
   20348              : 
   20349              :        fb_none means that no temporary should be generated
   20350              :        fb_rvalue means that an rvalue is OK to generate
   20351              :        fb_lvalue means that an lvalue is OK to generate
   20352              :        fb_either means that either is OK, but an lvalue is preferable.
   20353              :        fb_mayfail means that gimplification may fail (in which case
   20354              :        GS_ERROR will be returned)
   20355              : 
   20356              :    The return value is either GS_ERROR or GS_ALL_DONE, since this
   20357              :    function iterates until EXPR is completely gimplified or an error
   20358              :    occurs.  */
   20359              : 
   20360              : enum gimplify_status
   20361    534997725 : gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
   20362              :                bool (*gimple_test_f) (tree), fallback_t fallback)
   20363              : {
   20364    534997725 :   tree tmp;
   20365    534997725 :   gimple_seq internal_pre = NULL;
   20366    534997725 :   gimple_seq internal_post = NULL;
   20367    534997725 :   tree save_expr;
   20368    534997725 :   bool is_statement;
   20369    534997725 :   location_t saved_location;
   20370    534997725 :   enum gimplify_status ret;
   20371    534997725 :   gimple_stmt_iterator pre_last_gsi, post_last_gsi;
   20372    534997725 :   tree label;
   20373              : 
   20374    534997725 :   save_expr = *expr_p;
   20375    534997725 :   if (save_expr == NULL_TREE)
   20376              :     return GS_ALL_DONE;
   20377              : 
   20378              :   /* If we are gimplifying a top-level statement, PRE_P must be valid.  */
   20379    481053678 :   is_statement = gimple_test_f == is_gimple_stmt;
   20380    481053678 :   if (is_statement)
   20381    104123358 :     gcc_assert (pre_p);
   20382              : 
   20383              :   /* Consistency checks.  */
   20384    481053678 :   if (gimple_test_f == is_gimple_reg)
   20385      6489988 :     gcc_assert (fallback & (fb_rvalue | fb_lvalue));
   20386    474563690 :   else if (gimple_test_f == is_gimple_val
   20387    349838528 :            || gimple_test_f == is_gimple_call_addr
   20388    332960544 :            || gimple_test_f == is_gimple_condexpr_for_cond
   20389    327231661 :            || gimple_test_f == is_gimple_mem_rhs
   20390    327086865 :            || gimple_test_f == is_gimple_mem_rhs_or_call
   20391    315401217 :            || gimple_test_f == is_gimple_reg_rhs
   20392    314771874 :            || gimple_test_f == is_gimple_reg_rhs_or_call
   20393    240671519 :            || gimple_test_f == is_gimple_asm_val
   20394    240631022 :            || gimple_test_f == is_gimple_mem_ref_addr)
   20395    249794226 :     gcc_assert (fallback & fb_rvalue);
   20396    224769464 :   else if (gimple_test_f == is_gimple_min_lval
   20397    193499719 :            || gimple_test_f == is_gimple_lvalue)
   20398     86502481 :     gcc_assert (fallback & fb_lvalue);
   20399    138266983 :   else if (gimple_test_f == is_gimple_addressable)
   20400     34143625 :     gcc_assert (fallback & fb_either);
   20401    104123358 :   else if (gimple_test_f == is_gimple_stmt)
   20402    104123358 :     gcc_assert (fallback == fb_none);
   20403              :   else
   20404              :     {
   20405              :       /* We should have recognized the GIMPLE_TEST_F predicate to
   20406              :          know what kind of fallback to use in case a temporary is
   20407              :          needed to hold the value or address of *EXPR_P.  */
   20408            0 :       gcc_unreachable ();
   20409              :     }
   20410              : 
   20411              :   /* We used to check the predicate here and return immediately if it
   20412              :      succeeds.  This is wrong; the design is for gimplification to be
   20413              :      idempotent, and for the predicates to only test for valid forms, not
   20414              :      whether they are fully simplified.  */
   20415    481053678 :   if (pre_p == NULL)
   20416            0 :     pre_p = &internal_pre;
   20417              : 
   20418    481053678 :   if (post_p == NULL)
   20419    193391030 :     post_p = &internal_post;
   20420              : 
   20421              :   /* Remember the last statements added to PRE_P and POST_P.  Every
   20422              :      new statement added by the gimplification helpers needs to be
   20423              :      annotated with location information.  To centralize the
   20424              :      responsibility, we remember the last statement that had been
   20425              :      added to both queues before gimplifying *EXPR_P.  If
   20426              :      gimplification produces new statements in PRE_P and POST_P, those
   20427              :      statements will be annotated with the same location information
   20428              :      as *EXPR_P.  */
   20429    481053678 :   pre_last_gsi = gsi_last (*pre_p);
   20430    481053678 :   post_last_gsi = gsi_last (*post_p);
   20431              : 
   20432    481053678 :   saved_location = input_location;
   20433    481053678 :   if (save_expr != error_mark_node
   20434    481053678 :       && EXPR_HAS_LOCATION (*expr_p))
   20435    180143344 :     input_location = EXPR_LOCATION (*expr_p);
   20436              : 
   20437              :   /* Loop over the specific gimplifiers until the toplevel node
   20438              :      remains the same.  */
   20439    499981520 :   do
   20440              :     {
   20441              :       /* Strip away as many useless type conversions as possible
   20442              :          at the toplevel.  */
   20443    499981520 :       STRIP_USELESS_TYPE_CONVERSION (*expr_p);
   20444              : 
   20445              :       /* Remember the expr.  */
   20446    499981520 :       save_expr = *expr_p;
   20447              : 
   20448              :       /* Die, die, die, my darling.  */
   20449    499981520 :       if (error_operand_p (save_expr))
   20450              :         {
   20451              :           ret = GS_ERROR;
   20452              :           break;
   20453              :         }
   20454              : 
   20455              :       /* Do any language-specific gimplification.  */
   20456    499977764 :       ret = ((enum gimplify_status)
   20457    499977764 :              lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
   20458    499977764 :       if (ret == GS_OK)
   20459              :         {
   20460     29736847 :           if (*expr_p == NULL_TREE)
   20461              :             break;
   20462     29736847 :           if (*expr_p != save_expr)
   20463      4966511 :             continue;
   20464              :         }
   20465    470240917 :       else if (ret != GS_UNHANDLED)
   20466              :         break;
   20467              : 
   20468              :       /* Make sure that all the cases set 'ret' appropriately.  */
   20469    494408806 :       ret = GS_UNHANDLED;
   20470    494408806 :       switch (TREE_CODE (*expr_p))
   20471              :         {
   20472              :           /* First deal with the special cases.  */
   20473              : 
   20474      1150954 :         case POSTINCREMENT_EXPR:
   20475      1150954 :         case POSTDECREMENT_EXPR:
   20476      1150954 :         case PREINCREMENT_EXPR:
   20477      1150954 :         case PREDECREMENT_EXPR:
   20478      2301908 :           ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
   20479              :                                         fallback != fb_none,
   20480      1150954 :                                         TREE_TYPE (*expr_p));
   20481      1150954 :           break;
   20482              : 
   20483       607307 :         case VIEW_CONVERT_EXPR:
   20484       607307 :           if ((fallback & fb_rvalue)
   20485       607187 :               && is_gimple_reg_type (TREE_TYPE (*expr_p))
   20486      1037338 :               && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
   20487              :             {
   20488       418144 :               ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
   20489              :                                    post_p, is_gimple_val, fb_rvalue);
   20490       418144 :               recalculate_side_effects (*expr_p);
   20491       418144 :               break;
   20492              :             }
   20493              :           /* Fallthru.  */
   20494              : 
   20495     31206600 :         case ARRAY_REF:
   20496     31206600 :         case ARRAY_RANGE_REF:
   20497     31206600 :         case REALPART_EXPR:
   20498     31206600 :         case IMAGPART_EXPR:
   20499     31206600 :         case COMPONENT_REF:
   20500     31206600 :           ret = gimplify_compound_lval (expr_p, pre_p, post_p,
   20501              :                                         fallback ? fallback : fb_rvalue);
   20502     31206600 :           break;
   20503              : 
   20504      6354363 :         case COND_EXPR:
   20505      6354363 :           ret = gimplify_cond_expr (expr_p, pre_p, fallback);
   20506              : 
   20507              :           /* C99 code may assign to an array in a structure value of a
   20508              :              conditional expression, and this has undefined behavior
   20509              :              only on execution, so create a temporary if an lvalue is
   20510              :              required.  */
   20511      6354363 :           if (fallback == fb_lvalue)
   20512              :             {
   20513            7 :               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
   20514            7 :               mark_addressable (*expr_p);
   20515            7 :               ret = GS_OK;
   20516              :             }
   20517              :           break;
   20518              : 
   20519     17446566 :         case CALL_EXPR:
   20520     17446566 :           ret = gimplify_call_expr (expr_p, pre_p, fallback);
   20521              : 
   20522              :           /* C99 code may assign to an array in a structure returned
   20523              :              from a function, and this has undefined behavior only on
   20524              :              execution, so create a temporary if an lvalue is
   20525              :              required.  */
   20526     17446566 :           if (fallback == fb_lvalue)
   20527              :             {
   20528        17395 :               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
   20529        17395 :               mark_addressable (*expr_p);
   20530        17395 :               ret = GS_OK;
   20531              :             }
   20532              :           break;
   20533              : 
   20534            0 :         case TREE_LIST:
   20535            0 :           gcc_unreachable ();
   20536              : 
   20537            0 :         case OMP_ARRAY_SECTION:
   20538            0 :           gcc_unreachable ();
   20539              : 
   20540       506497 :         case COMPOUND_EXPR:
   20541       506497 :           ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
   20542       506497 :           break;
   20543              : 
   20544        37446 :         case COMPOUND_LITERAL_EXPR:
   20545        37446 :           ret = gimplify_compound_literal_expr (expr_p, pre_p,
   20546              :                                                 gimple_test_f, fallback);
   20547        37446 :           break;
   20548              : 
   20549     51789572 :         case MODIFY_EXPR:
   20550     51789572 :         case INIT_EXPR:
   20551     51789572 :           ret = gimplify_modify_expr (expr_p, pre_p, post_p,
   20552              :                                       fallback != fb_none);
   20553     51789572 :           break;
   20554              : 
   20555       115789 :         case TRUTH_ANDIF_EXPR:
   20556       115789 :         case TRUTH_ORIF_EXPR:
   20557       115789 :           {
   20558              :             /* Preserve the original type of the expression and the
   20559              :                source location of the outer expression.  */
   20560       115789 :             tree org_type = TREE_TYPE (*expr_p);
   20561       115789 :             *expr_p = gimple_boolify (*expr_p);
   20562       115789 :             *expr_p = build3_loc (input_location, COND_EXPR,
   20563              :                                   org_type, *expr_p,
   20564              :                                   fold_convert_loc
   20565              :                                     (input_location,
   20566              :                                      org_type, boolean_true_node),
   20567              :                                   fold_convert_loc
   20568              :                                     (input_location,
   20569              :                                      org_type, boolean_false_node));
   20570       115789 :             ret = GS_OK;
   20571       115789 :             break;
   20572              :           }
   20573              : 
   20574       238921 :         case TRUTH_NOT_EXPR:
   20575       238921 :           {
   20576       238921 :             tree type = TREE_TYPE (*expr_p);
   20577              :             /* The parsers are careful to generate TRUTH_NOT_EXPR
   20578              :                only with operands that are always zero or one.
   20579              :                We do not fold here but handle the only interesting case
   20580              :                manually, as fold may re-introduce the TRUTH_NOT_EXPR.  */
   20581       238921 :             *expr_p = gimple_boolify (*expr_p);
   20582       238921 :             if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
   20583       238921 :               *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
   20584       238921 :                                     TREE_TYPE (*expr_p),
   20585       238921 :                                     TREE_OPERAND (*expr_p, 0));
   20586              :             else
   20587            0 :               *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
   20588            0 :                                     TREE_TYPE (*expr_p),
   20589            0 :                                     TREE_OPERAND (*expr_p, 0),
   20590            0 :                                     build_int_cst (TREE_TYPE (*expr_p), 1));
   20591       238921 :             if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
   20592         6562 :               *expr_p = fold_convert_loc (input_location, type, *expr_p);
   20593              :             ret = GS_OK;
   20594              :             break;
   20595              :           }
   20596              : 
   20597     34229061 :         case ADDR_EXPR:
   20598     34229061 :           ret = gimplify_addr_expr (expr_p, pre_p, post_p);
   20599     34229061 :           break;
   20600              : 
   20601         5851 :         case ANNOTATE_EXPR:
   20602         5851 :           {
   20603         5851 :             tree cond = TREE_OPERAND (*expr_p, 0);
   20604         5851 :             tree kind = TREE_OPERAND (*expr_p, 1);
   20605         5851 :             tree data = TREE_OPERAND (*expr_p, 2);
   20606         5851 :             tree type = TREE_TYPE (cond);
   20607         5851 :             if (!INTEGRAL_TYPE_P (type))
   20608              :               {
   20609            0 :                 *expr_p = cond;
   20610            0 :                 ret = GS_OK;
   20611            0 :                 break;
   20612              :               }
   20613         5851 :             tree tmp = create_tmp_var (type);
   20614         5851 :             gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
   20615         5851 :             gcall *call
   20616         5851 :               = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
   20617         5851 :             gimple_call_set_lhs (call, tmp);
   20618         5851 :             gimplify_seq_add_stmt (pre_p, call);
   20619         5851 :             *expr_p = tmp;
   20620         5851 :             ret = GS_ALL_DONE;
   20621         5851 :             break;
   20622              :           }
   20623              : 
   20624        51233 :         case VA_ARG_EXPR:
   20625        51233 :           ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
   20626        51233 :           break;
   20627              : 
   20628     17985329 :         CASE_CONVERT:
   20629     17985329 :           if (IS_EMPTY_STMT (*expr_p))
   20630              :             {
   20631              :               ret = GS_ALL_DONE;
   20632              :               break;
   20633              :             }
   20634              : 
   20635     16223494 :           if (VOID_TYPE_P (TREE_TYPE (*expr_p))
   20636     16223494 :               || fallback == fb_none)
   20637              :             {
   20638              :               /* Just strip a conversion to void (or in void context) and
   20639              :                  try again.  */
   20640      2738628 :               *expr_p = TREE_OPERAND (*expr_p, 0);
   20641      2738628 :               ret = GS_OK;
   20642      2738628 :               break;
   20643              :             }
   20644              : 
   20645     13484866 :           ret = gimplify_conversion (expr_p);
   20646     13484866 :           if (ret == GS_ERROR)
   20647              :             break;
   20648     13484866 :           if (*expr_p != save_expr)
   20649              :             break;
   20650              :           /* FALLTHRU */
   20651              : 
   20652     13613551 :         case FIX_TRUNC_EXPR:
   20653              :           /* unary_expr: ... | '(' cast ')' val | ...  */
   20654     13613551 :           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
   20655              :                                is_gimple_val, fb_rvalue);
   20656     13613551 :           recalculate_side_effects (*expr_p);
   20657     13613551 :           break;
   20658              : 
   20659      6418399 :         case INDIRECT_REF:
   20660      6418399 :           {
   20661      6418399 :             bool volatilep = TREE_THIS_VOLATILE (*expr_p);
   20662      6418399 :             bool notrap = TREE_THIS_NOTRAP (*expr_p);
   20663      6418399 :             tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
   20664              : 
   20665      6418399 :             *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
   20666      6418399 :             if (*expr_p != save_expr)
   20667              :               {
   20668              :                 ret = GS_OK;
   20669              :                 break;
   20670              :               }
   20671              : 
   20672      6399338 :             ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
   20673              :                                  is_gimple_reg, fb_rvalue);
   20674      6399338 :             if (ret == GS_ERROR)
   20675              :               break;
   20676              : 
   20677      6399337 :             recalculate_side_effects (*expr_p);
   20678     12798674 :             *expr_p = fold_build2_loc (input_location, MEM_REF,
   20679      6399337 :                                        TREE_TYPE (*expr_p),
   20680      6399337 :                                        TREE_OPERAND (*expr_p, 0),
   20681              :                                        build_int_cst (saved_ptr_type, 0));
   20682      6399337 :             TREE_THIS_VOLATILE (*expr_p) = volatilep;
   20683      6399337 :             TREE_THIS_NOTRAP (*expr_p) = notrap;
   20684      6399337 :             ret = GS_OK;
   20685      6399337 :             break;
   20686              :           }
   20687              : 
   20688              :         /* We arrive here through the various re-gimplifcation paths.  */
   20689     16318604 :         case MEM_REF:
   20690              :           /* First try re-folding the whole thing.  */
   20691     16318604 :           tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
   20692              :                              TREE_OPERAND (*expr_p, 0),
   20693              :                              TREE_OPERAND (*expr_p, 1));
   20694     16318604 :           if (tmp)
   20695              :             {
   20696         6612 :               REF_REVERSE_STORAGE_ORDER (tmp)
   20697         3306 :                 = REF_REVERSE_STORAGE_ORDER (*expr_p);
   20698         3306 :               *expr_p = tmp;
   20699         3306 :               recalculate_side_effects (*expr_p);
   20700         3306 :               ret = GS_OK;
   20701         3306 :               break;
   20702              :             }
   20703              :           /* Avoid re-gimplifying the address operand if it is already
   20704              :              in suitable form.  Re-gimplifying would mark the address
   20705              :              operand addressable.  Always gimplify when not in SSA form
   20706              :              as we still may have to gimplify decls with value-exprs.  */
   20707     16315298 :           if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
   20708     17340421 :               || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
   20709              :             {
   20710     15345383 :               ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
   20711              :                                    is_gimple_mem_ref_addr, fb_rvalue);
   20712     15345383 :               if (ret == GS_ERROR)
   20713              :                 break;
   20714              :             }
   20715     16315298 :           recalculate_side_effects (*expr_p);
   20716     16315298 :           ret = GS_ALL_DONE;
   20717     16315298 :           break;
   20718              : 
   20719              :         /* Constants need not be gimplified.  */
   20720     45773028 :         case INTEGER_CST:
   20721     45773028 :         case REAL_CST:
   20722     45773028 :         case FIXED_CST:
   20723     45773028 :         case STRING_CST:
   20724     45773028 :         case COMPLEX_CST:
   20725     45773028 :         case VECTOR_CST:
   20726              :           /* Drop the overflow flag on constants, we do not want
   20727              :              that in the GIMPLE IL.  */
   20728     45773028 :           if (TREE_OVERFLOW_P (*expr_p))
   20729          887 :             *expr_p = drop_tree_overflow (*expr_p);
   20730              :           ret = GS_ALL_DONE;
   20731              :           break;
   20732              : 
   20733       119242 :         case CONST_DECL:
   20734              :           /* If we require an lvalue, such as for ADDR_EXPR, retain the
   20735              :              CONST_DECL node.  Otherwise the decl is replaceable by its
   20736              :              value.  */
   20737              :           /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either.  */
   20738       119242 :           if (fallback & fb_lvalue)
   20739              :             ret = GS_ALL_DONE;
   20740              :           else
   20741              :             {
   20742         3316 :               *expr_p = DECL_INITIAL (*expr_p);
   20743         3316 :               ret = GS_OK;
   20744              :             }
   20745              :           break;
   20746              : 
   20747      6687131 :         case DECL_EXPR:
   20748      6687131 :           ret = gimplify_decl_expr (expr_p, pre_p);
   20749      6687131 :           break;
   20750              : 
   20751      6121620 :         case BIND_EXPR:
   20752      6121620 :           ret = gimplify_bind_expr (expr_p, pre_p);
   20753      6121620 :           break;
   20754              : 
   20755       211390 :         case LOOP_EXPR:
   20756       211390 :           ret = gimplify_loop_expr (expr_p, pre_p);
   20757       211390 :           break;
   20758              : 
   20759        51297 :         case SWITCH_EXPR:
   20760        51297 :           ret = gimplify_switch_expr (expr_p, pre_p);
   20761        51297 :           break;
   20762              : 
   20763         3411 :         case EXIT_EXPR:
   20764         3411 :           ret = gimplify_exit_expr (expr_p);
   20765         3411 :           break;
   20766              : 
   20767      1135005 :         case GOTO_EXPR:
   20768              :           /* If the target is not LABEL, then it is a computed jump
   20769              :              and the target needs to be gimplified.  */
   20770      1135005 :           if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
   20771              :             {
   20772         1274 :               ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
   20773              :                                    NULL, is_gimple_val, fb_rvalue);
   20774         1274 :               if (ret == GS_ERROR)
   20775              :                 break;
   20776              :             }
   20777      2270008 :           gimplify_seq_add_stmt (pre_p,
   20778      1135004 :                           gimple_build_goto (GOTO_DESTINATION (*expr_p)));
   20779      1135004 :           ret = GS_ALL_DONE;
   20780      1135004 :           break;
   20781              : 
   20782       105410 :         case PREDICT_EXPR:
   20783       421640 :           gimplify_seq_add_stmt (pre_p,
   20784       105410 :                         gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
   20785       105410 :                                               PREDICT_EXPR_OUTCOME (*expr_p)));
   20786       105410 :           ret = GS_ALL_DONE;
   20787       105410 :           break;
   20788              : 
   20789      2649060 :         case LABEL_EXPR:
   20790      2649060 :           ret = gimplify_label_expr (expr_p, pre_p);
   20791      2649060 :           label = LABEL_EXPR_LABEL (*expr_p);
   20792      2649060 :           gcc_assert (decl_function_context (label) == current_function_decl);
   20793              : 
   20794              :           /* If the label is used in a goto statement, or address of the label
   20795              :              is taken, we need to unpoison all variables that were seen so far.
   20796              :              Doing so would prevent us from reporting a false positives.  */
   20797      2649060 :           if (asan_poisoned_variables
   20798         5995 :               && asan_used_labels != NULL
   20799         1325 :               && asan_used_labels->contains (label)
   20800      2649972 :               && !gimplify_omp_ctxp)
   20801          904 :             asan_poison_variables (asan_poisoned_variables, false, pre_p);
   20802              :           break;
   20803              : 
   20804      1048651 :         case CASE_LABEL_EXPR:
   20805      1048651 :           ret = gimplify_case_label_expr (expr_p, pre_p);
   20806              : 
   20807      1048651 :           if (gimplify_ctxp->live_switch_vars)
   20808      1048464 :             asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
   20809              :                                    pre_p);
   20810              :           break;
   20811              : 
   20812      2303083 :         case RETURN_EXPR:
   20813      2303083 :           ret = gimplify_return_expr (*expr_p, pre_p);
   20814      2303083 :           break;
   20815              : 
   20816       983365 :         case CONSTRUCTOR:
   20817              :           /* Don't reduce this in place; let gimplify_init_constructor work its
   20818              :              magic.  Buf if we're just elaborating this for side effects, just
   20819              :              gimplify any element that has side-effects.  */
   20820       983365 :           if (fallback == fb_none)
   20821              :             {
   20822          411 :               unsigned HOST_WIDE_INT ix;
   20823          411 :               tree val;
   20824          411 :               tree temp = NULL_TREE;
   20825          428 :               FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
   20826           17 :                 if (TREE_SIDE_EFFECTS (val))
   20827            4 :                   append_to_statement_list (val, &temp);
   20828              : 
   20829          411 :               *expr_p = temp;
   20830          411 :               ret = temp ? GS_OK : GS_ALL_DONE;
   20831              :             }
   20832              :           /* C99 code may assign to an array in a constructed
   20833              :              structure or union, and this has undefined behavior only
   20834              :              on execution, so create a temporary if an lvalue is
   20835              :              required.  */
   20836       982954 :           else if (fallback == fb_lvalue)
   20837              :             {
   20838            3 :               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
   20839            3 :               mark_addressable (*expr_p);
   20840            3 :               ret = GS_OK;
   20841              :             }
   20842              :           else
   20843              :             ret = GS_ALL_DONE;
   20844              :           break;
   20845              : 
   20846              :           /* The following are special cases that are not handled by the
   20847              :              original GIMPLE grammar.  */
   20848              : 
   20849              :           /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
   20850              :              eliminated.  */
   20851       445539 :         case SAVE_EXPR:
   20852       445539 :           ret = gimplify_save_expr (expr_p, pre_p, post_p);
   20853       445539 :           break;
   20854              : 
   20855       359114 :         case BIT_FIELD_REF:
   20856       359114 :           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
   20857              :                                post_p, is_gimple_lvalue, fb_either);
   20858       359114 :           recalculate_side_effects (*expr_p);
   20859       359114 :           break;
   20860              : 
   20861         2262 :         case TARGET_MEM_REF:
   20862         2262 :           {
   20863         2262 :             enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
   20864              : 
   20865         2262 :             if (TMR_BASE (*expr_p))
   20866         2262 :               r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
   20867              :                                   post_p, is_gimple_mem_ref_addr, fb_either);
   20868         2262 :             if (TMR_INDEX (*expr_p))
   20869         1221 :               r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
   20870              :                                   post_p, is_gimple_val, fb_rvalue);
   20871         2262 :             if (TMR_INDEX2 (*expr_p))
   20872           72 :               r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
   20873              :                                   post_p, is_gimple_val, fb_rvalue);
   20874              :             /* TMR_STEP and TMR_OFFSET are always integer constants.  */
   20875         2262 :             ret = MIN (r0, r1);
   20876              :           }
   20877              :           break;
   20878              : 
   20879            0 :         case NON_LVALUE_EXPR:
   20880              :           /* This should have been stripped above.  */
   20881            0 :           gcc_unreachable ();
   20882              : 
   20883        83600 :         case ASM_EXPR:
   20884        83600 :           ret = gimplify_asm_expr (expr_p, pre_p, post_p);
   20885        83600 :           break;
   20886              : 
   20887       510047 :         case TRY_FINALLY_EXPR:
   20888       510047 :         case TRY_CATCH_EXPR:
   20889       510047 :           {
   20890       510047 :             gimple_seq eval, cleanup;
   20891       510047 :             gtry *try_;
   20892              : 
   20893              :             /* Calls to destructors are generated automatically in FINALLY/CATCH
   20894              :                block. They should have location as UNKNOWN_LOCATION. However,
   20895              :                gimplify_call_expr will reset these call stmts to input_location
   20896              :                if it finds stmt's location is unknown. To prevent resetting for
   20897              :                destructors, we set the input_location to unknown.
   20898              :                Note that this only affects the destructor calls in FINALLY/CATCH
   20899              :                block, and will automatically reset to its original value by the
   20900              :                end of gimplify_expr.  */
   20901       510047 :             input_location = UNKNOWN_LOCATION;
   20902       510047 :             eval = cleanup = NULL;
   20903       510047 :             gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
   20904       510047 :             bool save_in_handler_expr = gimplify_ctxp->in_handler_expr;
   20905       510047 :             if (TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
   20906       510047 :                 && TREE_CODE (TREE_OPERAND (*expr_p, 1)) == EH_ELSE_EXPR)
   20907              :               {
   20908          282 :                 gimple_seq n = NULL, e = NULL;
   20909          282 :                 gimplify_ctxp->in_handler_expr = true;
   20910          282 :                 gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
   20911              :                                                 0), &n);
   20912          282 :                 gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
   20913              :                                                 1), &e);
   20914          282 :                 if (!gimple_seq_empty_p (n) || !gimple_seq_empty_p (e))
   20915              :                   {
   20916          276 :                     geh_else *stmt = gimple_build_eh_else (n, e);
   20917          276 :                     gimple_seq_add_stmt (&cleanup, stmt);
   20918              :                   }
   20919              :               }
   20920              :             else
   20921              :               {
   20922       509765 :                 gimplify_ctxp->in_handler_expr = true;
   20923       509765 :                 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
   20924              :               }
   20925       510047 :             gimplify_ctxp->in_handler_expr = save_in_handler_expr;
   20926              :             /* Don't create bogus GIMPLE_TRY with empty cleanup.  */
   20927       510047 :             if (gimple_seq_empty_p (cleanup))
   20928              :               {
   20929        27168 :                 gimple_seq_add_seq (pre_p, eval);
   20930        27168 :                 ret = GS_ALL_DONE;
   20931        27168 :                 break;
   20932              :               }
   20933       482879 :             try_ = gimple_build_try (eval, cleanup,
   20934       482879 :                                      TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
   20935              :                                      ? GIMPLE_TRY_FINALLY
   20936              :                                      : GIMPLE_TRY_CATCH);
   20937       482879 :             if (EXPR_HAS_LOCATION (save_expr))
   20938       882282 :               gimple_set_location (try_, EXPR_LOCATION (save_expr));
   20939        41738 :             else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
   20940        30742 :               gimple_set_location (try_, saved_location);
   20941       482879 :             if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
   20942       172389 :               gimple_try_set_catch_is_cleanup (try_,
   20943       172389 :                                                TRY_CATCH_IS_CLEANUP (*expr_p));
   20944       482879 :             gimplify_seq_add_stmt (pre_p, try_);
   20945       482879 :             ret = GS_ALL_DONE;
   20946       482879 :             break;
   20947              :           }
   20948              : 
   20949      5436501 :         case CLEANUP_POINT_EXPR:
   20950      5436501 :           ret = gimplify_cleanup_point_expr (expr_p, pre_p);
   20951      5436501 :           break;
   20952              : 
   20953       867101 :         case TARGET_EXPR:
   20954       867101 :           ret = gimplify_target_expr (expr_p, pre_p, post_p);
   20955       867101 :           break;
   20956              : 
   20957        43036 :         case CATCH_EXPR:
   20958        43036 :           {
   20959        43036 :             gimple *c;
   20960        43036 :             gimple_seq handler = NULL;
   20961        43036 :             gimplify_and_add (CATCH_BODY (*expr_p), &handler);
   20962        43036 :             c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
   20963        43036 :             gimplify_seq_add_stmt (pre_p, c);
   20964        43036 :             ret = GS_ALL_DONE;
   20965        43036 :             break;
   20966              :           }
   20967              : 
   20968         4351 :         case EH_FILTER_EXPR:
   20969         4351 :           {
   20970         4351 :             gimple *ehf;
   20971         4351 :             gimple_seq failure = NULL;
   20972              : 
   20973         4351 :             gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
   20974         4351 :             ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
   20975         4351 :             copy_warning (ehf, *expr_p);
   20976         4351 :             gimplify_seq_add_stmt (pre_p, ehf);
   20977         4351 :             ret = GS_ALL_DONE;
   20978         4351 :             break;
   20979              :           }
   20980              : 
   20981        52285 :         case OBJ_TYPE_REF:
   20982        52285 :           {
   20983        52285 :             enum gimplify_status r0, r1;
   20984        52285 :             r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
   20985              :                                 post_p, is_gimple_val, fb_rvalue);
   20986        52285 :             r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
   20987              :                                 post_p, is_gimple_val, fb_rvalue);
   20988        52285 :             TREE_SIDE_EFFECTS (*expr_p) = 0;
   20989        52285 :             ret = MIN (r0, r1);
   20990              :           }
   20991              :           break;
   20992              : 
   20993        41267 :         case LABEL_DECL:
   20994              :           /* We get here when taking the address of a label.  We mark
   20995              :              the label as "forced"; meaning it can never be removed and
   20996              :              it is a potential target for any computed goto.  */
   20997        41267 :           FORCED_LABEL (*expr_p) = 1;
   20998        41267 :           ret = GS_ALL_DONE;
   20999        41267 :           break;
   21000              : 
   21001      8720639 :         case STATEMENT_LIST:
   21002      8720639 :           ret = gimplify_statement_list (expr_p, pre_p);
   21003      8720639 :           break;
   21004              : 
   21005         1746 :         case WITH_SIZE_EXPR:
   21006         1746 :           {
   21007         2028 :             gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
   21008              :                            post_p == &internal_post ? NULL : post_p,
   21009              :                            gimple_test_f, fallback);
   21010         1746 :             gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
   21011              :                            is_gimple_val, fb_rvalue);
   21012         1746 :             ret = GS_ALL_DONE;
   21013              :           }
   21014         1746 :           break;
   21015              : 
   21016    112420177 :         case VAR_DECL:
   21017    112420177 :         case PARM_DECL:
   21018    112420177 :           ret = gimplify_var_or_parm_decl (expr_p);
   21019    112420177 :           break;
   21020              : 
   21021       346595 :         case RESULT_DECL:
   21022              :           /* When within an OMP context, notice uses of variables.  */
   21023       346595 :           if (gimplify_omp_ctxp)
   21024         1412 :             omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
   21025              :           /* Handlers can refer to the function result; if that has been
   21026              :              moved, we need to track it.  */
   21027       346595 :           if (gimplify_ctxp->in_handler_expr && gimplify_ctxp->return_temp)
   21028           68 :             *expr_p = gimplify_ctxp->return_temp;
   21029              :           ret = GS_ALL_DONE;
   21030              :           break;
   21031              : 
   21032            0 :         case DEBUG_EXPR_DECL:
   21033            0 :           gcc_unreachable ();
   21034              : 
   21035      2483716 :         case DEBUG_BEGIN_STMT:
   21036      4967432 :           gimplify_seq_add_stmt (pre_p,
   21037              :                                  gimple_build_debug_begin_stmt
   21038      2483716 :                                  (TREE_BLOCK (*expr_p),
   21039      2483716 :                                   EXPR_LOCATION (*expr_p)));
   21040      2483716 :           ret = GS_ALL_DONE;
   21041      2483716 :           *expr_p = NULL;
   21042      2483716 :           break;
   21043              : 
   21044              :         case SSA_NAME:
   21045              :           /* Allow callbacks into the gimplifier during optimization.  */
   21046              :           ret = GS_ALL_DONE;
   21047              :           break;
   21048              : 
   21049        19176 :         case OMP_PARALLEL:
   21050        19176 :           gimplify_omp_parallel (expr_p, pre_p);
   21051        19176 :           ret = GS_ALL_DONE;
   21052        19176 :           break;
   21053              : 
   21054         4133 :         case OMP_TASK:
   21055         4133 :           gimplify_omp_task (expr_p, pre_p);
   21056         4133 :           ret = GS_ALL_DONE;
   21057         4133 :           break;
   21058              : 
   21059        11855 :         case OMP_SIMD:
   21060        11855 :           {
   21061              :             /* Temporarily disable into_ssa, as scan_omp_simd
   21062              :                which calls copy_gimple_seq_and_replace_locals can't deal
   21063              :                with SSA_NAMEs defined outside of the body properly.  */
   21064        11855 :             bool saved_into_ssa = gimplify_ctxp->into_ssa;
   21065        11855 :             gimplify_ctxp->into_ssa = false;
   21066        11855 :             ret = gimplify_omp_for (expr_p, pre_p);
   21067        11855 :             gimplify_ctxp->into_ssa = saved_into_ssa;
   21068        11855 :             break;
   21069              :           }
   21070              : 
   21071        49441 :         case OMP_FOR:
   21072        49441 :         case OMP_DISTRIBUTE:
   21073        49441 :         case OMP_TASKLOOP:
   21074        49441 :         case OMP_TILE:
   21075        49441 :         case OMP_UNROLL:
   21076        49441 :         case OACC_LOOP:
   21077        49441 :           ret = gimplify_omp_for (expr_p, pre_p);
   21078        49441 :           break;
   21079              : 
   21080         1075 :         case OMP_LOOP:
   21081         1075 :           ret = gimplify_omp_loop (expr_p, pre_p);
   21082         1075 :           break;
   21083              : 
   21084          717 :         case OACC_CACHE:
   21085          717 :           gimplify_oacc_cache (expr_p, pre_p);
   21086          717 :           ret = GS_ALL_DONE;
   21087          717 :           break;
   21088              : 
   21089          254 :         case OACC_DECLARE:
   21090          254 :           gimplify_oacc_declare (expr_p, pre_p);
   21091          254 :           ret = GS_ALL_DONE;
   21092          254 :           break;
   21093              : 
   21094        41760 :         case OACC_HOST_DATA:
   21095        41760 :         case OACC_DATA:
   21096        41760 :         case OACC_KERNELS:
   21097        41760 :         case OACC_PARALLEL:
   21098        41760 :         case OACC_SERIAL:
   21099        41760 :         case OMP_SCOPE:
   21100        41760 :         case OMP_SECTIONS:
   21101        41760 :         case OMP_SINGLE:
   21102        41760 :         case OMP_TARGET:
   21103        41760 :         case OMP_TARGET_DATA:
   21104        41760 :         case OMP_TEAMS:
   21105        41760 :           gimplify_omp_workshare (expr_p, pre_p);
   21106        41760 :           ret = GS_ALL_DONE;
   21107        41760 :           break;
   21108              : 
   21109        12758 :         case OACC_ENTER_DATA:
   21110        12758 :         case OACC_EXIT_DATA:
   21111        12758 :         case OACC_UPDATE:
   21112        12758 :         case OMP_TARGET_UPDATE:
   21113        12758 :         case OMP_TARGET_ENTER_DATA:
   21114        12758 :         case OMP_TARGET_EXIT_DATA:
   21115        12758 :           gimplify_omp_target_update (expr_p, pre_p);
   21116        12758 :           ret = GS_ALL_DONE;
   21117        12758 :           break;
   21118              : 
   21119         7096 :         case OMP_SECTION:
   21120         7096 :         case OMP_STRUCTURED_BLOCK:
   21121         7096 :         case OMP_MASTER:
   21122         7096 :         case OMP_MASKED:
   21123         7096 :         case OMP_ORDERED:
   21124         7096 :         case OMP_CRITICAL:
   21125         7096 :         case OMP_SCAN:
   21126         7096 :           {
   21127         7096 :             gimple_seq body = NULL;
   21128         7096 :             gimple *g;
   21129         7096 :             bool saved_in_omp_construct = in_omp_construct;
   21130              : 
   21131         7096 :             in_omp_construct = true;
   21132         7096 :             gimplify_and_add (OMP_BODY (*expr_p), &body);
   21133         7096 :             in_omp_construct = saved_in_omp_construct;
   21134         7096 :             switch (TREE_CODE (*expr_p))
   21135              :               {
   21136         1279 :               case OMP_SECTION:
   21137         1279 :                 g = gimple_build_omp_section (body);
   21138         1279 :                 break;
   21139          791 :               case OMP_STRUCTURED_BLOCK:
   21140          791 :                 g = gimple_build_omp_structured_block (body);
   21141          791 :                 break;
   21142          874 :               case OMP_MASTER:
   21143          874 :                 g = gimple_build_omp_master (body);
   21144          874 :                 break;
   21145         1954 :               case OMP_ORDERED:
   21146         1954 :                 g = gimplify_omp_ordered (*expr_p, body);
   21147         1954 :                 if (OMP_BODY (*expr_p) == NULL_TREE
   21148         1954 :                     && gimple_code (g) == GIMPLE_OMP_ORDERED)
   21149         1025 :                   gimple_omp_ordered_standalone (g);
   21150              :                 break;
   21151          500 :               case OMP_MASKED:
   21152          500 :                 gimplify_scan_omp_clauses (&OMP_MASKED_CLAUSES (*expr_p),
   21153              :                                            pre_p, ORT_WORKSHARE, OMP_MASKED);
   21154          500 :                 gimplify_adjust_omp_clauses (pre_p, body,
   21155              :                                              &OMP_MASKED_CLAUSES (*expr_p),
   21156              :                                              OMP_MASKED);
   21157         1000 :                 g = gimple_build_omp_masked (body,
   21158          500 :                                              OMP_MASKED_CLAUSES (*expr_p));
   21159          500 :                 break;
   21160          552 :               case OMP_CRITICAL:
   21161          552 :                 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
   21162              :                                            pre_p, ORT_WORKSHARE, OMP_CRITICAL);
   21163          552 :                 gimplify_adjust_omp_clauses (pre_p, body,
   21164              :                                              &OMP_CRITICAL_CLAUSES (*expr_p),
   21165              :                                              OMP_CRITICAL);
   21166         1656 :                 g = gimple_build_omp_critical (body,
   21167          552 :                                                OMP_CRITICAL_NAME (*expr_p),
   21168          552 :                                                OMP_CRITICAL_CLAUSES (*expr_p));
   21169          552 :                 break;
   21170         1146 :               case OMP_SCAN:
   21171         1146 :                 gimplify_scan_omp_clauses (&OMP_SCAN_CLAUSES (*expr_p),
   21172              :                                            pre_p, ORT_WORKSHARE, OMP_SCAN);
   21173         1146 :                 gimplify_adjust_omp_clauses (pre_p, body,
   21174              :                                              &OMP_SCAN_CLAUSES (*expr_p),
   21175              :                                              OMP_SCAN);
   21176         1146 :                 g = gimple_build_omp_scan (body, OMP_SCAN_CLAUSES (*expr_p));
   21177         1146 :                 break;
   21178            0 :               default:
   21179            0 :                 gcc_unreachable ();
   21180              :               }
   21181         7096 :             gimplify_seq_add_stmt (pre_p, g);
   21182         7096 :             ret = GS_ALL_DONE;
   21183         7096 :             break;
   21184              :           }
   21185              : 
   21186          611 :         case OMP_TASKGROUP:
   21187          611 :           {
   21188          611 :             gimple_seq body = NULL;
   21189              : 
   21190          611 :             tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
   21191          611 :             bool saved_in_omp_construct = in_omp_construct;
   21192          611 :             gimplify_scan_omp_clauses (pclauses, pre_p, ORT_TASKGROUP,
   21193              :                                        OMP_TASKGROUP);
   21194          611 :             gimplify_adjust_omp_clauses (pre_p, NULL, pclauses, OMP_TASKGROUP);
   21195              : 
   21196          611 :             in_omp_construct = true;
   21197          611 :             gimplify_and_add (OMP_BODY (*expr_p), &body);
   21198          611 :             in_omp_construct = saved_in_omp_construct;
   21199          611 :             gimple_seq cleanup = NULL;
   21200          611 :             tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
   21201          611 :             gimple *g = gimple_build_call (fn, 0);
   21202          611 :             gimple_seq_add_stmt (&cleanup, g);
   21203          611 :             g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
   21204          611 :             body = NULL;
   21205          611 :             gimple_seq_add_stmt (&body, g);
   21206          611 :             g = gimple_build_omp_taskgroup (body, *pclauses);
   21207          611 :             gimplify_seq_add_stmt (pre_p, g);
   21208          611 :             ret = GS_ALL_DONE;
   21209          611 :             break;
   21210              :           }
   21211              : 
   21212          612 :         case OMP_INTEROP:
   21213          612 :           ret = gimplify_omp_interop (expr_p, pre_p);
   21214          612 :           break;
   21215        10744 :         case OMP_ATOMIC:
   21216        10744 :         case OMP_ATOMIC_READ:
   21217        10744 :         case OMP_ATOMIC_CAPTURE_OLD:
   21218        10744 :         case OMP_ATOMIC_CAPTURE_NEW:
   21219        10744 :           ret = gimplify_omp_atomic (expr_p, pre_p);
   21220        10744 :           break;
   21221              : 
   21222          852 :         case OMP_DISPATCH:
   21223          852 :           ret = gimplify_omp_dispatch (expr_p, pre_p);
   21224          852 :           break;
   21225              : 
   21226          180 :         case OMP_METADIRECTIVE:
   21227          180 :           ret = gimplify_omp_metadirective (expr_p, pre_p, post_p,
   21228              :                                             gimple_test_f, fallback);
   21229          180 :           break;
   21230              : 
   21231           36 :         case OMP_NEXT_VARIANT:
   21232           36 :         case OMP_TARGET_DEVICE_MATCHES:
   21233              :           /* These are placeholders for constants.  There's nothing to do with
   21234              :              them here but we must mark the containing function as needing
   21235              :              to run the ompdevlow pass to resolve them.  Note that
   21236              :              OMP_TARGET_DEVICE_MATCHES, in particular, may be inserted by
   21237              :              the front ends.  */
   21238           36 :           cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
   21239           36 :           ret = GS_ALL_DONE;
   21240           36 :           break;
   21241              : 
   21242            0 :         case OMP_DECLARE_MAPPER:
   21243            0 :           ret = gimplify_omp_declare_mapper (expr_p);
   21244            0 :           break;
   21245              : 
   21246          478 :         case TRANSACTION_EXPR:
   21247          478 :           ret = gimplify_transaction (expr_p, pre_p);
   21248          478 :           break;
   21249              : 
   21250       294546 :         case TRUTH_AND_EXPR:
   21251       294546 :         case TRUTH_OR_EXPR:
   21252       294546 :         case TRUTH_XOR_EXPR:
   21253       294546 :           {
   21254       294546 :             tree orig_type = TREE_TYPE (*expr_p);
   21255       294546 :             tree new_type, xop0, xop1;
   21256       294546 :             *expr_p = gimple_boolify (*expr_p);
   21257       294546 :             new_type = TREE_TYPE (*expr_p);
   21258       294546 :             if (!useless_type_conversion_p (orig_type, new_type))
   21259              :               {
   21260         1202 :                 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
   21261         1202 :                 ret = GS_OK;
   21262         1202 :                 break;
   21263              :               }
   21264              : 
   21265              :           /* Boolified binary truth expressions are semantically equivalent
   21266              :              to bitwise binary expressions.  Canonicalize them to the
   21267              :              bitwise variant.  */
   21268       293344 :             switch (TREE_CODE (*expr_p))
   21269              :               {
   21270       177368 :               case TRUTH_AND_EXPR:
   21271       177368 :                 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
   21272       177368 :                 break;
   21273       115343 :               case TRUTH_OR_EXPR:
   21274       115343 :                 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
   21275       115343 :                 break;
   21276          633 :               case TRUTH_XOR_EXPR:
   21277          633 :                 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
   21278          633 :                 break;
   21279              :               default:
   21280              :                 break;
   21281              :               }
   21282              :             /* Now make sure that operands have compatible type to
   21283              :                expression's new_type.  */
   21284       293344 :             xop0 = TREE_OPERAND (*expr_p, 0);
   21285       293344 :             xop1 = TREE_OPERAND (*expr_p, 1);
   21286       293344 :             if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
   21287          326 :               TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
   21288              :                                                             new_type,
   21289              :                                                             xop0);
   21290       293344 :             if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
   21291          366 :               TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
   21292              :                                                             new_type,
   21293              :                                                             xop1);
   21294              :             /* Continue classified as tcc_binary.  */
   21295       293344 :             goto expr_2;
   21296              :           }
   21297              : 
   21298        11456 :         case VEC_COND_EXPR:
   21299        11456 :           goto expr_3;
   21300              : 
   21301       134535 :         case VEC_PERM_EXPR:
   21302              :           /* Classified as tcc_expression.  */
   21303       134535 :           goto expr_3;
   21304              : 
   21305           48 :         case BIT_INSERT_EXPR:
   21306              :           /* Argument 3 is a constant.  */
   21307           48 :           goto expr_2;
   21308              : 
   21309      3879693 :         case POINTER_PLUS_EXPR:
   21310      3879693 :           {
   21311      3879693 :             enum gimplify_status r0, r1;
   21312      3879693 :             r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
   21313              :                                 post_p, is_gimple_val, fb_rvalue);
   21314      3879693 :             r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
   21315              :                                 post_p, is_gimple_val, fb_rvalue);
   21316      3879693 :             recalculate_side_effects (*expr_p);
   21317      3879693 :             ret = MIN (r0, r1);
   21318              :             break;
   21319              :           }
   21320              : 
   21321     45688810 :         default:
   21322     45688810 :           switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
   21323              :             {
   21324      7147504 :             case tcc_comparison:
   21325              :               /* Handle comparison of objects of non scalar mode aggregates
   21326              :                  with a call to memcmp.  It would be nice to only have to do
   21327              :                  this for variable-sized objects, but then we'd have to allow
   21328              :                  the same nest of reference nodes we allow for MODIFY_EXPR and
   21329              :                  that's too complex.
   21330              : 
   21331              :                  Compare scalar mode aggregates as scalar mode values.  Using
   21332              :                  memcmp for them would be very inefficient at best, and is
   21333              :                  plain wrong if bitfields are involved.  */
   21334      7147504 :               if (error_operand_p (TREE_OPERAND (*expr_p, 1)))
   21335              :                 ret = GS_ERROR;
   21336              :               else
   21337              :                 {
   21338      7147504 :                   tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
   21339              : 
   21340              :                   /* Vector comparisons need no boolification.  */
   21341      7147504 :                   if (TREE_CODE (type) == VECTOR_TYPE)
   21342        13642 :                     goto expr_2;
   21343      7133862 :                   else if (!AGGREGATE_TYPE_P (type))
   21344              :                     {
   21345      7133846 :                       tree org_type = TREE_TYPE (*expr_p);
   21346      7133846 :                       *expr_p = gimple_boolify (*expr_p);
   21347      7133846 :                       if (!useless_type_conversion_p (org_type,
   21348      7133846 :                                                       TREE_TYPE (*expr_p)))
   21349              :                         {
   21350        59011 :                           *expr_p = fold_convert_loc (input_location,
   21351              :                                                       org_type, *expr_p);
   21352        59011 :                           ret = GS_OK;
   21353              :                         }
   21354              :                       else
   21355      7074835 :                         goto expr_2;
   21356              :                     }
   21357           16 :                   else if (SCALAR_INT_MODE_P (TYPE_MODE (type)))
   21358           16 :                     ret = gimplify_scalar_mode_aggregate_compare (expr_p);
   21359              :                   else
   21360            0 :                     ret = gimplify_variable_sized_compare (expr_p);
   21361              :                 }
   21362              :               break;
   21363              : 
   21364              :             /* If *EXPR_P does not need to be special-cased, handle it
   21365              :                according to its class.  */
   21366      1628429 :             case tcc_unary:
   21367      1628429 :               ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
   21368              :                                    post_p, is_gimple_val, fb_rvalue);
   21369      1628429 :               break;
   21370              : 
   21371     27129188 :             case tcc_binary:
   21372     27129188 :             expr_2:
   21373     27129188 :               {
   21374     27129188 :                 enum gimplify_status r0, r1;
   21375              : 
   21376     27129188 :                 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
   21377              :                                     post_p, is_gimple_val, fb_rvalue);
   21378     27129188 :                 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
   21379              :                                     post_p, is_gimple_val, fb_rvalue);
   21380              : 
   21381     27129188 :                 ret = MIN (r0, r1);
   21382              :                 break;
   21383              :               }
   21384              : 
   21385       145991 :             expr_3:
   21386       145991 :               {
   21387       145991 :                 enum gimplify_status r0, r1, r2;
   21388              : 
   21389       145991 :                 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
   21390              :                                     post_p, is_gimple_val, fb_rvalue);
   21391       145991 :                 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
   21392              :                                     post_p, is_gimple_val, fb_rvalue);
   21393       145991 :                 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
   21394              :                                     post_p, is_gimple_val, fb_rvalue);
   21395              : 
   21396       145991 :                 ret = MIN (MIN (r0, r1), r2);
   21397              :                 break;
   21398              :               }
   21399              : 
   21400     17165558 :             case tcc_declaration:
   21401     17165558 :             case tcc_constant:
   21402     17165558 :               ret = GS_ALL_DONE;
   21403     17165558 :               goto dont_recalculate;
   21404              : 
   21405            0 :             default:
   21406            0 :               gcc_unreachable ();
   21407              :             }
   21408              : 
   21409     28962635 :           recalculate_side_effects (*expr_p);
   21410              : 
   21411    494408806 :         dont_recalculate:
   21412              :           break;
   21413              :         }
   21414              : 
   21415    494408806 :       gcc_assert (*expr_p || ret != GS_OK);
   21416              :     }
   21417    499375317 :   while (ret == GS_OK);
   21418              : 
   21419              :   /* If we encountered an error_mark somewhere nested inside, either
   21420              :      stub out the statement or propagate the error back out.  */
   21421    481053678 :   if (ret == GS_ERROR)
   21422              :     {
   21423         5391 :       if (is_statement)
   21424         4164 :         *expr_p = NULL;
   21425         5391 :       goto out;
   21426              :     }
   21427              : 
   21428              :   /* This was only valid as a return value from the langhook, which
   21429              :      we handled.  Make sure it doesn't escape from any other context.  */
   21430    481048287 :   gcc_assert (ret != GS_UNHANDLED);
   21431              : 
   21432    481048287 :   if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
   21433              :     {
   21434              :       /* We aren't looking for a value, and we don't have a valid
   21435              :          statement.  If it doesn't have side-effects, throw it away.
   21436              :          We can also get here with code such as "*&&L;", where L is
   21437              :          a LABEL_DECL that is marked as FORCED_LABEL.  */
   21438      1250915 :       if (TREE_CODE (*expr_p) == LABEL_DECL
   21439      1250915 :           || !TREE_SIDE_EFFECTS (*expr_p))
   21440      1249878 :         *expr_p = NULL;
   21441         1037 :       else if (!TREE_THIS_VOLATILE (*expr_p))
   21442              :         {
   21443              :           /* This is probably a _REF that contains something nested that
   21444              :              has side effects.  Recurse through the operands to find it.  */
   21445            0 :           enum tree_code code = TREE_CODE (*expr_p);
   21446              : 
   21447            0 :           switch (code)
   21448              :             {
   21449            0 :             case COMPONENT_REF:
   21450            0 :             case REALPART_EXPR:
   21451            0 :             case IMAGPART_EXPR:
   21452            0 :             case VIEW_CONVERT_EXPR:
   21453            0 :               gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
   21454              :                              gimple_test_f, fallback);
   21455            0 :               break;
   21456              : 
   21457            0 :             case ARRAY_REF:
   21458            0 :             case ARRAY_RANGE_REF:
   21459            0 :               gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
   21460              :                              gimple_test_f, fallback);
   21461            0 :               gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
   21462              :                              gimple_test_f, fallback);
   21463            0 :               break;
   21464              : 
   21465            0 :             default:
   21466              :                /* Anything else with side-effects must be converted to
   21467              :                   a valid statement before we get here.  */
   21468            0 :               gcc_unreachable ();
   21469              :             }
   21470              : 
   21471            0 :           *expr_p = NULL;
   21472              :         }
   21473         1037 :       else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
   21474          987 :                && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode
   21475         2011 :                && !is_empty_type (TREE_TYPE (*expr_p)))
   21476              :         {
   21477              :           /* Historically, the compiler has treated a bare reference
   21478              :              to a non-BLKmode volatile lvalue as forcing a load.  */
   21479          914 :           tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
   21480              : 
   21481              :           /* Normally, we do not want to create a temporary for a
   21482              :              TREE_ADDRESSABLE type because such a type should not be
   21483              :              copied by bitwise-assignment.  However, we make an
   21484              :              exception here, as all we are doing here is ensuring that
   21485              :              we read the bytes that make up the type.  We use
   21486              :              create_tmp_var_raw because create_tmp_var will abort when
   21487              :              given a TREE_ADDRESSABLE type.  */
   21488          914 :           tree tmp = create_tmp_var_raw (type, "vol");
   21489          914 :           gimple_add_tmp_var (tmp);
   21490          914 :           gimplify_assign (tmp, *expr_p, pre_p);
   21491          914 :           *expr_p = NULL;
   21492              :         }
   21493              :       else
   21494              :         /* We can't do anything useful with a volatile reference to
   21495              :            an incomplete type, so just throw it away.  Likewise for
   21496              :            a BLKmode type, since any implicit inner load should
   21497              :            already have been turned into an explicit one by the
   21498              :            gimplification process.  */
   21499          123 :         *expr_p = NULL;
   21500              :     }
   21501              : 
   21502              :   /* If we are gimplifying at the statement level, we're done.  Tack
   21503              :      everything together and return.  */
   21504    481048287 :   if (fallback == fb_none || is_statement)
   21505              :     {
   21506              :       /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
   21507              :          it out for GC to reclaim it.  */
   21508    104119194 :       *expr_p = NULL_TREE;
   21509              : 
   21510    104119194 :       if (!gimple_seq_empty_p (internal_pre)
   21511    104119194 :           || !gimple_seq_empty_p (internal_post))
   21512              :         {
   21513           19 :           gimplify_seq_add_seq (&internal_pre, internal_post);
   21514           19 :           gimplify_seq_add_seq (pre_p, internal_pre);
   21515              :         }
   21516              : 
   21517              :       /* The result of gimplifying *EXPR_P is going to be the last few
   21518              :          statements in *PRE_P and *POST_P.  Add location information
   21519              :          to all the statements that were added by the gimplification
   21520              :          helpers.  */
   21521    104119194 :       if (!gimple_seq_empty_p (*pre_p))
   21522    101553269 :         annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
   21523              : 
   21524    104119194 :       if (!gimple_seq_empty_p (*post_p))
   21525           19 :         annotate_all_with_location_after (*post_p, post_last_gsi,
   21526              :                                           input_location);
   21527              : 
   21528    104119194 :       goto out;
   21529              :     }
   21530              : 
   21531              : #ifdef ENABLE_GIMPLE_CHECKING
   21532    376929093 :   if (*expr_p)
   21533              :     {
   21534    376929093 :       enum tree_code code = TREE_CODE (*expr_p);
   21535              :       /* These expressions should already be in gimple IR form.  */
   21536    376929093 :       gcc_assert (code != MODIFY_EXPR
   21537              :                   && code != ASM_EXPR
   21538              :                   && code != BIND_EXPR
   21539              :                   && code != CATCH_EXPR
   21540              :                   && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
   21541              :                   && code != EH_FILTER_EXPR
   21542              :                   && code != GOTO_EXPR
   21543              :                   && code != LABEL_EXPR
   21544              :                   && code != LOOP_EXPR
   21545              :                   && code != SWITCH_EXPR
   21546              :                   && code != TRY_FINALLY_EXPR
   21547              :                   && code != EH_ELSE_EXPR
   21548              :                   && code != OACC_PARALLEL
   21549              :                   && code != OACC_KERNELS
   21550              :                   && code != OACC_SERIAL
   21551              :                   && code != OACC_DATA
   21552              :                   && code != OACC_HOST_DATA
   21553              :                   && code != OACC_DECLARE
   21554              :                   && code != OACC_UPDATE
   21555              :                   && code != OACC_ENTER_DATA
   21556              :                   && code != OACC_EXIT_DATA
   21557              :                   && code != OACC_CACHE
   21558              :                   && code != OMP_CRITICAL
   21559              :                   && code != OMP_FOR
   21560              :                   && code != OACC_LOOP
   21561              :                   && code != OMP_MASTER
   21562              :                   && code != OMP_MASKED
   21563              :                   && code != OMP_TASKGROUP
   21564              :                   && code != OMP_ORDERED
   21565              :                   && code != OMP_PARALLEL
   21566              :                   && code != OMP_SCAN
   21567              :                   && code != OMP_SECTIONS
   21568              :                   && code != OMP_SECTION
   21569              :                   && code != OMP_STRUCTURED_BLOCK
   21570              :                   && code != OMP_SINGLE
   21571              :                   && code != OMP_SCOPE
   21572              :                   && code != OMP_DISPATCH);
   21573              :     }
   21574              : #endif
   21575              : 
   21576              :   /* Otherwise we're gimplifying a subexpression, so the resulting
   21577              :      value is interesting.  If it's a valid operand that matches
   21578              :      GIMPLE_TEST_F, we're done. Unless we are handling some
   21579              :      post-effects internally; if that's the case, we need to copy into
   21580              :      a temporary before adding the post-effects to POST_P.  */
   21581    376929093 :   if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
   21582    350162509 :     goto out;
   21583              : 
   21584              :   /* Otherwise, we need to create a new temporary for the gimplified
   21585              :      expression.  */
   21586              : 
   21587              :   /* We can't return an lvalue if we have an internal postqueue.  The
   21588              :      object the lvalue refers to would (probably) be modified by the
   21589              :      postqueue; we need to copy the value out first, which means an
   21590              :      rvalue.  */
   21591     26766584 :   if ((fallback & fb_lvalue)
   21592       600115 :       && gimple_seq_empty_p (internal_post)
   21593     27366699 :       && is_gimple_addressable (*expr_p))
   21594              :     {
   21595              :       /* An lvalue will do.  Take the address of the expression, store it
   21596              :          in a temporary, and replace the expression with an INDIRECT_REF of
   21597              :          that temporary.  */
   21598           15 :       tree ref_alias_type = reference_alias_ptr_type (*expr_p);
   21599           15 :       unsigned int ref_align = get_object_alignment (*expr_p);
   21600           15 :       tree ref_type = TREE_TYPE (*expr_p);
   21601           15 :       tmp = build_fold_addr_expr_loc (input_location, *expr_p);
   21602           15 :       gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
   21603           15 :       if (TYPE_ALIGN (ref_type) != ref_align)
   21604            1 :         ref_type = build_aligned_type (ref_type, ref_align);
   21605           15 :       *expr_p = build2 (MEM_REF, ref_type,
   21606              :                         tmp, build_zero_cst (ref_alias_type));
   21607              :     }
   21608     26766569 :   else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
   21609              :     {
   21610              :       /* An rvalue will do.  Assign the gimplified expression into a
   21611              :          new temporary TMP and replace the original expression with
   21612              :          TMP.  First, make sure that the expression has a type so that
   21613              :          it can be assigned into a temporary.  */
   21614     26766561 :       gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
   21615     26766561 :       *expr_p = get_formal_tmp_var (*expr_p, pre_p);
   21616              :     }
   21617              :   else
   21618              :     {
   21619              : #ifdef ENABLE_GIMPLE_CHECKING
   21620            8 :       if (!(fallback & fb_mayfail))
   21621              :         {
   21622            0 :           fprintf (stderr, "gimplification failed:\n");
   21623            0 :           print_generic_expr (stderr, *expr_p);
   21624            0 :           debug_tree (*expr_p);
   21625            0 :           internal_error ("gimplification failed");
   21626              :         }
   21627              : #endif
   21628            8 :       gcc_assert (fallback & fb_mayfail);
   21629              : 
   21630              :       /* If this is an asm statement, and the user asked for the
   21631              :          impossible, don't die.  Fail and let gimplify_asm_expr
   21632              :          issue an error.  */
   21633            8 :       ret = GS_ERROR;
   21634            8 :       goto out;
   21635              :     }
   21636              : 
   21637              :   /* Make sure the temporary matches our predicate.  */
   21638     26766576 :   gcc_assert ((*gimple_test_f) (*expr_p));
   21639              : 
   21640     26766576 :   if (!gimple_seq_empty_p (internal_post))
   21641              :     {
   21642            0 :       annotate_all_with_location (internal_post, input_location);
   21643            0 :       gimplify_seq_add_seq (pre_p, internal_post);
   21644              :     }
   21645              : 
   21646     26766576 :  out:
   21647    481053678 :   input_location = saved_location;
   21648    481053678 :   return ret;
   21649              : }
   21650              : 
   21651              : /* Like gimplify_expr but make sure the gimplified result is not itself
   21652              :    a SSA name (but a decl if it were).  Temporaries required by
   21653              :    evaluating *EXPR_P may be still SSA names.  */
   21654              : 
   21655              : static enum gimplify_status
   21656     35221054 : gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
   21657              :                bool (*gimple_test_f) (tree), fallback_t fallback,
   21658              :                bool allow_ssa)
   21659              : {
   21660     35221054 :   enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
   21661              :                                             gimple_test_f, fallback);
   21662     35221054 :   if (! allow_ssa
   21663       278173 :       && TREE_CODE (*expr_p) == SSA_NAME)
   21664        68835 :     *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
   21665     35221054 :   return ret;
   21666              : }
   21667              : 
   21668              : /* Look through TYPE for variable-sized objects and gimplify each such
   21669              :    size that we find.  Add to LIST_P any statements generated.  */
   21670              : 
   21671              : void
   21672      8429951 : gimplify_type_sizes (tree type, gimple_seq *list_p)
   21673              : {
   21674      8429951 :   if (type == NULL || type == error_mark_node)
   21675              :     return;
   21676              : 
   21677      8429705 :   const bool ignored_p
   21678      8429705 :     = TYPE_NAME (type)
   21679      4206103 :       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
   21680     12394234 :       && DECL_IGNORED_P (TYPE_NAME (type));
   21681      8429705 :   tree t;
   21682              : 
   21683              :   /* We first do the main variant, then copy into any other variants.  */
   21684      8429705 :   type = TYPE_MAIN_VARIANT (type);
   21685              : 
   21686              :   /* Avoid infinite recursion.  */
   21687      8429705 :   if (TYPE_SIZES_GIMPLIFIED (type))
   21688              :     return;
   21689              : 
   21690      2515398 :   TYPE_SIZES_GIMPLIFIED (type) = 1;
   21691              : 
   21692      2515398 :   switch (TREE_CODE (type))
   21693              :     {
   21694       506993 :     case INTEGER_TYPE:
   21695       506993 :     case ENUMERAL_TYPE:
   21696       506993 :     case BOOLEAN_TYPE:
   21697       506993 :     case REAL_TYPE:
   21698       506993 :     case FIXED_POINT_TYPE:
   21699       506993 :       gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
   21700       506993 :       gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
   21701              : 
   21702     10138484 :       for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
   21703              :         {
   21704      9631491 :           TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
   21705      9631491 :           TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
   21706              :         }
   21707              :       break;
   21708              : 
   21709       185109 :     case ARRAY_TYPE:
   21710              :       /* These types may not have declarations, so handle them here.  */
   21711       185109 :       gimplify_type_sizes (TREE_TYPE (type), list_p);
   21712       185109 :       gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
   21713              :       /* Ensure VLA bounds aren't removed, for -O0 they should be variables
   21714              :          with assigned stack slots, for -O1+ -g they should be tracked
   21715              :          by VTA.  */
   21716       185109 :       if (!ignored_p
   21717       185109 :           && TYPE_DOMAIN (type)
   21718       369993 :           && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
   21719              :         {
   21720       184884 :           t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
   21721       184884 :           if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
   21722            0 :             DECL_IGNORED_P (t) = 0;
   21723       184884 :           t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
   21724       184884 :           if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
   21725        19203 :             DECL_IGNORED_P (t) = 0;
   21726              :         }
   21727              :       break;
   21728              : 
   21729       507764 :     case RECORD_TYPE:
   21730       507764 :     case UNION_TYPE:
   21731       507764 :     case QUAL_UNION_TYPE:
   21732     11155158 :       for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
   21733     10647394 :         if (TREE_CODE (field) == FIELD_DECL)
   21734              :           {
   21735      1101406 :             gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
   21736              :             /* Likewise, ensure variable offsets aren't removed.  */
   21737      1101406 :             if (!ignored_p
   21738      1101406 :                 && (t = DECL_FIELD_OFFSET (field))
   21739      1101394 :                 && VAR_P (t)
   21740      1101568 :                 && DECL_ARTIFICIAL (t))
   21741          162 :               DECL_IGNORED_P (t) = 0;
   21742      1101406 :             gimplify_one_sizepos (&DECL_SIZE (field), list_p);
   21743      1101406 :             gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
   21744      1101406 :             gimplify_type_sizes (TREE_TYPE (field), list_p);
   21745              :           }
   21746              :       break;
   21747              : 
   21748              :     case POINTER_TYPE:
   21749              :     case REFERENCE_TYPE:
   21750              :         /* We used to recurse on the pointed-to type here, which turned out to
   21751              :            be incorrect because its definition might refer to variables not
   21752              :            yet initialized at this point if a forward declaration is involved.
   21753              : 
   21754              :            It was actually useful for anonymous pointed-to types to ensure
   21755              :            that the sizes evaluation dominates every possible later use of the
   21756              :            values.  Restricting to such types here would be safe since there
   21757              :            is no possible forward declaration around, but would introduce an
   21758              :            undesirable middle-end semantic to anonymity.  We then defer to
   21759              :            front-ends the responsibility of ensuring that the sizes are
   21760              :            evaluated both early and late enough, e.g. by attaching artificial
   21761              :            type declarations to the tree.  */
   21762              :       break;
   21763              : 
   21764              :     default:
   21765              :       break;
   21766              :     }
   21767              : 
   21768      2515398 :   gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
   21769      2515398 :   gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
   21770              : 
   21771     16731066 :   for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
   21772              :     {
   21773     14215668 :       TYPE_SIZE (t) = TYPE_SIZE (type);
   21774     14215668 :       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
   21775     14215668 :       TYPE_SIZES_GIMPLIFIED (t) = 1;
   21776              :     }
   21777              : }
   21778              : 
   21779              : /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
   21780              :    a size or position, has had all of its SAVE_EXPRs evaluated.
   21781              :    We add any required statements to *STMT_P.  */
   21782              : 
   21783              : void
   21784      9367304 : gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
   21785              : {
   21786      9367304 :   tree expr = *expr_p;
   21787              : 
   21788              :   /* We don't do anything if the value isn't there, is constant, or contains
   21789              :      A PLACEHOLDER_EXPR.  We also don't want to do anything if it's already
   21790              :      a VAR_DECL.  If it's a VAR_DECL from another function, the gimplifier
   21791              :      will want to replace it with a new variable, but that will cause problems
   21792              :      if this type is from outside the function.  It's OK to have that here.  */
   21793      9367304 :   if (expr == NULL_TREE
   21794      9449177 :       || is_gimple_constant (expr)
   21795        81873 :       || VAR_P (expr)
   21796      9444990 :       || CONTAINS_PLACEHOLDER_P (expr))
   21797              :     return;
   21798              : 
   21799        77686 :   *expr_p = unshare_expr (expr);
   21800              : 
   21801              :   /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
   21802              :      if the def vanishes.  */
   21803        77686 :   gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
   21804              : 
   21805              :   /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
   21806              :      FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
   21807              :      as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs.  */
   21808        77686 :   if (is_gimple_constant (*expr_p))
   21809          548 :     *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
   21810              : }
   21811              : 
   21812              : /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
   21813              :    containing the sequence of corresponding GIMPLE statements.  If DO_PARMS
   21814              :    is true, also gimplify the parameters.  */
   21815              : 
   21816              : gbind *
   21817      3020530 : gimplify_body (tree fndecl, bool do_parms)
   21818              : {
   21819      3020530 :   location_t saved_location = input_location;
   21820      3020530 :   gimple_seq parm_stmts, parm_cleanup = NULL, seq;
   21821      3020530 :   gimple *outer_stmt;
   21822      3020530 :   gbind *outer_bind;
   21823              : 
   21824      3020530 :   timevar_push (TV_TREE_GIMPLIFY);
   21825              : 
   21826      3020530 :   init_tree_ssa (cfun);
   21827              : 
   21828              :   /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
   21829              :      gimplification.  */
   21830      3020530 :   default_rtl_profile ();
   21831              : 
   21832      3020530 :   gcc_assert (gimplify_ctxp == NULL);
   21833      3020530 :   push_gimplify_context (true);
   21834              : 
   21835      3020530 :   if (flag_openacc || flag_openmp)
   21836              :     {
   21837        58859 :       gcc_assert (gimplify_omp_ctxp == NULL);
   21838        58859 :       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
   21839         9805 :         gimplify_omp_ctxp = new_omp_context (ORT_IMPLICIT_TARGET);
   21840              :     }
   21841              : 
   21842              :   /* Unshare most shared trees in the body and in that of any nested functions.
   21843              :      It would seem we don't have to do this for nested functions because
   21844              :      they are supposed to be output and then the outer function gimplified
   21845              :      first, but the g++ front end doesn't always do it that way.  */
   21846      3020530 :   unshare_body (fndecl);
   21847      3020530 :   unvisit_body (fndecl);
   21848              : 
   21849              :   /* Make sure input_location isn't set to something weird.  */
   21850      3020530 :   input_location = DECL_SOURCE_LOCATION (fndecl);
   21851              : 
   21852              :   /* Resolve callee-copies.  This has to be done before processing
   21853              :      the body so that DECL_VALUE_EXPR gets processed correctly.  */
   21854      3020530 :   parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
   21855              : 
   21856              :   /* Gimplify the function's body.  */
   21857      3020530 :   seq = NULL;
   21858      3020530 :   gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
   21859      3020530 :   outer_stmt = gimple_seq_first_nondebug_stmt (seq);
   21860      3020530 :   if (!outer_stmt)
   21861              :     {
   21862        27439 :       outer_stmt = gimple_build_nop ();
   21863        27439 :       gimplify_seq_add_stmt (&seq, outer_stmt);
   21864              :     }
   21865              : 
   21866              :   /* The body must contain exactly one statement, a GIMPLE_BIND.  If this is
   21867              :      not the case, wrap everything in a GIMPLE_BIND to make it so.  */
   21868      3020530 :   if (gimple_code (outer_stmt) == GIMPLE_BIND
   21869      3020530 :       && (gimple_seq_first_nondebug_stmt (seq)
   21870      1757857 :           == gimple_seq_last_nondebug_stmt (seq)))
   21871              :     {
   21872      1691087 :       outer_bind = as_a <gbind *> (outer_stmt);
   21873      1691087 :       if (gimple_seq_first_stmt (seq) != outer_stmt
   21874      1691087 :           || gimple_seq_last_stmt (seq) != outer_stmt)
   21875              :         {
   21876              :           /* If there are debug stmts before or after outer_stmt, move them
   21877              :              inside of outer_bind body.  */
   21878            1 :           gimple_stmt_iterator gsi = gsi_for_stmt (outer_stmt, &seq);
   21879            1 :           gimple_seq second_seq = NULL;
   21880            1 :           if (gimple_seq_first_stmt (seq) != outer_stmt
   21881            2 :               && gimple_seq_last_stmt (seq) != outer_stmt)
   21882              :             {
   21883            0 :               second_seq = gsi_split_seq_after (gsi);
   21884            0 :               gsi_remove (&gsi, false);
   21885              :             }
   21886            1 :           else if (gimple_seq_first_stmt (seq) != outer_stmt)
   21887            1 :             gsi_remove (&gsi, false);
   21888              :           else
   21889              :             {
   21890            0 :               gsi_remove (&gsi, false);
   21891            0 :               second_seq = seq;
   21892            0 :               seq = NULL;
   21893              :             }
   21894            1 :           gimple_seq_add_seq_without_update (&seq,
   21895              :                                              gimple_bind_body (outer_bind));
   21896            1 :           gimple_seq_add_seq_without_update (&seq, second_seq);
   21897            1 :           gimple_bind_set_body (outer_bind, seq);
   21898              :         }
   21899              :     }
   21900              :   else
   21901      1329443 :     outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
   21902              : 
   21903      3020530 :   DECL_SAVED_TREE (fndecl) = NULL_TREE;
   21904              : 
   21905              :   /* If we had callee-copies statements, insert them at the beginning
   21906              :      of the function and clear DECL_HAS_VALUE_EXPR_P on the parameters.  */
   21907      3020530 :   if (!gimple_seq_empty_p (parm_stmts))
   21908              :     {
   21909           41 :       tree parm;
   21910              : 
   21911           41 :       gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
   21912           41 :       if (parm_cleanup)
   21913              :         {
   21914            0 :           gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
   21915              :                                       GIMPLE_TRY_FINALLY);
   21916            0 :           parm_stmts = NULL;
   21917            0 :           gimple_seq_add_stmt (&parm_stmts, g);
   21918              :         }
   21919           41 :       gimple_bind_set_body (outer_bind, parm_stmts);
   21920              : 
   21921           41 :       for (parm = DECL_ARGUMENTS (current_function_decl);
   21922          101 :            parm; parm = DECL_CHAIN (parm))
   21923           60 :         if (DECL_HAS_VALUE_EXPR_P (parm))
   21924              :           {
   21925            0 :             DECL_HAS_VALUE_EXPR_P (parm) = 0;
   21926            0 :             DECL_IGNORED_P (parm) = 0;
   21927              :           }
   21928              :     }
   21929              : 
   21930      3020530 :   if ((flag_openacc || flag_openmp || flag_openmp_simd)
   21931        61154 :       && gimplify_omp_ctxp)
   21932              :     {
   21933         9858 :       delete_omp_context (gimplify_omp_ctxp);
   21934         9858 :       gimplify_omp_ctxp = NULL;
   21935              :     }
   21936              : 
   21937      3020530 :   pop_gimplify_context (outer_bind);
   21938      3020530 :   gcc_assert (gimplify_ctxp == NULL);
   21939              : 
   21940      3020530 :   if (flag_checking && !seen_error ())
   21941      2968190 :     verify_gimple_in_seq (gimple_bind_body (outer_bind));
   21942              : 
   21943      3020530 :   timevar_pop (TV_TREE_GIMPLIFY);
   21944      3020530 :   input_location = saved_location;
   21945              : 
   21946      3020530 :   return outer_bind;
   21947              : }
   21948              : 
   21949              : typedef char *char_p; /* For DEF_VEC_P.  */
   21950              : 
   21951              : /* Return whether we should exclude FNDECL from instrumentation.  */
   21952              : 
   21953              : static bool
   21954           43 : flag_instrument_functions_exclude_p (tree fndecl)
   21955              : {
   21956           43 :   vec<char_p> *v;
   21957              : 
   21958           43 :   v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
   21959           44 :   if (v && v->length () > 0)
   21960              :     {
   21961            1 :       const char *name;
   21962            1 :       int i;
   21963            1 :       char *s;
   21964              : 
   21965            1 :       name = lang_hooks.decl_printable_name (fndecl, 1);
   21966            2 :       FOR_EACH_VEC_ELT (*v, i, s)
   21967            1 :         if (strstr (name, s) != NULL)
   21968           43 :           return true;
   21969              :     }
   21970              : 
   21971           42 :   v = (vec<char_p> *) flag_instrument_functions_exclude_files;
   21972           43 :   if (v && v->length () > 0)
   21973              :     {
   21974            1 :       const char *name;
   21975            1 :       int i;
   21976            1 :       char *s;
   21977              : 
   21978            1 :       name = DECL_SOURCE_FILE (fndecl);
   21979            1 :       FOR_EACH_VEC_ELT (*v, i, s)
   21980            1 :         if (strstr (name, s) != NULL)
   21981           43 :           return true;
   21982              :     }
   21983              : 
   21984              :   return false;
   21985              : }
   21986              : 
   21987              : /* Build a call to the instrumentation function FNCODE and add it to SEQ.
   21988              :    If COND_VAR is not NULL, it is a boolean variable guarding the call to
   21989              :    the instrumentation function.  IF STMT is not NULL, it is a statement
   21990              :    to be executed just before the call to the instrumentation function.  */
   21991              : 
   21992              : static void
   21993           82 : build_instrumentation_call (gimple_seq *seq, enum built_in_function fncode,
   21994              :                             tree cond_var, gimple *stmt)
   21995              : {
   21996              :   /* The instrumentation hooks aren't going to call the instrumented
   21997              :      function and the address they receive is expected to be matchable
   21998              :      against symbol addresses.  Make sure we don't create a trampoline,
   21999              :      in case the current function is nested.  */
   22000           82 :   tree this_fn_addr = build_fold_addr_expr (current_function_decl);
   22001           82 :   TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
   22002              : 
   22003           82 :   tree label_true, label_false;
   22004           82 :   if (cond_var)
   22005              :     {
   22006           20 :       label_true = create_artificial_label (UNKNOWN_LOCATION);
   22007           20 :       label_false = create_artificial_label (UNKNOWN_LOCATION);
   22008           20 :       gcond *cond = gimple_build_cond (EQ_EXPR, cond_var, boolean_false_node,
   22009              :                                       label_true, label_false);
   22010           20 :       gimplify_seq_add_stmt (seq, cond);
   22011           20 :       gimplify_seq_add_stmt (seq, gimple_build_label (label_true));
   22012           20 :       gimplify_seq_add_stmt (seq, gimple_build_predict (PRED_COLD_LABEL,
   22013              :                                                         NOT_TAKEN));
   22014              :     }
   22015              : 
   22016           82 :   if (stmt)
   22017           10 :     gimplify_seq_add_stmt (seq, stmt);
   22018              : 
   22019           82 :   tree x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
   22020           82 :   gcall *call = gimple_build_call (x, 1, integer_zero_node);
   22021           82 :   tree tmp_var = create_tmp_var (ptr_type_node, "return_addr");
   22022           82 :   gimple_call_set_lhs (call, tmp_var);
   22023           82 :   gimplify_seq_add_stmt (seq, call);
   22024           82 :   x = builtin_decl_implicit (fncode);
   22025           82 :   call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
   22026           82 :   gimplify_seq_add_stmt (seq, call);
   22027              : 
   22028           82 :   if (cond_var)
   22029           20 :     gimplify_seq_add_stmt (seq, gimple_build_label (label_false));
   22030           82 : }
   22031              : 
   22032              : /* Entry point to the gimplification pass.  FNDECL is the FUNCTION_DECL
   22033              :    node for the function we want to gimplify.
   22034              : 
   22035              :    Return the sequence of GIMPLE statements corresponding to the body
   22036              :    of FNDECL.  */
   22037              : 
   22038              : void
   22039      3020016 : gimplify_function_tree (tree fndecl)
   22040              : {
   22041      3020016 :   gimple_seq seq;
   22042      3020016 :   gbind *bind;
   22043              : 
   22044      3020016 :   gcc_assert (!gimple_body (fndecl));
   22045              : 
   22046      3020016 :   if (DECL_STRUCT_FUNCTION (fndecl))
   22047      3015586 :     push_cfun (DECL_STRUCT_FUNCTION (fndecl));
   22048              :   else
   22049         4430 :     push_struct_function (fndecl);
   22050              : 
   22051      3020016 :   reset_cond_uid ();
   22052      3020016 :   if (cond_uids)
   22053              :     {
   22054           68 :       delete cond_uids;
   22055           68 :       cond_uids = NULL;
   22056              :     }
   22057              : 
   22058              :   /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
   22059              :      if necessary.  */
   22060      3020016 :   cfun->curr_properties |= PROP_gimple_lva;
   22061              : 
   22062      3020016 :   if (asan_sanitize_use_after_scope ())
   22063         9061 :     asan_poisoned_variables = new hash_set<tree> ();
   22064      3020016 :   if (flag_openmp)
   22065        51078 :     omp_resolved_variant_calls = new hash_set<tree> ();
   22066              : 
   22067      3020016 :   bind = gimplify_body (fndecl, true);
   22068              : 
   22069      3020016 :   if (omp_resolved_variant_calls)
   22070              :     {
   22071        51078 :       delete omp_resolved_variant_calls;
   22072        51078 :       omp_resolved_variant_calls = NULL;
   22073              :     }
   22074      3020016 :   if (asan_poisoned_variables)
   22075              :     {
   22076         9061 :       delete asan_poisoned_variables;
   22077         9061 :       asan_poisoned_variables = NULL;
   22078              :     }
   22079              : 
   22080              :   /* The tree body of the function is no longer needed, replace it
   22081              :      with the new GIMPLE body.  */
   22082      3020016 :   seq = NULL;
   22083      3020016 :   gimple_seq_add_stmt (&seq, bind);
   22084      3020016 :   gimple_set_body (fndecl, seq);
   22085              : 
   22086              :   /* If we're instrumenting function entry/exit, then prepend the call to
   22087              :      the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
   22088              :      catch the exit hook.  */
   22089              :   /* ??? Add some way to ignore exceptions for this TFE.  */
   22090      3020016 :   if (flag_instrument_function_entry_exit
   22091           97 :       && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
   22092              :       /* Do not instrument extern inline functions.  */
   22093           44 :       && !(DECL_DECLARED_INLINE_P (fndecl)
   22094            6 :            && DECL_EXTERNAL (fndecl)
   22095            1 :            && DECL_DISREGARD_INLINE_LIMITS (fndecl))
   22096      3020059 :       && !flag_instrument_functions_exclude_p (fndecl))
   22097              :     {
   22098           41 :       gimple_seq body = NULL, cleanup = NULL;
   22099           41 :       gassign *assign;
   22100           41 :       tree cond_var;
   22101              : 
   22102              :       /* If -finstrument-functions-once is specified, generate:
   22103              : 
   22104              :            static volatile bool C.0 = false;
   22105              :            bool tmp_called;
   22106              : 
   22107              :            tmp_called = C.0;
   22108              :            if (!tmp_called)
   22109              :              {
   22110              :                C.0 = true;
   22111              :                [call profiling enter function]
   22112              :              }
   22113              : 
   22114              :          without specific protection for data races.  */
   22115           41 :       if (flag_instrument_function_entry_exit > 1)
   22116              :         {
   22117           10 :           tree first_var
   22118           10 :             = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
   22119              :                           VAR_DECL,
   22120              :                           create_tmp_var_name ("C"),
   22121              :                           boolean_type_node);
   22122           10 :           DECL_ARTIFICIAL (first_var) = 1;
   22123           10 :           DECL_IGNORED_P (first_var) = 1;
   22124           10 :           TREE_STATIC (first_var) = 1;
   22125           10 :           TREE_THIS_VOLATILE (first_var) = 1;
   22126           10 :           TREE_USED (first_var) = 1;
   22127           10 :           DECL_INITIAL (first_var) = boolean_false_node;
   22128           10 :           varpool_node::add (first_var);
   22129              : 
   22130           10 :           cond_var = create_tmp_var (boolean_type_node, "tmp_called");
   22131           10 :           assign = gimple_build_assign (cond_var, first_var);
   22132           10 :           gimplify_seq_add_stmt (&body, assign);
   22133              : 
   22134           10 :           assign = gimple_build_assign (first_var, boolean_true_node);
   22135              :         }
   22136              : 
   22137              :       else
   22138              :         {
   22139              :           cond_var = NULL_TREE;
   22140              :           assign = NULL;
   22141              :         }
   22142              : 
   22143           41 :       build_instrumentation_call (&body, BUILT_IN_PROFILE_FUNC_ENTER,
   22144              :                                   cond_var, assign);
   22145              : 
   22146              :       /* If -finstrument-functions-once is specified, generate:
   22147              : 
   22148              :            if (!tmp_called)
   22149              :              [call profiling exit function]
   22150              : 
   22151              :          without specific protection for data races.  */
   22152           41 :       build_instrumentation_call (&cleanup, BUILT_IN_PROFILE_FUNC_EXIT,
   22153              :                                   cond_var, NULL);
   22154              : 
   22155           41 :       gimple *tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
   22156           41 :       gimplify_seq_add_stmt (&body, tf);
   22157           41 :       gbind *new_bind = gimple_build_bind (NULL, body, NULL);
   22158              : 
   22159              :       /* Replace the current function body with the body
   22160              :          wrapped in the try/finally TF.  */
   22161           41 :       seq = NULL;
   22162           41 :       gimple_seq_add_stmt (&seq, new_bind);
   22163           41 :       gimple_set_body (fndecl, seq);
   22164           41 :       bind = new_bind;
   22165              :     }
   22166              : 
   22167      3020016 :   if (sanitize_flags_p (SANITIZE_THREAD)
   22168      3020016 :       && param_tsan_instrument_func_entry_exit)
   22169              :     {
   22170         1131 :       gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
   22171         1131 :       gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
   22172         1131 :       gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
   22173              :       /* Replace the current function body with the body
   22174              :          wrapped in the try/finally TF.  */
   22175         1131 :       seq = NULL;
   22176         1131 :       gimple_seq_add_stmt (&seq, new_bind);
   22177         1131 :       gimple_set_body (fndecl, seq);
   22178              :     }
   22179              : 
   22180      3020016 :   DECL_SAVED_TREE (fndecl) = NULL_TREE;
   22181      3020016 :   cfun->curr_properties |= PROP_gimple_any;
   22182              : 
   22183      3020016 :   pop_cfun ();
   22184              : 
   22185      3020016 :   dump_function (TDI_gimple, fndecl);
   22186      3020016 : }
   22187              : 
   22188              : /* Return a dummy expression of type TYPE in order to keep going after an
   22189              :    error.  */
   22190              : 
   22191              : static tree
   22192           30 : dummy_object (tree type)
   22193              : {
   22194           30 :   tree t = build_int_cst (build_pointer_type (type), 0);
   22195           30 :   return build2 (MEM_REF, type, t, t);
   22196              : }
   22197              : 
   22198              : /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
   22199              :    builtin function, but a very special sort of operator.  */
   22200              : 
   22201              : enum gimplify_status
   22202        51233 : gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
   22203              :                       gimple_seq *post_p ATTRIBUTE_UNUSED)
   22204              : {
   22205        51233 :   tree promoted_type, have_va_type;
   22206        51233 :   tree valist = TREE_OPERAND (*expr_p, 0);
   22207        51233 :   tree type = TREE_TYPE (*expr_p);
   22208        51233 :   tree t, tag, aptag;
   22209        51233 :   location_t loc = EXPR_LOCATION (*expr_p);
   22210              : 
   22211              :   /* Verify that valist is of the proper type.  */
   22212        51233 :   have_va_type = TREE_TYPE (valist);
   22213        51233 :   if (have_va_type == error_mark_node)
   22214              :     return GS_ERROR;
   22215        51214 :   have_va_type = targetm.canonical_va_list_type (have_va_type);
   22216        51214 :   if (have_va_type == NULL_TREE
   22217        51214 :       && POINTER_TYPE_P (TREE_TYPE (valist)))
   22218              :     /* Handle 'Case 1: Not an array type' from c-common.cc/build_va_arg.  */
   22219          270 :     have_va_type
   22220          270 :       = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
   22221        51214 :   gcc_assert (have_va_type != NULL_TREE);
   22222              : 
   22223              :   /* Generate a diagnostic for requesting data of a type that cannot
   22224              :      be passed through `...' due to type promotion at the call site.  */
   22225        51214 :   if ((promoted_type = lang_hooks.types.type_promotes_to (type))
   22226              :            != type)
   22227              :     {
   22228           30 :       static bool gave_help;
   22229           30 :       bool warned;
   22230              :       /* Use the expansion point to handle cases such as passing bool (defined
   22231              :          in a system header) through `...'.  */
   22232           30 :       location_t xloc
   22233           30 :         = expansion_point_location_if_in_system_header (loc);
   22234              : 
   22235              :       /* Unfortunately, this is merely undefined, rather than a constraint
   22236              :          violation, so we cannot make this an error.  If this call is never
   22237              :          executed, the program is still strictly conforming.  */
   22238           30 :       auto_diagnostic_group d;
   22239           30 :       warned = warning_at (xloc, 0,
   22240              :                            "%qT is promoted to %qT when passed through %<...%>",
   22241              :                            type, promoted_type);
   22242           30 :       if (!gave_help && warned)
   22243              :         {
   22244           15 :           gave_help = true;
   22245           15 :           inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
   22246              :                   promoted_type, type);
   22247              :         }
   22248              : 
   22249              :       /* We can, however, treat "undefined" any way we please.
   22250              :          Call abort to encourage the user to fix the program.  */
   22251           23 :       if (warned)
   22252           23 :         inform (xloc, "if this code is reached, the program will abort");
   22253              :       /* Before the abort, allow the evaluation of the va_list
   22254              :          expression to exit or longjmp.  */
   22255           30 :       gimplify_and_add (valist, pre_p);
   22256           60 :       t = build_call_expr_loc (loc,
   22257              :                                builtin_decl_implicit (BUILT_IN_TRAP), 0);
   22258           30 :       gimplify_and_add (t, pre_p);
   22259              : 
   22260              :       /* This is dead code, but go ahead and finish so that the
   22261              :          mode of the result comes out right.  */
   22262           30 :       *expr_p = dummy_object (type);
   22263           30 :       return GS_ALL_DONE;
   22264           30 :     }
   22265              : 
   22266        51184 :   tag = build_int_cst (build_pointer_type (type), 0);
   22267        51184 :   aptag = build_int_cst (TREE_TYPE (valist), 0);
   22268              : 
   22269        51184 :   *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
   22270              :                                           valist, tag, aptag);
   22271              : 
   22272              :   /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
   22273              :      needs to be expanded.  */
   22274        51184 :   cfun->curr_properties &= ~PROP_gimple_lva;
   22275              : 
   22276        51184 :   return GS_OK;
   22277              : }
   22278              : 
   22279              : /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
   22280              : 
   22281              :    DST/SRC are the destination and source respectively.  You can pass
   22282              :    ungimplified trees in DST or SRC, in which case they will be
   22283              :    converted to a gimple operand if necessary.
   22284              : 
   22285              :    This function returns the newly created GIMPLE_ASSIGN tuple.  */
   22286              : 
   22287              : gimple *
   22288       928234 : gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
   22289              : {
   22290       928234 :   tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
   22291       928234 :   gimplify_and_add (t, seq_p);
   22292       928234 :   ggc_free (t);
   22293       928234 :   return gimple_seq_last_stmt (*seq_p);
   22294              : }
   22295              : 
   22296              : inline hashval_t
   22297      1657536 : gimplify_hasher::hash (const elt_t *p)
   22298              : {
   22299      1657536 :   tree t = p->val;
   22300      1657536 :   return iterative_hash_expr (t, 0);
   22301              : }
   22302              : 
   22303              : inline bool
   22304       603599 : gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
   22305              : {
   22306       603599 :   tree t1 = p1->val;
   22307       603599 :   tree t2 = p2->val;
   22308       603599 :   enum tree_code code = TREE_CODE (t1);
   22309              : 
   22310       603599 :   if (TREE_CODE (t2) != code
   22311       603599 :       || TREE_TYPE (t1) != TREE_TYPE (t2))
   22312              :     return false;
   22313              : 
   22314       370020 :   if (!operand_equal_p (t1, t2, 0))
   22315              :     return false;
   22316              : 
   22317              :   return true;
   22318              : }
        

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.