xref: /dragonfly/contrib/gcc-8.0/gcc/gimplify.c (revision 3851e4b8)
1 /* Tree lowering pass.  This pass converts the GENERIC functions-as-trees
2    tree representation into the GIMPLE form.
3    Copyright (C) 2002-2018 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 "alias.h"
40 #include "fold-const.h"
41 #include "calls.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "gimple-fold.h"
46 #include "tree-eh.h"
47 #include "gimplify.h"
48 #include "gimple-iterator.h"
49 #include "stor-layout.h"
50 #include "print-tree.h"
51 #include "tree-iterator.h"
52 #include "tree-inline.h"
53 #include "langhooks.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa.h"
56 #include "omp-general.h"
57 #include "omp-low.h"
58 #include "gimple-low.h"
59 #include "gomp-constants.h"
60 #include "splay-tree.h"
61 #include "gimple-walk.h"
62 #include "langhooks-def.h"	/* FIXME: for lhd_set_decl_assembler_name */
63 #include "builtins.h"
64 #include "stringpool.h"
65 #include "attribs.h"
66 #include "asan.h"
67 #include "dbgcnt.h"
68 
69 /* Hash set of poisoned variables in a bind expr.  */
70 static hash_set<tree> *asan_poisoned_variables = NULL;
71 
72 enum gimplify_omp_var_data
73 {
74   GOVD_SEEN = 1,
75   GOVD_EXPLICIT = 2,
76   GOVD_SHARED = 4,
77   GOVD_PRIVATE = 8,
78   GOVD_FIRSTPRIVATE = 16,
79   GOVD_LASTPRIVATE = 32,
80   GOVD_REDUCTION = 64,
81   GOVD_LOCAL = 128,
82   GOVD_MAP = 256,
83   GOVD_DEBUG_PRIVATE = 512,
84   GOVD_PRIVATE_OUTER_REF = 1024,
85   GOVD_LINEAR = 2048,
86   GOVD_ALIGNED = 4096,
87 
88   /* Flag for GOVD_MAP: don't copy back.  */
89   GOVD_MAP_TO_ONLY = 8192,
90 
91   /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference.  */
92   GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
93 
94   GOVD_MAP_0LEN_ARRAY = 32768,
95 
96   /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping.  */
97   GOVD_MAP_ALWAYS_TO = 65536,
98 
99   /* Flag for shared vars that are or might be stored to in the region.  */
100   GOVD_WRITTEN = 131072,
101 
102   /* Flag for GOVD_MAP, if it is a forced mapping.  */
103   GOVD_MAP_FORCE = 262144,
104 
105   /* Flag for GOVD_MAP: must be present already.  */
106   GOVD_MAP_FORCE_PRESENT = 524288,
107 
108   GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
109 			   | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
110 			   | GOVD_LOCAL)
111 };
112 
113 
114 enum omp_region_type
115 {
116   ORT_WORKSHARE = 0x00,
117   ORT_SIMD 	= 0x01,
118 
119   ORT_PARALLEL	= 0x02,
120   ORT_COMBINED_PARALLEL = 0x03,
121 
122   ORT_TASK	= 0x04,
123   ORT_UNTIED_TASK = 0x05,
124 
125   ORT_TEAMS	= 0x08,
126   ORT_COMBINED_TEAMS = 0x09,
127 
128   /* Data region.  */
129   ORT_TARGET_DATA = 0x10,
130 
131   /* Data region with offloading.  */
132   ORT_TARGET	= 0x20,
133   ORT_COMBINED_TARGET = 0x21,
134 
135   /* OpenACC variants.  */
136   ORT_ACC	= 0x40,  /* A generic OpenACC region.  */
137   ORT_ACC_DATA	= ORT_ACC | ORT_TARGET_DATA, /* Data construct.  */
138   ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET,  /* Parallel construct */
139   ORT_ACC_KERNELS  = ORT_ACC | ORT_TARGET | 0x80,  /* Kernels construct.  */
140   ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 0x80,  /* Host data.  */
141 
142   /* Dummy OpenMP region, used to disable expansion of
143      DECL_VALUE_EXPRs in taskloop pre body.  */
144   ORT_NONE	= 0x100
145 };
146 
147 /* Gimplify hashtable helper.  */
148 
149 struct gimplify_hasher : free_ptr_hash <elt_t>
150 {
151   static inline hashval_t hash (const elt_t *);
152   static inline bool equal (const elt_t *, const elt_t *);
153 };
154 
155 struct gimplify_ctx
156 {
157   struct gimplify_ctx *prev_context;
158 
159   vec<gbind *> bind_expr_stack;
160   tree temps;
161   gimple_seq conditional_cleanups;
162   tree exit_label;
163   tree return_temp;
164 
165   vec<tree> case_labels;
166   hash_set<tree> *live_switch_vars;
167   /* The formal temporary table.  Should this be persistent?  */
168   hash_table<gimplify_hasher> *temp_htab;
169 
170   int conditions;
171   unsigned into_ssa : 1;
172   unsigned allow_rhs_cond_expr : 1;
173   unsigned in_cleanup_point_expr : 1;
174   unsigned keep_stack : 1;
175   unsigned save_stack : 1;
176   unsigned in_switch_expr : 1;
177 };
178 
179 struct gimplify_omp_ctx
180 {
181   struct gimplify_omp_ctx *outer_context;
182   splay_tree variables;
183   hash_set<tree> *privatized_types;
184   /* Iteration variables in an OMP_FOR.  */
185   vec<tree> loop_iter_var;
186   location_t location;
187   enum omp_clause_default_kind default_kind;
188   enum omp_region_type region_type;
189   bool combined_loop;
190   bool distribute;
191   bool target_map_scalars_firstprivate;
192   bool target_map_pointers_as_0len_arrays;
193   bool target_firstprivatize_array_bases;
194 };
195 
196 static struct gimplify_ctx *gimplify_ctxp;
197 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
198 
199 /* Forward declaration.  */
200 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
201 static hash_map<tree, tree> *oacc_declare_returns;
202 static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
203 					   bool (*) (tree), fallback_t, bool);
204 
205 /* Shorter alias name for the above function for use in gimplify.c
206    only.  */
207 
208 static inline void
209 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
210 {
211   gimple_seq_add_stmt_without_update (seq_p, gs);
212 }
213 
214 /* Append sequence SRC to the end of sequence *DST_P.  If *DST_P is
215    NULL, a new sequence is allocated.   This function is
216    similar to gimple_seq_add_seq, but does not scan the operands.
217    During gimplification, we need to manipulate statement sequences
218    before the def/use vectors have been constructed.  */
219 
220 static void
221 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
222 {
223   gimple_stmt_iterator si;
224 
225   if (src == NULL)
226     return;
227 
228   si = gsi_last (*dst_p);
229   gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
230 }
231 
232 
233 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
234    and popping gimplify contexts.  */
235 
236 static struct gimplify_ctx *ctx_pool = NULL;
237 
238 /* Return a gimplify context struct from the pool.  */
239 
240 static inline struct gimplify_ctx *
241 ctx_alloc (void)
242 {
243   struct gimplify_ctx * c = ctx_pool;
244 
245   if (c)
246     ctx_pool = c->prev_context;
247   else
248     c = XNEW (struct gimplify_ctx);
249 
250   memset (c, '\0', sizeof (*c));
251   return c;
252 }
253 
254 /* Put gimplify context C back into the pool.  */
255 
256 static inline void
257 ctx_free (struct gimplify_ctx *c)
258 {
259   c->prev_context = ctx_pool;
260   ctx_pool = c;
261 }
262 
263 /* Free allocated ctx stack memory.  */
264 
265 void
266 free_gimplify_stack (void)
267 {
268   struct gimplify_ctx *c;
269 
270   while ((c = ctx_pool))
271     {
272       ctx_pool = c->prev_context;
273       free (c);
274     }
275 }
276 
277 
278 /* Set up a context for the gimplifier.  */
279 
280 void
281 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
282 {
283   struct gimplify_ctx *c = ctx_alloc ();
284 
285   c->prev_context = gimplify_ctxp;
286   gimplify_ctxp = c;
287   gimplify_ctxp->into_ssa = in_ssa;
288   gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
289 }
290 
291 /* Tear down a context for the gimplifier.  If BODY is non-null, then
292    put the temporaries into the outer BIND_EXPR.  Otherwise, put them
293    in the local_decls.
294 
295    BODY is not a sequence, but the first tuple in a sequence.  */
296 
297 void
298 pop_gimplify_context (gimple *body)
299 {
300   struct gimplify_ctx *c = gimplify_ctxp;
301 
302   gcc_assert (c
303               && (!c->bind_expr_stack.exists ()
304 		  || c->bind_expr_stack.is_empty ()));
305   c->bind_expr_stack.release ();
306   gimplify_ctxp = c->prev_context;
307 
308   if (body)
309     declare_vars (c->temps, body, false);
310   else
311     record_vars (c->temps);
312 
313   delete c->temp_htab;
314   c->temp_htab = NULL;
315   ctx_free (c);
316 }
317 
318 /* Push a GIMPLE_BIND tuple onto the stack of bindings.  */
319 
320 static void
321 gimple_push_bind_expr (gbind *bind_stmt)
322 {
323   gimplify_ctxp->bind_expr_stack.reserve (8);
324   gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
325 }
326 
327 /* Pop the first element off the stack of bindings.  */
328 
329 static void
330 gimple_pop_bind_expr (void)
331 {
332   gimplify_ctxp->bind_expr_stack.pop ();
333 }
334 
335 /* Return the first element of the stack of bindings.  */
336 
337 gbind *
338 gimple_current_bind_expr (void)
339 {
340   return gimplify_ctxp->bind_expr_stack.last ();
341 }
342 
343 /* Return the stack of bindings created during gimplification.  */
344 
345 vec<gbind *>
346 gimple_bind_expr_stack (void)
347 {
348   return gimplify_ctxp->bind_expr_stack;
349 }
350 
351 /* Return true iff there is a COND_EXPR between us and the innermost
352    CLEANUP_POINT_EXPR.  This info is used by gimple_push_cleanup.  */
353 
354 static bool
355 gimple_conditional_context (void)
356 {
357   return gimplify_ctxp->conditions > 0;
358 }
359 
360 /* Note that we've entered a COND_EXPR.  */
361 
362 static void
363 gimple_push_condition (void)
364 {
365 #ifdef ENABLE_GIMPLE_CHECKING
366   if (gimplify_ctxp->conditions == 0)
367     gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
368 #endif
369   ++(gimplify_ctxp->conditions);
370 }
371 
372 /* Note that we've left a COND_EXPR.  If we're back at unconditional scope
373    now, add any conditional cleanups we've seen to the prequeue.  */
374 
375 static void
376 gimple_pop_condition (gimple_seq *pre_p)
377 {
378   int conds = --(gimplify_ctxp->conditions);
379 
380   gcc_assert (conds >= 0);
381   if (conds == 0)
382     {
383       gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
384       gimplify_ctxp->conditional_cleanups = NULL;
385     }
386 }
387 
388 /* A stable comparison routine for use with splay trees and DECLs.  */
389 
390 static int
391 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
392 {
393   tree a = (tree) xa;
394   tree b = (tree) xb;
395 
396   return DECL_UID (a) - DECL_UID (b);
397 }
398 
399 /* Create a new omp construct that deals with variable remapping.  */
400 
401 static struct gimplify_omp_ctx *
402 new_omp_context (enum omp_region_type region_type)
403 {
404   struct gimplify_omp_ctx *c;
405 
406   c = XCNEW (struct gimplify_omp_ctx);
407   c->outer_context = gimplify_omp_ctxp;
408   c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
409   c->privatized_types = new hash_set<tree>;
410   c->location = input_location;
411   c->region_type = region_type;
412   if ((region_type & ORT_TASK) == 0)
413     c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
414   else
415     c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
416 
417   return c;
418 }
419 
420 /* Destroy an omp construct that deals with variable remapping.  */
421 
422 static void
423 delete_omp_context (struct gimplify_omp_ctx *c)
424 {
425   splay_tree_delete (c->variables);
426   delete c->privatized_types;
427   c->loop_iter_var.release ();
428   XDELETE (c);
429 }
430 
431 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
432 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
433 
434 /* Both gimplify the statement T and append it to *SEQ_P.  This function
435    behaves exactly as gimplify_stmt, but you don't have to pass T as a
436    reference.  */
437 
438 void
439 gimplify_and_add (tree t, gimple_seq *seq_p)
440 {
441   gimplify_stmt (&t, seq_p);
442 }
443 
444 /* Gimplify statement T into sequence *SEQ_P, and return the first
445    tuple in the sequence of generated tuples for this statement.
446    Return NULL if gimplifying T produced no tuples.  */
447 
448 static gimple *
449 gimplify_and_return_first (tree t, gimple_seq *seq_p)
450 {
451   gimple_stmt_iterator last = gsi_last (*seq_p);
452 
453   gimplify_and_add (t, seq_p);
454 
455   if (!gsi_end_p (last))
456     {
457       gsi_next (&last);
458       return gsi_stmt (last);
459     }
460   else
461     return gimple_seq_first_stmt (*seq_p);
462 }
463 
464 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
465    LHS, or for a call argument.  */
466 
467 static bool
468 is_gimple_mem_rhs (tree t)
469 {
470   /* If we're dealing with a renamable type, either source or dest must be
471      a renamed variable.  */
472   if (is_gimple_reg_type (TREE_TYPE (t)))
473     return is_gimple_val (t);
474   else
475     return is_gimple_val (t) || is_gimple_lvalue (t);
476 }
477 
478 /* Return true if T is a CALL_EXPR or an expression that can be
479    assigned to a temporary.  Note that this predicate should only be
480    used during gimplification.  See the rationale for this in
481    gimplify_modify_expr.  */
482 
483 static bool
484 is_gimple_reg_rhs_or_call (tree t)
485 {
486   return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
487 	  || TREE_CODE (t) == CALL_EXPR);
488 }
489 
490 /* Return true if T is a valid memory RHS or a CALL_EXPR.  Note that
491    this predicate should only be used during gimplification.  See the
492    rationale for this in gimplify_modify_expr.  */
493 
494 static bool
495 is_gimple_mem_rhs_or_call (tree t)
496 {
497   /* If we're dealing with a renamable type, either source or dest must be
498      a renamed variable.  */
499   if (is_gimple_reg_type (TREE_TYPE (t)))
500     return is_gimple_val (t);
501   else
502     return (is_gimple_val (t)
503 	    || is_gimple_lvalue (t)
504 	    || TREE_CLOBBER_P (t)
505 	    || TREE_CODE (t) == CALL_EXPR);
506 }
507 
508 /* Create a temporary with a name derived from VAL.  Subroutine of
509    lookup_tmp_var; nobody else should call this function.  */
510 
511 static inline tree
512 create_tmp_from_val (tree val)
513 {
514   /* Drop all qualifiers and address-space information from the value type.  */
515   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
516   tree var = create_tmp_var (type, get_name (val));
517   if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
518       || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
519     DECL_GIMPLE_REG_P (var) = 1;
520   return var;
521 }
522 
523 /* Create a temporary to hold the value of VAL.  If IS_FORMAL, try to reuse
524    an existing expression temporary.  */
525 
526 static tree
527 lookup_tmp_var (tree val, bool is_formal)
528 {
529   tree ret;
530 
531   /* If not optimizing, never really reuse a temporary.  local-alloc
532      won't allocate any variable that is used in more than one basic
533      block, which means it will go into memory, causing much extra
534      work in reload and final and poorer code generation, outweighing
535      the extra memory allocation here.  */
536   if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
537     ret = create_tmp_from_val (val);
538   else
539     {
540       elt_t elt, *elt_p;
541       elt_t **slot;
542 
543       elt.val = val;
544       if (!gimplify_ctxp->temp_htab)
545         gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
546       slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
547       if (*slot == NULL)
548 	{
549 	  elt_p = XNEW (elt_t);
550 	  elt_p->val = val;
551 	  elt_p->temp = ret = create_tmp_from_val (val);
552 	  *slot = elt_p;
553 	}
554       else
555 	{
556 	  elt_p = *slot;
557           ret = elt_p->temp;
558 	}
559     }
560 
561   return ret;
562 }
563 
564 /* Helper for get_formal_tmp_var and get_initialized_tmp_var.  */
565 
566 static tree
567 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
568                       bool is_formal, bool allow_ssa)
569 {
570   tree t, mod;
571 
572   /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
573      can create an INIT_EXPR and convert it into a GIMPLE_CALL below.  */
574   gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
575 		 fb_rvalue);
576 
577   if (allow_ssa
578       && gimplify_ctxp->into_ssa
579       && is_gimple_reg_type (TREE_TYPE (val)))
580     {
581       t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
582       if (! gimple_in_ssa_p (cfun))
583 	{
584 	  const char *name = get_name (val);
585 	  if (name)
586 	    SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
587 	}
588     }
589   else
590     t = lookup_tmp_var (val, is_formal);
591 
592   mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
593 
594   SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
595 
596   /* gimplify_modify_expr might want to reduce this further.  */
597   gimplify_and_add (mod, pre_p);
598   ggc_free (mod);
599 
600   return t;
601 }
602 
603 /* Return a formal temporary variable initialized with VAL.  PRE_P is as
604    in gimplify_expr.  Only use this function if:
605 
606    1) The value of the unfactored expression represented by VAL will not
607       change between the initialization and use of the temporary, and
608    2) The temporary will not be otherwise modified.
609 
610    For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
611    and #2 means it is inappropriate for && temps.
612 
613    For other cases, use get_initialized_tmp_var instead.  */
614 
615 tree
616 get_formal_tmp_var (tree val, gimple_seq *pre_p)
617 {
618   return internal_get_tmp_var (val, pre_p, NULL, true, true);
619 }
620 
621 /* Return a temporary variable initialized with VAL.  PRE_P and POST_P
622    are as in gimplify_expr.  */
623 
624 tree
625 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
626 			 bool allow_ssa)
627 {
628   return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa);
629 }
630 
631 /* Declare all the variables in VARS in SCOPE.  If DEBUG_INFO is true,
632    generate debug info for them; otherwise don't.  */
633 
634 void
635 declare_vars (tree vars, gimple *gs, bool debug_info)
636 {
637   tree last = vars;
638   if (last)
639     {
640       tree temps, block;
641 
642       gbind *scope = as_a <gbind *> (gs);
643 
644       temps = nreverse (last);
645 
646       block = gimple_bind_block (scope);
647       gcc_assert (!block || TREE_CODE (block) == BLOCK);
648       if (!block || !debug_info)
649 	{
650 	  DECL_CHAIN (last) = gimple_bind_vars (scope);
651 	  gimple_bind_set_vars (scope, temps);
652 	}
653       else
654 	{
655 	  /* We need to attach the nodes both to the BIND_EXPR and to its
656 	     associated BLOCK for debugging purposes.  The key point here
657 	     is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
658 	     is a subchain of the BIND_EXPR_VARS of the BIND_EXPR.  */
659 	  if (BLOCK_VARS (block))
660 	    BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
661 	  else
662 	    {
663 	      gimple_bind_set_vars (scope,
664 	      			    chainon (gimple_bind_vars (scope), temps));
665 	      BLOCK_VARS (block) = temps;
666 	    }
667 	}
668     }
669 }
670 
671 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
672    for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly.  Abort if
673    no such upper bound can be obtained.  */
674 
675 static void
676 force_constant_size (tree var)
677 {
678   /* The only attempt we make is by querying the maximum size of objects
679      of the variable's type.  */
680 
681   HOST_WIDE_INT max_size;
682 
683   gcc_assert (VAR_P (var));
684 
685   max_size = max_int_size_in_bytes (TREE_TYPE (var));
686 
687   gcc_assert (max_size >= 0);
688 
689   DECL_SIZE_UNIT (var)
690     = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
691   DECL_SIZE (var)
692     = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
693 }
694 
695 /* Push the temporary variable TMP into the current binding.  */
696 
697 void
698 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
699 {
700   gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
701 
702   /* Later processing assumes that the object size is constant, which might
703      not be true at this point.  Force the use of a constant upper bound in
704      this case.  */
705   if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
706     force_constant_size (tmp);
707 
708   DECL_CONTEXT (tmp) = fn->decl;
709   DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
710 
711   record_vars_into (tmp, fn->decl);
712 }
713 
714 /* Push the temporary variable TMP into the current binding.  */
715 
716 void
717 gimple_add_tmp_var (tree tmp)
718 {
719   gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
720 
721   /* Later processing assumes that the object size is constant, which might
722      not be true at this point.  Force the use of a constant upper bound in
723      this case.  */
724   if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
725     force_constant_size (tmp);
726 
727   DECL_CONTEXT (tmp) = current_function_decl;
728   DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
729 
730   if (gimplify_ctxp)
731     {
732       DECL_CHAIN (tmp) = gimplify_ctxp->temps;
733       gimplify_ctxp->temps = tmp;
734 
735       /* Mark temporaries local within the nearest enclosing parallel.  */
736       if (gimplify_omp_ctxp)
737 	{
738 	  struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
739 	  while (ctx
740 		 && (ctx->region_type == ORT_WORKSHARE
741 		     || ctx->region_type == ORT_SIMD
742 		     || ctx->region_type == ORT_ACC))
743 	    ctx = ctx->outer_context;
744 	  if (ctx)
745 	    omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
746 	}
747     }
748   else if (cfun)
749     record_vars (tmp);
750   else
751     {
752       gimple_seq body_seq;
753 
754       /* This case is for nested functions.  We need to expose the locals
755 	 they create.  */
756       body_seq = gimple_body (current_function_decl);
757       declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
758     }
759 }
760 
761 
762 
763 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
764    nodes that are referenced more than once in GENERIC functions.  This is
765    necessary because gimplification (translation into GIMPLE) is performed
766    by modifying tree nodes in-place, so gimplication of a shared node in a
767    first context could generate an invalid GIMPLE form in a second context.
768 
769    This is achieved with a simple mark/copy/unmark algorithm that walks the
770    GENERIC representation top-down, marks nodes with TREE_VISITED the first
771    time it encounters them, duplicates them if they already have TREE_VISITED
772    set, and finally removes the TREE_VISITED marks it has set.
773 
774    The algorithm works only at the function level, i.e. it generates a GENERIC
775    representation of a function with no nodes shared within the function when
776    passed a GENERIC function (except for nodes that are allowed to be shared).
777 
778    At the global level, it is also necessary to unshare tree nodes that are
779    referenced in more than one function, for the same aforementioned reason.
780    This requires some cooperation from the front-end.  There are 2 strategies:
781 
782      1. Manual unsharing.  The front-end needs to call unshare_expr on every
783         expression that might end up being shared across functions.
784 
785      2. Deep unsharing.  This is an extension of regular unsharing.  Instead
786         of calling unshare_expr on expressions that might be shared across
787         functions, the front-end pre-marks them with TREE_VISITED.  This will
788         ensure that they are unshared on the first reference within functions
789         when the regular unsharing algorithm runs.  The counterpart is that
790         this algorithm must look deeper than for manual unsharing, which is
791         specified by LANG_HOOKS_DEEP_UNSHARING.
792 
793   If there are only few specific cases of node sharing across functions, it is
794   probably easier for a front-end to unshare the expressions manually.  On the
795   contrary, if the expressions generated at the global level are as widespread
796   as expressions generated within functions, deep unsharing is very likely the
797   way to go.  */
798 
799 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
800    These nodes model computations that must be done once.  If we were to
801    unshare something like SAVE_EXPR(i++), the gimplification process would
802    create wrong code.  However, if DATA is non-null, it must hold a pointer
803    set that is used to unshare the subtrees of these nodes.  */
804 
805 static tree
806 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
807 {
808   tree t = *tp;
809   enum tree_code code = TREE_CODE (t);
810 
811   /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
812      copy their subtrees if we can make sure to do it only once.  */
813   if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
814     {
815       if (data && !((hash_set<tree> *)data)->add (t))
816 	;
817       else
818 	*walk_subtrees = 0;
819     }
820 
821   /* Stop at types, decls, constants like copy_tree_r.  */
822   else if (TREE_CODE_CLASS (code) == tcc_type
823 	   || TREE_CODE_CLASS (code) == tcc_declaration
824 	   || TREE_CODE_CLASS (code) == tcc_constant)
825     *walk_subtrees = 0;
826 
827   /* Cope with the statement expression extension.  */
828   else if (code == STATEMENT_LIST)
829     ;
830 
831   /* Leave the bulk of the work to copy_tree_r itself.  */
832   else
833     copy_tree_r (tp, walk_subtrees, NULL);
834 
835   return NULL_TREE;
836 }
837 
838 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
839    If *TP has been visited already, then *TP is deeply copied by calling
840    mostly_copy_tree_r.  DATA is passed to mostly_copy_tree_r unmodified.  */
841 
842 static tree
843 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
844 {
845   tree t = *tp;
846   enum tree_code code = TREE_CODE (t);
847 
848   /* Skip types, decls, and constants.  But we do want to look at their
849      types and the bounds of types.  Mark them as visited so we properly
850      unmark their subtrees on the unmark pass.  If we've already seen them,
851      don't look down further.  */
852   if (TREE_CODE_CLASS (code) == tcc_type
853       || TREE_CODE_CLASS (code) == tcc_declaration
854       || TREE_CODE_CLASS (code) == tcc_constant)
855     {
856       if (TREE_VISITED (t))
857 	*walk_subtrees = 0;
858       else
859 	TREE_VISITED (t) = 1;
860     }
861 
862   /* If this node has been visited already, unshare it and don't look
863      any deeper.  */
864   else if (TREE_VISITED (t))
865     {
866       walk_tree (tp, mostly_copy_tree_r, data, NULL);
867       *walk_subtrees = 0;
868     }
869 
870   /* Otherwise, mark the node as visited and keep looking.  */
871   else
872     TREE_VISITED (t) = 1;
873 
874   return NULL_TREE;
875 }
876 
877 /* Unshare most of the shared trees rooted at *TP.  DATA is passed to the
878    copy_if_shared_r callback unmodified.  */
879 
880 static inline void
881 copy_if_shared (tree *tp, void *data)
882 {
883   walk_tree (tp, copy_if_shared_r, data, NULL);
884 }
885 
886 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
887    any nested functions.  */
888 
889 static void
890 unshare_body (tree fndecl)
891 {
892   struct cgraph_node *cgn = cgraph_node::get (fndecl);
893   /* If the language requires deep unsharing, we need a pointer set to make
894      sure we don't repeatedly unshare subtrees of unshareable nodes.  */
895   hash_set<tree> *visited
896     = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
897 
898   copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
899   copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
900   copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
901 
902   delete visited;
903 
904   if (cgn)
905     for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
906       unshare_body (cgn->decl);
907 }
908 
909 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
910    Subtrees are walked until the first unvisited node is encountered.  */
911 
912 static tree
913 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
914 {
915   tree t = *tp;
916 
917   /* If this node has been visited, unmark it and keep looking.  */
918   if (TREE_VISITED (t))
919     TREE_VISITED (t) = 0;
920 
921   /* Otherwise, don't look any deeper.  */
922   else
923     *walk_subtrees = 0;
924 
925   return NULL_TREE;
926 }
927 
928 /* Unmark the visited trees rooted at *TP.  */
929 
930 static inline void
931 unmark_visited (tree *tp)
932 {
933   walk_tree (tp, unmark_visited_r, NULL, NULL);
934 }
935 
936 /* Likewise, but mark all trees as not visited.  */
937 
938 static void
939 unvisit_body (tree fndecl)
940 {
941   struct cgraph_node *cgn = cgraph_node::get (fndecl);
942 
943   unmark_visited (&DECL_SAVED_TREE (fndecl));
944   unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
945   unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
946 
947   if (cgn)
948     for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
949       unvisit_body (cgn->decl);
950 }
951 
952 /* Unconditionally make an unshared copy of EXPR.  This is used when using
953    stored expressions which span multiple functions, such as BINFO_VTABLE,
954    as the normal unsharing process can't tell that they're shared.  */
955 
956 tree
957 unshare_expr (tree expr)
958 {
959   walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
960   return expr;
961 }
962 
963 /* Worker for unshare_expr_without_location.  */
964 
965 static tree
966 prune_expr_location (tree *tp, int *walk_subtrees, void *)
967 {
968   if (EXPR_P (*tp))
969     SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
970   else
971     *walk_subtrees = 0;
972   return NULL_TREE;
973 }
974 
975 /* Similar to unshare_expr but also prune all expression locations
976    from EXPR.  */
977 
978 tree
979 unshare_expr_without_location (tree expr)
980 {
981   walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
982   if (EXPR_P (expr))
983     walk_tree (&expr, prune_expr_location, NULL, NULL);
984   return expr;
985 }
986 
987 /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
988    one, OR_ELSE otherwise.  The location of a STATEMENT_LISTs
989    comprising at least one DEBUG_BEGIN_STMT followed by exactly one
990    EXPR is the location of the EXPR.  */
991 
992 static location_t
993 rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
994 {
995   if (!expr)
996     return or_else;
997 
998   if (EXPR_HAS_LOCATION (expr))
999     return EXPR_LOCATION (expr);
1000 
1001   if (TREE_CODE (expr) != STATEMENT_LIST)
1002     return or_else;
1003 
1004   tree_stmt_iterator i = tsi_start (expr);
1005 
1006   bool found = false;
1007   while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
1008     {
1009       found = true;
1010       tsi_next (&i);
1011     }
1012 
1013   if (!found || !tsi_one_before_end_p (i))
1014     return or_else;
1015 
1016   return rexpr_location (tsi_stmt (i), or_else);
1017 }
1018 
1019 /* Return TRUE iff EXPR (maybe recursively) has a location; see
1020    rexpr_location for the potential recursion.  */
1021 
1022 static inline bool
1023 rexpr_has_location (tree expr)
1024 {
1025   return rexpr_location (expr) != UNKNOWN_LOCATION;
1026 }
1027 
1028 
1029 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1030    contain statements and have a value.  Assign its value to a temporary
1031    and give it void_type_node.  Return the temporary, or NULL_TREE if
1032    WRAPPER was already void.  */
1033 
1034 tree
1035 voidify_wrapper_expr (tree wrapper, tree temp)
1036 {
1037   tree type = TREE_TYPE (wrapper);
1038   if (type && !VOID_TYPE_P (type))
1039     {
1040       tree *p;
1041 
1042       /* Set p to point to the body of the wrapper.  Loop until we find
1043 	 something that isn't a wrapper.  */
1044       for (p = &wrapper; p && *p; )
1045 	{
1046 	  switch (TREE_CODE (*p))
1047 	    {
1048 	    case BIND_EXPR:
1049 	      TREE_SIDE_EFFECTS (*p) = 1;
1050 	      TREE_TYPE (*p) = void_type_node;
1051 	      /* For a BIND_EXPR, the body is operand 1.  */
1052 	      p = &BIND_EXPR_BODY (*p);
1053 	      break;
1054 
1055 	    case CLEANUP_POINT_EXPR:
1056 	    case TRY_FINALLY_EXPR:
1057 	    case TRY_CATCH_EXPR:
1058 	      TREE_SIDE_EFFECTS (*p) = 1;
1059 	      TREE_TYPE (*p) = void_type_node;
1060 	      p = &TREE_OPERAND (*p, 0);
1061 	      break;
1062 
1063 	    case STATEMENT_LIST:
1064 	      {
1065 		tree_stmt_iterator i = tsi_last (*p);
1066 		TREE_SIDE_EFFECTS (*p) = 1;
1067 		TREE_TYPE (*p) = void_type_node;
1068 		p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1069 	      }
1070 	      break;
1071 
1072 	    case COMPOUND_EXPR:
1073 	      /* Advance to the last statement.  Set all container types to
1074 		 void.  */
1075 	      for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1076 		{
1077 		  TREE_SIDE_EFFECTS (*p) = 1;
1078 		  TREE_TYPE (*p) = void_type_node;
1079 		}
1080 	      break;
1081 
1082 	    case TRANSACTION_EXPR:
1083 	      TREE_SIDE_EFFECTS (*p) = 1;
1084 	      TREE_TYPE (*p) = void_type_node;
1085 	      p = &TRANSACTION_EXPR_BODY (*p);
1086 	      break;
1087 
1088 	    default:
1089 	      /* Assume that any tree upon which voidify_wrapper_expr is
1090 		 directly called is a wrapper, and that its body is op0.  */
1091 	      if (p == &wrapper)
1092 		{
1093 		  TREE_SIDE_EFFECTS (*p) = 1;
1094 		  TREE_TYPE (*p) = void_type_node;
1095 		  p = &TREE_OPERAND (*p, 0);
1096 		  break;
1097 		}
1098 	      goto out;
1099 	    }
1100 	}
1101 
1102     out:
1103       if (p == NULL || IS_EMPTY_STMT (*p))
1104 	temp = NULL_TREE;
1105       else if (temp)
1106 	{
1107 	  /* The wrapper is on the RHS of an assignment that we're pushing
1108 	     down.  */
1109 	  gcc_assert (TREE_CODE (temp) == INIT_EXPR
1110 		      || TREE_CODE (temp) == MODIFY_EXPR);
1111 	  TREE_OPERAND (temp, 1) = *p;
1112 	  *p = temp;
1113 	}
1114       else
1115 	{
1116 	  temp = create_tmp_var (type, "retval");
1117 	  *p = build2 (INIT_EXPR, type, temp, *p);
1118 	}
1119 
1120       return temp;
1121     }
1122 
1123   return NULL_TREE;
1124 }
1125 
1126 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1127    a temporary through which they communicate.  */
1128 
1129 static void
1130 build_stack_save_restore (gcall **save, gcall **restore)
1131 {
1132   tree tmp_var;
1133 
1134   *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1135   tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1136   gimple_call_set_lhs (*save, tmp_var);
1137 
1138   *restore
1139     = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1140 			 1, tmp_var);
1141 }
1142 
1143 /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable.  */
1144 
1145 static tree
1146 build_asan_poison_call_expr (tree decl)
1147 {
1148   /* Do not poison variables that have size equal to zero.  */
1149   tree unit_size = DECL_SIZE_UNIT (decl);
1150   if (zerop (unit_size))
1151     return NULL_TREE;
1152 
1153   tree base = build_fold_addr_expr (decl);
1154 
1155   return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1156 				       void_type_node, 3,
1157 				       build_int_cst (integer_type_node,
1158 						      ASAN_MARK_POISON),
1159 				       base, unit_size);
1160 }
1161 
1162 /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1163    on POISON flag, shadow memory of a DECL variable.  The call will be
1164    put on location identified by IT iterator, where BEFORE flag drives
1165    position where the stmt will be put.  */
1166 
1167 static void
1168 asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1169 		      bool before)
1170 {
1171   tree unit_size = DECL_SIZE_UNIT (decl);
1172   tree base = build_fold_addr_expr (decl);
1173 
1174   /* Do not poison variables that have size equal to zero.  */
1175   if (zerop (unit_size))
1176     return;
1177 
1178   /* It's necessary to have all stack variables aligned to ASAN granularity
1179      bytes.  */
1180   if (DECL_ALIGN_UNIT (decl) <= ASAN_SHADOW_GRANULARITY)
1181     SET_DECL_ALIGN (decl, BITS_PER_UNIT * ASAN_SHADOW_GRANULARITY);
1182 
1183   HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1184 
1185   gimple *g
1186     = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1187 				  build_int_cst (integer_type_node, flags),
1188 				  base, unit_size);
1189 
1190   if (before)
1191     gsi_insert_before (it, g, GSI_NEW_STMT);
1192   else
1193     gsi_insert_after (it, g, GSI_NEW_STMT);
1194 }
1195 
1196 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1197    either poisons or unpoisons a DECL.  Created statement is appended
1198    to SEQ_P gimple sequence.  */
1199 
1200 static void
1201 asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1202 {
1203   gimple_stmt_iterator it = gsi_last (*seq_p);
1204   bool before = false;
1205 
1206   if (gsi_end_p (it))
1207     before = true;
1208 
1209   asan_poison_variable (decl, poison, &it, before);
1210 }
1211 
1212 /* Sort pair of VAR_DECLs A and B by DECL_UID.  */
1213 
1214 static int
1215 sort_by_decl_uid (const void *a, const void *b)
1216 {
1217   const tree *t1 = (const tree *)a;
1218   const tree *t2 = (const tree *)b;
1219 
1220   int uid1 = DECL_UID (*t1);
1221   int uid2 = DECL_UID (*t2);
1222 
1223   if (uid1 < uid2)
1224     return -1;
1225   else if (uid1 > uid2)
1226     return 1;
1227   else
1228     return 0;
1229 }
1230 
1231 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1232    depending on POISON flag.  Created statement is appended
1233    to SEQ_P gimple sequence.  */
1234 
1235 static void
1236 asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1237 {
1238   unsigned c = variables->elements ();
1239   if (c == 0)
1240     return;
1241 
1242   auto_vec<tree> sorted_variables (c);
1243 
1244   for (hash_set<tree>::iterator it = variables->begin ();
1245        it != variables->end (); ++it)
1246     sorted_variables.safe_push (*it);
1247 
1248   sorted_variables.qsort (sort_by_decl_uid);
1249 
1250   unsigned i;
1251   tree var;
1252   FOR_EACH_VEC_ELT (sorted_variables, i, var)
1253     {
1254       asan_poison_variable (var, poison, seq_p);
1255 
1256       /* Add use_after_scope_memory attribute for the variable in order
1257 	 to prevent re-written into SSA.  */
1258       if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1259 			     DECL_ATTRIBUTES (var)))
1260 	DECL_ATTRIBUTES (var)
1261 	  = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1262 		       integer_one_node,
1263 		       DECL_ATTRIBUTES (var));
1264     }
1265 }
1266 
1267 /* Gimplify a BIND_EXPR.  Just voidify and recurse.  */
1268 
1269 static enum gimplify_status
1270 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1271 {
1272   tree bind_expr = *expr_p;
1273   bool old_keep_stack = gimplify_ctxp->keep_stack;
1274   bool old_save_stack = gimplify_ctxp->save_stack;
1275   tree t;
1276   gbind *bind_stmt;
1277   gimple_seq body, cleanup;
1278   gcall *stack_save;
1279   location_t start_locus = 0, end_locus = 0;
1280   tree ret_clauses = NULL;
1281 
1282   tree temp = voidify_wrapper_expr (bind_expr, NULL);
1283 
1284   /* Mark variables seen in this bind expr.  */
1285   for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1286     {
1287       if (VAR_P (t))
1288 	{
1289 	  struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1290 
1291 	  /* Mark variable as local.  */
1292 	  if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1293 	      && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1294 		  || splay_tree_lookup (ctx->variables,
1295 					(splay_tree_key) t) == NULL))
1296 	    {
1297 	      if (ctx->region_type == ORT_SIMD
1298 		  && TREE_ADDRESSABLE (t)
1299 		  && !TREE_STATIC (t))
1300 		omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1301 	      else
1302 		omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1303 	    }
1304 
1305 	  DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1306 
1307 	  if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1308 	    cfun->has_local_explicit_reg_vars = true;
1309 	}
1310 
1311       /* Preliminarily mark non-addressed complex variables as eligible
1312 	 for promotion to gimple registers.  We'll transform their uses
1313 	 as we find them.  */
1314       if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1315 	   || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1316 	  && !TREE_THIS_VOLATILE (t)
1317 	  && (VAR_P (t) && !DECL_HARD_REGISTER (t))
1318 	  && !needs_to_live_in_memory (t))
1319 	DECL_GIMPLE_REG_P (t) = 1;
1320     }
1321 
1322   bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1323 				 BIND_EXPR_BLOCK (bind_expr));
1324   gimple_push_bind_expr (bind_stmt);
1325 
1326   gimplify_ctxp->keep_stack = false;
1327   gimplify_ctxp->save_stack = false;
1328 
1329   /* Gimplify the body into the GIMPLE_BIND tuple's body.  */
1330   body = NULL;
1331   gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1332   gimple_bind_set_body (bind_stmt, body);
1333 
1334   /* Source location wise, the cleanup code (stack_restore and clobbers)
1335      belongs to the end of the block, so propagate what we have.  The
1336      stack_save operation belongs to the beginning of block, which we can
1337      infer from the bind_expr directly if the block has no explicit
1338      assignment.  */
1339   if (BIND_EXPR_BLOCK (bind_expr))
1340     {
1341       end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1342       start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1343     }
1344   if (start_locus == 0)
1345     start_locus = EXPR_LOCATION (bind_expr);
1346 
1347   cleanup = NULL;
1348   stack_save = NULL;
1349 
1350   /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1351      the stack space allocated to the VLAs.  */
1352   if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1353     {
1354       gcall *stack_restore;
1355 
1356       /* Save stack on entry and restore it on exit.  Add a try_finally
1357 	 block to achieve this.  */
1358       build_stack_save_restore (&stack_save, &stack_restore);
1359 
1360       gimple_set_location (stack_save, start_locus);
1361       gimple_set_location (stack_restore, end_locus);
1362 
1363       gimplify_seq_add_stmt (&cleanup, stack_restore);
1364     }
1365 
1366   /* Add clobbers for all variables that go out of scope.  */
1367   for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1368     {
1369       if (VAR_P (t)
1370 	  && !is_global_var (t)
1371 	  && DECL_CONTEXT (t) == current_function_decl)
1372 	{
1373 	  if (!DECL_HARD_REGISTER (t)
1374 	      && !TREE_THIS_VOLATILE (t)
1375 	      && !DECL_HAS_VALUE_EXPR_P (t)
1376 	      /* Only care for variables that have to be in memory.  Others
1377 		 will be rewritten into SSA names, hence moved to the
1378 		 top-level.  */
1379 	      && !is_gimple_reg (t)
1380 	      && flag_stack_reuse != SR_NONE)
1381 	    {
1382 	      tree clobber = build_constructor (TREE_TYPE (t), NULL);
1383 	      gimple *clobber_stmt;
1384 	      TREE_THIS_VOLATILE (clobber) = 1;
1385 	      clobber_stmt = gimple_build_assign (t, clobber);
1386 	      gimple_set_location (clobber_stmt, end_locus);
1387 	      gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1388 	    }
1389 
1390 	  if (flag_openacc && oacc_declare_returns != NULL)
1391 	    {
1392 	      tree *c = oacc_declare_returns->get (t);
1393 	      if (c != NULL)
1394 		{
1395 		  if (ret_clauses)
1396 		    OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1397 
1398 		  ret_clauses = *c;
1399 
1400 		  oacc_declare_returns->remove (t);
1401 
1402 		  if (oacc_declare_returns->elements () == 0)
1403 		    {
1404 		      delete oacc_declare_returns;
1405 		      oacc_declare_returns = NULL;
1406 		    }
1407 		}
1408 	    }
1409 	}
1410 
1411       if (asan_poisoned_variables != NULL
1412 	  && asan_poisoned_variables->contains (t))
1413 	{
1414 	  asan_poisoned_variables->remove (t);
1415 	  asan_poison_variable (t, true, &cleanup);
1416 	}
1417 
1418       if (gimplify_ctxp->live_switch_vars != NULL
1419 	  && gimplify_ctxp->live_switch_vars->contains (t))
1420 	gimplify_ctxp->live_switch_vars->remove (t);
1421     }
1422 
1423   if (ret_clauses)
1424     {
1425       gomp_target *stmt;
1426       gimple_stmt_iterator si = gsi_start (cleanup);
1427 
1428       stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1429 				      ret_clauses);
1430       gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1431     }
1432 
1433   if (cleanup)
1434     {
1435       gtry *gs;
1436       gimple_seq new_body;
1437 
1438       new_body = NULL;
1439       gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1440 	  		     GIMPLE_TRY_FINALLY);
1441 
1442       if (stack_save)
1443 	gimplify_seq_add_stmt (&new_body, stack_save);
1444       gimplify_seq_add_stmt (&new_body, gs);
1445       gimple_bind_set_body (bind_stmt, new_body);
1446     }
1447 
1448   /* keep_stack propagates all the way up to the outermost BIND_EXPR.  */
1449   if (!gimplify_ctxp->keep_stack)
1450     gimplify_ctxp->keep_stack = old_keep_stack;
1451   gimplify_ctxp->save_stack = old_save_stack;
1452 
1453   gimple_pop_bind_expr ();
1454 
1455   gimplify_seq_add_stmt (pre_p, bind_stmt);
1456 
1457   if (temp)
1458     {
1459       *expr_p = temp;
1460       return GS_OK;
1461     }
1462 
1463   *expr_p = NULL_TREE;
1464   return GS_ALL_DONE;
1465 }
1466 
1467 /* Maybe add early return predict statement to PRE_P sequence.  */
1468 
1469 static void
1470 maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1471 {
1472   /* If we are not in a conditional context, add PREDICT statement.  */
1473   if (gimple_conditional_context ())
1474     {
1475       gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1476 					      NOT_TAKEN);
1477       gimplify_seq_add_stmt (pre_p, predict);
1478     }
1479 }
1480 
1481 /* Gimplify a RETURN_EXPR.  If the expression to be returned is not a
1482    GIMPLE value, it is assigned to a new temporary and the statement is
1483    re-written to return the temporary.
1484 
1485    PRE_P points to the sequence where side effects that must happen before
1486    STMT should be stored.  */
1487 
1488 static enum gimplify_status
1489 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1490 {
1491   greturn *ret;
1492   tree ret_expr = TREE_OPERAND (stmt, 0);
1493   tree result_decl, result;
1494 
1495   if (ret_expr == error_mark_node)
1496     return GS_ERROR;
1497 
1498   if (!ret_expr
1499       || TREE_CODE (ret_expr) == RESULT_DECL)
1500     {
1501       maybe_add_early_return_predict_stmt (pre_p);
1502       greturn *ret = gimple_build_return (ret_expr);
1503       gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1504       gimplify_seq_add_stmt (pre_p, ret);
1505       return GS_ALL_DONE;
1506     }
1507 
1508   if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1509     result_decl = NULL_TREE;
1510   else
1511     {
1512       result_decl = TREE_OPERAND (ret_expr, 0);
1513 
1514       /* See through a return by reference.  */
1515       if (TREE_CODE (result_decl) == INDIRECT_REF)
1516 	result_decl = TREE_OPERAND (result_decl, 0);
1517 
1518       gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1519 		   || TREE_CODE (ret_expr) == INIT_EXPR)
1520 		  && TREE_CODE (result_decl) == RESULT_DECL);
1521     }
1522 
1523   /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1524      Recall that aggregate_value_p is FALSE for any aggregate type that is
1525      returned in registers.  If we're returning values in registers, then
1526      we don't want to extend the lifetime of the RESULT_DECL, particularly
1527      across another call.  In addition, for those aggregates for which
1528      hard_function_value generates a PARALLEL, we'll die during normal
1529      expansion of structure assignments; there's special code in expand_return
1530      to handle this case that does not exist in expand_expr.  */
1531   if (!result_decl)
1532     result = NULL_TREE;
1533   else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1534     {
1535       if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1536 	{
1537 	  if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1538 	    gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1539 	  /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1540 	     should be effectively allocated by the caller, i.e. all calls to
1541 	     this function must be subject to the Return Slot Optimization.  */
1542 	  gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1543 	  gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1544 	}
1545       result = result_decl;
1546     }
1547   else if (gimplify_ctxp->return_temp)
1548     result = gimplify_ctxp->return_temp;
1549   else
1550     {
1551       result = create_tmp_reg (TREE_TYPE (result_decl));
1552 
1553       /* ??? With complex control flow (usually involving abnormal edges),
1554 	 we can wind up warning about an uninitialized value for this.  Due
1555 	 to how this variable is constructed and initialized, this is never
1556 	 true.  Give up and never warn.  */
1557       TREE_NO_WARNING (result) = 1;
1558 
1559       gimplify_ctxp->return_temp = result;
1560     }
1561 
1562   /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1563      Then gimplify the whole thing.  */
1564   if (result != result_decl)
1565     TREE_OPERAND (ret_expr, 0) = result;
1566 
1567   gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1568 
1569   maybe_add_early_return_predict_stmt (pre_p);
1570   ret = gimple_build_return (result);
1571   gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1572   gimplify_seq_add_stmt (pre_p, ret);
1573 
1574   return GS_ALL_DONE;
1575 }
1576 
1577 /* Gimplify a variable-length array DECL.  */
1578 
1579 static void
1580 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1581 {
1582   /* This is a variable-sized decl.  Simplify its size and mark it
1583      for deferred expansion.  */
1584   tree t, addr, ptr_type;
1585 
1586   gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1587   gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1588 
1589   /* Don't mess with a DECL_VALUE_EXPR set by the front-end.  */
1590   if (DECL_HAS_VALUE_EXPR_P (decl))
1591     return;
1592 
1593   /* All occurrences of this decl in final gimplified code will be
1594      replaced by indirection.  Setting DECL_VALUE_EXPR does two
1595      things: First, it lets the rest of the gimplifier know what
1596      replacement to use.  Second, it lets the debug info know
1597      where to find the value.  */
1598   ptr_type = build_pointer_type (TREE_TYPE (decl));
1599   addr = create_tmp_var (ptr_type, get_name (decl));
1600   DECL_IGNORED_P (addr) = 0;
1601   t = build_fold_indirect_ref (addr);
1602   TREE_THIS_NOTRAP (t) = 1;
1603   SET_DECL_VALUE_EXPR (decl, t);
1604   DECL_HAS_VALUE_EXPR_P (decl) = 1;
1605 
1606   t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1607 			      max_int_size_in_bytes (TREE_TYPE (decl)));
1608   /* The call has been built for a variable-sized object.  */
1609   CALL_ALLOCA_FOR_VAR_P (t) = 1;
1610   t = fold_convert (ptr_type, t);
1611   t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1612 
1613   gimplify_and_add (t, seq_p);
1614 }
1615 
1616 /* A helper function to be called via walk_tree.  Mark all labels under *TP
1617    as being forced.  To be called for DECL_INITIAL of static variables.  */
1618 
1619 static tree
1620 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1621 {
1622   if (TYPE_P (*tp))
1623     *walk_subtrees = 0;
1624   if (TREE_CODE (*tp) == LABEL_DECL)
1625     {
1626       FORCED_LABEL (*tp) = 1;
1627       cfun->has_forced_label_in_static = 1;
1628     }
1629 
1630   return NULL_TREE;
1631 }
1632 
1633 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1634    and initialization explicit.  */
1635 
1636 static enum gimplify_status
1637 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1638 {
1639   tree stmt = *stmt_p;
1640   tree decl = DECL_EXPR_DECL (stmt);
1641 
1642   *stmt_p = NULL_TREE;
1643 
1644   if (TREE_TYPE (decl) == error_mark_node)
1645     return GS_ERROR;
1646 
1647   if ((TREE_CODE (decl) == TYPE_DECL
1648        || VAR_P (decl))
1649       && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1650     {
1651       gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1652       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1653 	gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1654     }
1655 
1656   /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1657      in case its size expressions contain problematic nodes like CALL_EXPR.  */
1658   if (TREE_CODE (decl) == TYPE_DECL
1659       && DECL_ORIGINAL_TYPE (decl)
1660       && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1661     {
1662       gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1663       if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1664 	gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1665     }
1666 
1667   if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1668     {
1669       tree init = DECL_INITIAL (decl);
1670       bool is_vla = false;
1671 
1672       if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1673 	  || (!TREE_STATIC (decl)
1674 	      && flag_stack_check == GENERIC_STACK_CHECK
1675 	      && compare_tree_int (DECL_SIZE_UNIT (decl),
1676 				   STACK_CHECK_MAX_VAR_SIZE) > 0))
1677 	{
1678 	  gimplify_vla_decl (decl, seq_p);
1679 	  is_vla = true;
1680 	}
1681 
1682       if (asan_poisoned_variables
1683 	  && !is_vla
1684 	  && TREE_ADDRESSABLE (decl)
1685 	  && !TREE_STATIC (decl)
1686 	  && !DECL_HAS_VALUE_EXPR_P (decl)
1687 	  && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
1688 	  && dbg_cnt (asan_use_after_scope)
1689 	  && !gimplify_omp_ctxp)
1690 	{
1691 	  asan_poisoned_variables->add (decl);
1692 	  asan_poison_variable (decl, false, seq_p);
1693 	  if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
1694 	    gimplify_ctxp->live_switch_vars->add (decl);
1695 	}
1696 
1697       /* Some front ends do not explicitly declare all anonymous
1698 	 artificial variables.  We compensate here by declaring the
1699 	 variables, though it would be better if the front ends would
1700 	 explicitly declare them.  */
1701       if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1702 	  && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1703 	gimple_add_tmp_var (decl);
1704 
1705       if (init && init != error_mark_node)
1706 	{
1707 	  if (!TREE_STATIC (decl))
1708 	    {
1709 	      DECL_INITIAL (decl) = NULL_TREE;
1710 	      init = build2 (INIT_EXPR, void_type_node, decl, init);
1711 	      gimplify_and_add (init, seq_p);
1712 	      ggc_free (init);
1713 	    }
1714 	  else
1715 	    /* We must still examine initializers for static variables
1716 	       as they may contain a label address.  */
1717 	    walk_tree (&init, force_labels_r, NULL, NULL);
1718 	}
1719     }
1720 
1721   return GS_ALL_DONE;
1722 }
1723 
1724 /* Gimplify a LOOP_EXPR.  Normally this just involves gimplifying the body
1725    and replacing the LOOP_EXPR with goto, but if the loop contains an
1726    EXIT_EXPR, we need to append a label for it to jump to.  */
1727 
1728 static enum gimplify_status
1729 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1730 {
1731   tree saved_label = gimplify_ctxp->exit_label;
1732   tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1733 
1734   gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1735 
1736   gimplify_ctxp->exit_label = NULL_TREE;
1737 
1738   gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1739 
1740   gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1741 
1742   if (gimplify_ctxp->exit_label)
1743     gimplify_seq_add_stmt (pre_p,
1744 			   gimple_build_label (gimplify_ctxp->exit_label));
1745 
1746   gimplify_ctxp->exit_label = saved_label;
1747 
1748   *expr_p = NULL;
1749   return GS_ALL_DONE;
1750 }
1751 
1752 /* Gimplify a statement list onto a sequence.  These may be created either
1753    by an enlightened front-end, or by shortcut_cond_expr.  */
1754 
1755 static enum gimplify_status
1756 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1757 {
1758   tree temp = voidify_wrapper_expr (*expr_p, NULL);
1759 
1760   tree_stmt_iterator i = tsi_start (*expr_p);
1761 
1762   while (!tsi_end_p (i))
1763     {
1764       gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1765       tsi_delink (&i);
1766     }
1767 
1768   if (temp)
1769     {
1770       *expr_p = temp;
1771       return GS_OK;
1772     }
1773 
1774   return GS_ALL_DONE;
1775 }
1776 
1777 /* Callback for walk_gimple_seq.  */
1778 
1779 static tree
1780 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1781 			   struct walk_stmt_info *wi)
1782 {
1783   gimple *stmt = gsi_stmt (*gsi_p);
1784 
1785   *handled_ops_p = true;
1786   switch (gimple_code (stmt))
1787     {
1788     case GIMPLE_TRY:
1789       /* A compiler-generated cleanup or a user-written try block.
1790 	 If it's empty, don't dive into it--that would result in
1791 	 worse location info.  */
1792       if (gimple_try_eval (stmt) == NULL)
1793 	{
1794 	  wi->info = stmt;
1795 	  return integer_zero_node;
1796 	}
1797       /* Fall through.  */
1798     case GIMPLE_BIND:
1799     case GIMPLE_CATCH:
1800     case GIMPLE_EH_FILTER:
1801     case GIMPLE_TRANSACTION:
1802       /* Walk the sub-statements.  */
1803       *handled_ops_p = false;
1804       break;
1805 
1806     case GIMPLE_DEBUG:
1807       /* Ignore these.  We may generate them before declarations that
1808 	 are never executed.  If there's something to warn about,
1809 	 there will be non-debug stmts too, and we'll catch those.  */
1810       break;
1811 
1812     case GIMPLE_CALL:
1813       if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1814 	{
1815 	  *handled_ops_p = false;
1816 	  break;
1817 	}
1818       /* Fall through.  */
1819     default:
1820       /* Save the first "real" statement (not a decl/lexical scope/...).  */
1821       wi->info = stmt;
1822       return integer_zero_node;
1823     }
1824   return NULL_TREE;
1825 }
1826 
1827 /* Possibly warn about unreachable statements between switch's controlling
1828    expression and the first case.  SEQ is the body of a switch expression.  */
1829 
1830 static void
1831 maybe_warn_switch_unreachable (gimple_seq seq)
1832 {
1833   if (!warn_switch_unreachable
1834       /* This warning doesn't play well with Fortran when optimizations
1835 	 are on.  */
1836       || lang_GNU_Fortran ()
1837       || seq == NULL)
1838     return;
1839 
1840   struct walk_stmt_info wi;
1841   memset (&wi, 0, sizeof (wi));
1842   walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1843   gimple *stmt = (gimple *) wi.info;
1844 
1845   if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1846     {
1847       if (gimple_code (stmt) == GIMPLE_GOTO
1848 	  && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1849 	  && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1850 	/* Don't warn for compiler-generated gotos.  These occur
1851 	   in Duff's devices, for example.  */;
1852       else
1853 	warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1854 		    "statement will never be executed");
1855     }
1856 }
1857 
1858 
1859 /* A label entry that pairs label and a location.  */
1860 struct label_entry
1861 {
1862   tree label;
1863   location_t loc;
1864 };
1865 
1866 /* Find LABEL in vector of label entries VEC.  */
1867 
1868 static struct label_entry *
1869 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1870 {
1871   unsigned int i;
1872   struct label_entry *l;
1873 
1874   FOR_EACH_VEC_ELT (*vec, i, l)
1875     if (l->label == label)
1876       return l;
1877   return NULL;
1878 }
1879 
1880 /* Return true if LABEL, a LABEL_DECL, represents a case label
1881    in a vector of labels CASES.  */
1882 
1883 static bool
1884 case_label_p (const vec<tree> *cases, tree label)
1885 {
1886   unsigned int i;
1887   tree l;
1888 
1889   FOR_EACH_VEC_ELT (*cases, i, l)
1890     if (CASE_LABEL (l) == label)
1891       return true;
1892   return false;
1893 }
1894 
1895 /* Find the last nondebug statement in a scope STMT.  */
1896 
1897 static gimple *
1898 last_stmt_in_scope (gimple *stmt)
1899 {
1900   if (!stmt)
1901     return NULL;
1902 
1903   switch (gimple_code (stmt))
1904     {
1905     case GIMPLE_BIND:
1906       {
1907 	gbind *bind = as_a <gbind *> (stmt);
1908 	stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
1909 	return last_stmt_in_scope (stmt);
1910       }
1911 
1912     case GIMPLE_TRY:
1913       {
1914 	gtry *try_stmt = as_a <gtry *> (stmt);
1915 	stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
1916 	gimple *last_eval = last_stmt_in_scope (stmt);
1917 	if (gimple_stmt_may_fallthru (last_eval)
1918 	    && (last_eval == NULL
1919 		|| !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1920 	    && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1921 	  {
1922 	    stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
1923 	    return last_stmt_in_scope (stmt);
1924 	  }
1925 	else
1926 	  return last_eval;
1927       }
1928 
1929     case GIMPLE_DEBUG:
1930       gcc_unreachable ();
1931 
1932     default:
1933       return stmt;
1934     }
1935 }
1936 
1937 /* Collect interesting labels in LABELS and return the statement preceding
1938    another case label, or a user-defined label.  */
1939 
1940 static gimple *
1941 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
1942 			    auto_vec <struct label_entry> *labels)
1943 {
1944   gimple *prev = NULL;
1945 
1946   do
1947     {
1948       if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
1949 	{
1950 	  /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
1951 	     which starts on a GIMPLE_SWITCH and ends with a break label.
1952 	     Handle that as a single statement that can fall through.  */
1953 	  gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
1954 	  gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
1955 	  gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
1956 	  if (last
1957 	      && gimple_code (first) == GIMPLE_SWITCH
1958 	      && gimple_code (last) == GIMPLE_LABEL)
1959 	    {
1960 	      tree label = gimple_label_label (as_a <glabel *> (last));
1961 	      if (SWITCH_BREAK_LABEL_P (label))
1962 		{
1963 		  prev = bind;
1964 		  gsi_next (gsi_p);
1965 		  continue;
1966 		}
1967 	    }
1968 	}
1969       if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
1970 	  || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
1971 	{
1972 	  /* Nested scope.  Only look at the last statement of
1973 	     the innermost scope.  */
1974 	  location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
1975 	  gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
1976 	  if (last)
1977 	    {
1978 	      prev = last;
1979 	      /* It might be a label without a location.  Use the
1980 		 location of the scope then.  */
1981 	      if (!gimple_has_location (prev))
1982 		gimple_set_location (prev, bind_loc);
1983 	    }
1984 	  gsi_next (gsi_p);
1985 	  continue;
1986 	}
1987 
1988       /* Ifs are tricky.  */
1989       if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
1990 	{
1991 	  gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
1992 	  tree false_lab = gimple_cond_false_label (cond_stmt);
1993 	  location_t if_loc = gimple_location (cond_stmt);
1994 
1995 	  /* If we have e.g.
1996 	       if (i > 1) goto <D.2259>; else goto D;
1997 	     we can't do much with the else-branch.  */
1998 	  if (!DECL_ARTIFICIAL (false_lab))
1999 	    break;
2000 
2001 	  /* Go on until the false label, then one step back.  */
2002 	  for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2003 	    {
2004 	      gimple *stmt = gsi_stmt (*gsi_p);
2005 	      if (gimple_code (stmt) == GIMPLE_LABEL
2006 		  && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2007 		break;
2008 	    }
2009 
2010 	  /* Not found?  Oops.  */
2011 	  if (gsi_end_p (*gsi_p))
2012 	    break;
2013 
2014 	  struct label_entry l = { false_lab, if_loc };
2015 	  labels->safe_push (l);
2016 
2017 	  /* Go to the last statement of the then branch.  */
2018 	  gsi_prev (gsi_p);
2019 
2020 	  /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2021 	     <D.1759>:
2022 	     <stmt>;
2023 	     goto <D.1761>;
2024 	     <D.1760>:
2025 	   */
2026 	  if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2027 	      && !gimple_has_location (gsi_stmt (*gsi_p)))
2028 	    {
2029 	      /* Look at the statement before, it might be
2030 		 attribute fallthrough, in which case don't warn.  */
2031 	      gsi_prev (gsi_p);
2032 	      bool fallthru_before_dest
2033 		= gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2034 	      gsi_next (gsi_p);
2035 	      tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2036 	      if (!fallthru_before_dest)
2037 		{
2038 		  struct label_entry l = { goto_dest, if_loc };
2039 		  labels->safe_push (l);
2040 		}
2041 	    }
2042 	  /* And move back.  */
2043 	  gsi_next (gsi_p);
2044 	}
2045 
2046       /* Remember the last statement.  Skip labels that are of no interest
2047 	 to us.  */
2048       if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2049 	{
2050 	  tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2051 	  if (find_label_entry (labels, label))
2052 	    prev = gsi_stmt (*gsi_p);
2053 	}
2054       else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2055 	;
2056       else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2057 	prev = gsi_stmt (*gsi_p);
2058       gsi_next (gsi_p);
2059     }
2060   while (!gsi_end_p (*gsi_p)
2061 	 /* Stop if we find a case or a user-defined label.  */
2062 	 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2063 	     || !gimple_has_location (gsi_stmt (*gsi_p))));
2064 
2065   return prev;
2066 }
2067 
2068 /* Return true if the switch fallthough warning should occur.  LABEL is
2069    the label statement that we're falling through to.  */
2070 
2071 static bool
2072 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2073 {
2074   gimple_stmt_iterator gsi = *gsi_p;
2075 
2076   /* Don't warn if the label is marked with a "falls through" comment.  */
2077   if (FALLTHROUGH_LABEL_P (label))
2078     return false;
2079 
2080   /* Don't warn for non-case labels followed by a statement:
2081        case 0:
2082 	 foo ();
2083        label:
2084 	 bar ();
2085      as these are likely intentional.  */
2086   if (!case_label_p (&gimplify_ctxp->case_labels, label))
2087     {
2088       tree l;
2089       while (!gsi_end_p (gsi)
2090 	     && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2091 	     && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2092 	     && !case_label_p (&gimplify_ctxp->case_labels, l))
2093 	gsi_next_nondebug (&gsi);
2094       if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2095 	return false;
2096     }
2097 
2098   /* Don't warn for terminated branches, i.e. when the subsequent case labels
2099      immediately breaks.  */
2100   gsi = *gsi_p;
2101 
2102   /* Skip all immediately following labels.  */
2103   while (!gsi_end_p (gsi)
2104 	 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2105 	     || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2106     gsi_next_nondebug (&gsi);
2107 
2108   /* { ... something; default:; } */
2109   if (gsi_end_p (gsi)
2110       /* { ... something; default: break; } or
2111 	 { ... something; default: goto L; } */
2112       || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2113       /* { ... something; default: return; } */
2114       || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2115     return false;
2116 
2117   return true;
2118 }
2119 
2120 /* Callback for walk_gimple_seq.  */
2121 
2122 static tree
2123 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2124 			     struct walk_stmt_info *)
2125 {
2126   gimple *stmt = gsi_stmt (*gsi_p);
2127 
2128   *handled_ops_p = true;
2129   switch (gimple_code (stmt))
2130     {
2131     case GIMPLE_TRY:
2132     case GIMPLE_BIND:
2133     case GIMPLE_CATCH:
2134     case GIMPLE_EH_FILTER:
2135     case GIMPLE_TRANSACTION:
2136       /* Walk the sub-statements.  */
2137       *handled_ops_p = false;
2138       break;
2139 
2140     /* Find a sequence of form:
2141 
2142        GIMPLE_LABEL
2143        [...]
2144        <may fallthru stmt>
2145        GIMPLE_LABEL
2146 
2147        and possibly warn.  */
2148     case GIMPLE_LABEL:
2149       {
2150 	/* Found a label.  Skip all immediately following labels.  */
2151 	while (!gsi_end_p (*gsi_p)
2152 	       && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2153 	  gsi_next_nondebug (gsi_p);
2154 
2155 	/* There might be no more statements.  */
2156 	if (gsi_end_p (*gsi_p))
2157 	  return integer_zero_node;
2158 
2159 	/* Vector of labels that fall through.  */
2160 	auto_vec <struct label_entry> labels;
2161 	gimple *prev = collect_fallthrough_labels (gsi_p, &labels);
2162 
2163 	/* There might be no more statements.  */
2164 	if (gsi_end_p (*gsi_p))
2165 	  return integer_zero_node;
2166 
2167 	gimple *next = gsi_stmt (*gsi_p);
2168 	tree label;
2169 	/* If what follows is a label, then we may have a fallthrough.  */
2170 	if (gimple_code (next) == GIMPLE_LABEL
2171 	    && gimple_has_location (next)
2172 	    && (label = gimple_label_label (as_a <glabel *> (next)))
2173 	    && prev != NULL)
2174 	  {
2175 	    struct label_entry *l;
2176 	    bool warned_p = false;
2177 	    if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2178 	      /* Quiet.  */;
2179 	    else if (gimple_code (prev) == GIMPLE_LABEL
2180 		     && (label = gimple_label_label (as_a <glabel *> (prev)))
2181 		     && (l = find_label_entry (&labels, label)))
2182 	      warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2183 				     "this statement may fall through");
2184 	    else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2185 		     /* Try to be clever and don't warn when the statement
2186 			can't actually fall through.  */
2187 		     && gimple_stmt_may_fallthru (prev)
2188 		     && gimple_has_location (prev))
2189 	      warned_p = warning_at (gimple_location (prev),
2190 				     OPT_Wimplicit_fallthrough_,
2191 				     "this statement may fall through");
2192 	    if (warned_p)
2193 	      inform (gimple_location (next), "here");
2194 
2195 	    /* Mark this label as processed so as to prevent multiple
2196 	       warnings in nested switches.  */
2197 	    FALLTHROUGH_LABEL_P (label) = true;
2198 
2199 	    /* So that next warn_implicit_fallthrough_r will start looking for
2200 	       a new sequence starting with this label.  */
2201 	    gsi_prev (gsi_p);
2202 	  }
2203       }
2204       break;
2205    default:
2206       break;
2207     }
2208   return NULL_TREE;
2209 }
2210 
2211 /* Warn when a switch case falls through.  */
2212 
2213 static void
2214 maybe_warn_implicit_fallthrough (gimple_seq seq)
2215 {
2216   if (!warn_implicit_fallthrough)
2217     return;
2218 
2219   /* This warning is meant for C/C++/ObjC/ObjC++ only.  */
2220   if (!(lang_GNU_C ()
2221 	|| lang_GNU_CXX ()
2222 	|| lang_GNU_OBJC ()))
2223     return;
2224 
2225   struct walk_stmt_info wi;
2226   memset (&wi, 0, sizeof (wi));
2227   walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2228 }
2229 
2230 /* Callback for walk_gimple_seq.  */
2231 
2232 static tree
2233 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2234 		      struct walk_stmt_info *)
2235 {
2236   gimple *stmt = gsi_stmt (*gsi_p);
2237 
2238   *handled_ops_p = true;
2239   switch (gimple_code (stmt))
2240     {
2241     case GIMPLE_TRY:
2242     case GIMPLE_BIND:
2243     case GIMPLE_CATCH:
2244     case GIMPLE_EH_FILTER:
2245     case GIMPLE_TRANSACTION:
2246       /* Walk the sub-statements.  */
2247       *handled_ops_p = false;
2248       break;
2249     case GIMPLE_CALL:
2250       if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2251 	{
2252 	  gsi_remove (gsi_p, true);
2253 	  if (gsi_end_p (*gsi_p))
2254 	    return integer_zero_node;
2255 
2256 	  bool found = false;
2257 	  location_t loc = gimple_location (stmt);
2258 
2259 	  gimple_stmt_iterator gsi2 = *gsi_p;
2260 	  stmt = gsi_stmt (gsi2);
2261 	  if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2262 	    {
2263 	      /* Go on until the artificial label.  */
2264 	      tree goto_dest = gimple_goto_dest (stmt);
2265 	      for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2266 		{
2267 		  if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2268 		      && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2269 			   == goto_dest)
2270 		    break;
2271 		}
2272 
2273 	      /* Not found?  Stop.  */
2274 	      if (gsi_end_p (gsi2))
2275 		break;
2276 
2277 	      /* Look one past it.  */
2278 	      gsi_next (&gsi2);
2279 	    }
2280 
2281 	  /* We're looking for a case label or default label here.  */
2282 	  while (!gsi_end_p (gsi2))
2283 	    {
2284 	      stmt = gsi_stmt (gsi2);
2285 	      if (gimple_code (stmt) == GIMPLE_LABEL)
2286 		{
2287 		  tree label = gimple_label_label (as_a <glabel *> (stmt));
2288 		  if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2289 		    {
2290 		      found = true;
2291 		      break;
2292 		    }
2293 		}
2294 	      else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2295 		;
2296 	      else if (!is_gimple_debug (stmt))
2297 		/* Anything else is not expected.  */
2298 		break;
2299 	      gsi_next (&gsi2);
2300 	    }
2301 	  if (!found)
2302 	    warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2303 			"a case label or default label");
2304 	}
2305       break;
2306     default:
2307       break;
2308     }
2309   return NULL_TREE;
2310 }
2311 
2312 /* Expand all FALLTHROUGH () calls in SEQ.  */
2313 
2314 static void
2315 expand_FALLTHROUGH (gimple_seq *seq_p)
2316 {
2317   struct walk_stmt_info wi;
2318   memset (&wi, 0, sizeof (wi));
2319   walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2320 }
2321 
2322 
2323 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2324    branch to.  */
2325 
2326 static enum gimplify_status
2327 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2328 {
2329   tree switch_expr = *expr_p;
2330   gimple_seq switch_body_seq = NULL;
2331   enum gimplify_status ret;
2332   tree index_type = TREE_TYPE (switch_expr);
2333   if (index_type == NULL_TREE)
2334     index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2335 
2336   ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2337                        fb_rvalue);
2338   if (ret == GS_ERROR || ret == GS_UNHANDLED)
2339     return ret;
2340 
2341   if (SWITCH_BODY (switch_expr))
2342     {
2343       vec<tree> labels;
2344       vec<tree> saved_labels;
2345       hash_set<tree> *saved_live_switch_vars = NULL;
2346       tree default_case = NULL_TREE;
2347       gswitch *switch_stmt;
2348 
2349       /* Save old labels, get new ones from body, then restore the old
2350          labels.  Save all the things from the switch body to append after.  */
2351       saved_labels = gimplify_ctxp->case_labels;
2352       gimplify_ctxp->case_labels.create (8);
2353 
2354       /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR.  */
2355       saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2356       tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2357       if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2358 	gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2359       else
2360 	gimplify_ctxp->live_switch_vars = NULL;
2361 
2362       bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2363       gimplify_ctxp->in_switch_expr = true;
2364 
2365       gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2366 
2367       gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2368       maybe_warn_switch_unreachable (switch_body_seq);
2369       maybe_warn_implicit_fallthrough (switch_body_seq);
2370       /* Only do this for the outermost GIMPLE_SWITCH.  */
2371       if (!gimplify_ctxp->in_switch_expr)
2372 	expand_FALLTHROUGH (&switch_body_seq);
2373 
2374       labels = gimplify_ctxp->case_labels;
2375       gimplify_ctxp->case_labels = saved_labels;
2376 
2377       if (gimplify_ctxp->live_switch_vars)
2378 	{
2379 	  gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0);
2380 	  delete gimplify_ctxp->live_switch_vars;
2381 	}
2382       gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2383 
2384       preprocess_case_label_vec_for_gimple (labels, index_type,
2385 					    &default_case);
2386 
2387       bool add_bind = false;
2388       if (!default_case)
2389 	{
2390 	  glabel *new_default;
2391 
2392 	  default_case
2393 	    = build_case_label (NULL_TREE, NULL_TREE,
2394 				create_artificial_label (UNKNOWN_LOCATION));
2395 	  if (old_in_switch_expr)
2396 	    {
2397 	      SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
2398 	      add_bind = true;
2399 	    }
2400 	  new_default = gimple_build_label (CASE_LABEL (default_case));
2401 	  gimplify_seq_add_stmt (&switch_body_seq, new_default);
2402 	}
2403       else if (old_in_switch_expr)
2404 	{
2405 	  gimple *last = gimple_seq_last_stmt (switch_body_seq);
2406 	  if (last && gimple_code (last) == GIMPLE_LABEL)
2407 	    {
2408 	      tree label = gimple_label_label (as_a <glabel *> (last));
2409 	      if (SWITCH_BREAK_LABEL_P (label))
2410 		add_bind = true;
2411 	    }
2412 	}
2413 
2414       switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2415 					 default_case, labels);
2416       /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
2417 	 ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
2418 	 wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
2419 	 so that we can easily find the start and end of the switch
2420 	 statement.  */
2421       if (add_bind)
2422 	{
2423 	  gimple_seq bind_body = NULL;
2424 	  gimplify_seq_add_stmt (&bind_body, switch_stmt);
2425 	  gimple_seq_add_seq (&bind_body, switch_body_seq);
2426 	  gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
2427 	  gimple_set_location (bind, EXPR_LOCATION (switch_expr));
2428 	  gimplify_seq_add_stmt (pre_p, bind);
2429 	}
2430       else
2431 	{
2432 	  gimplify_seq_add_stmt (pre_p, switch_stmt);
2433 	  gimplify_seq_add_seq (pre_p, switch_body_seq);
2434 	}
2435       labels.release ();
2436     }
2437   else
2438     gcc_unreachable ();
2439 
2440   return GS_ALL_DONE;
2441 }
2442 
2443 /* Gimplify the LABEL_EXPR pointed to by EXPR_P.  */
2444 
2445 static enum gimplify_status
2446 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2447 {
2448   gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2449 	      == current_function_decl);
2450 
2451   tree label = LABEL_EXPR_LABEL (*expr_p);
2452   glabel *label_stmt = gimple_build_label (label);
2453   gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2454   gimplify_seq_add_stmt (pre_p, label_stmt);
2455 
2456   if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2457     gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2458 						      NOT_TAKEN));
2459   else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2460     gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2461 						      TAKEN));
2462 
2463   return GS_ALL_DONE;
2464 }
2465 
2466 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P.  */
2467 
2468 static enum gimplify_status
2469 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2470 {
2471   struct gimplify_ctx *ctxp;
2472   glabel *label_stmt;
2473 
2474   /* Invalid programs can play Duff's Device type games with, for example,
2475      #pragma omp parallel.  At least in the C front end, we don't
2476      detect such invalid branches until after gimplification, in the
2477      diagnose_omp_blocks pass.  */
2478   for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2479     if (ctxp->case_labels.exists ())
2480       break;
2481 
2482   label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
2483   gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2484   ctxp->case_labels.safe_push (*expr_p);
2485   gimplify_seq_add_stmt (pre_p, label_stmt);
2486 
2487   return GS_ALL_DONE;
2488 }
2489 
2490 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2491    if necessary.  */
2492 
2493 tree
2494 build_and_jump (tree *label_p)
2495 {
2496   if (label_p == NULL)
2497     /* If there's nowhere to jump, just fall through.  */
2498     return NULL_TREE;
2499 
2500   if (*label_p == NULL_TREE)
2501     {
2502       tree label = create_artificial_label (UNKNOWN_LOCATION);
2503       *label_p = label;
2504     }
2505 
2506   return build1 (GOTO_EXPR, void_type_node, *label_p);
2507 }
2508 
2509 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2510    This also involves building a label to jump to and communicating it to
2511    gimplify_loop_expr through gimplify_ctxp->exit_label.  */
2512 
2513 static enum gimplify_status
2514 gimplify_exit_expr (tree *expr_p)
2515 {
2516   tree cond = TREE_OPERAND (*expr_p, 0);
2517   tree expr;
2518 
2519   expr = build_and_jump (&gimplify_ctxp->exit_label);
2520   expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2521   *expr_p = expr;
2522 
2523   return GS_OK;
2524 }
2525 
2526 /* *EXPR_P is a COMPONENT_REF being used as an rvalue.  If its type is
2527    different from its canonical type, wrap the whole thing inside a
2528    NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2529    type.
2530 
2531    The canonical type of a COMPONENT_REF is the type of the field being
2532    referenced--unless the field is a bit-field which can be read directly
2533    in a smaller mode, in which case the canonical type is the
2534    sign-appropriate type corresponding to that mode.  */
2535 
2536 static void
2537 canonicalize_component_ref (tree *expr_p)
2538 {
2539   tree expr = *expr_p;
2540   tree type;
2541 
2542   gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2543 
2544   if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2545     type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2546   else
2547     type = TREE_TYPE (TREE_OPERAND (expr, 1));
2548 
2549   /* One could argue that all the stuff below is not necessary for
2550      the non-bitfield case and declare it a FE error if type
2551      adjustment would be needed.  */
2552   if (TREE_TYPE (expr) != type)
2553     {
2554 #ifdef ENABLE_TYPES_CHECKING
2555       tree old_type = TREE_TYPE (expr);
2556 #endif
2557       int type_quals;
2558 
2559       /* We need to preserve qualifiers and propagate them from
2560 	 operand 0.  */
2561       type_quals = TYPE_QUALS (type)
2562 	| TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2563       if (TYPE_QUALS (type) != type_quals)
2564 	type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2565 
2566       /* Set the type of the COMPONENT_REF to the underlying type.  */
2567       TREE_TYPE (expr) = type;
2568 
2569 #ifdef ENABLE_TYPES_CHECKING
2570       /* It is now a FE error, if the conversion from the canonical
2571 	 type to the original expression type is not useless.  */
2572       gcc_assert (useless_type_conversion_p (old_type, type));
2573 #endif
2574     }
2575 }
2576 
2577 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2578    to foo, embed that change in the ADDR_EXPR by converting
2579       T array[U];
2580       (T *)&array
2581    ==>
2582       &array[L]
2583    where L is the lower bound.  For simplicity, only do this for constant
2584    lower bound.
2585    The constraint is that the type of &array[L] is trivially convertible
2586    to T *.  */
2587 
2588 static void
2589 canonicalize_addr_expr (tree *expr_p)
2590 {
2591   tree expr = *expr_p;
2592   tree addr_expr = TREE_OPERAND (expr, 0);
2593   tree datype, ddatype, pddatype;
2594 
2595   /* We simplify only conversions from an ADDR_EXPR to a pointer type.  */
2596   if (!POINTER_TYPE_P (TREE_TYPE (expr))
2597       || TREE_CODE (addr_expr) != ADDR_EXPR)
2598     return;
2599 
2600   /* The addr_expr type should be a pointer to an array.  */
2601   datype = TREE_TYPE (TREE_TYPE (addr_expr));
2602   if (TREE_CODE (datype) != ARRAY_TYPE)
2603     return;
2604 
2605   /* The pointer to element type shall be trivially convertible to
2606      the expression pointer type.  */
2607   ddatype = TREE_TYPE (datype);
2608   pddatype = build_pointer_type (ddatype);
2609   if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2610 				  pddatype))
2611     return;
2612 
2613   /* The lower bound and element sizes must be constant.  */
2614   if (!TYPE_SIZE_UNIT (ddatype)
2615       || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2616       || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2617       || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2618     return;
2619 
2620   /* All checks succeeded.  Build a new node to merge the cast.  */
2621   *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2622 		    TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2623 		    NULL_TREE, NULL_TREE);
2624   *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2625 
2626   /* We can have stripped a required restrict qualifier above.  */
2627   if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2628     *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2629 }
2630 
2631 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR.  Remove it and/or other conversions
2632    underneath as appropriate.  */
2633 
2634 static enum gimplify_status
2635 gimplify_conversion (tree *expr_p)
2636 {
2637   location_t loc = EXPR_LOCATION (*expr_p);
2638   gcc_assert (CONVERT_EXPR_P (*expr_p));
2639 
2640   /* Then strip away all but the outermost conversion.  */
2641   STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2642 
2643   /* And remove the outermost conversion if it's useless.  */
2644   if (tree_ssa_useless_type_conversion (*expr_p))
2645     *expr_p = TREE_OPERAND (*expr_p, 0);
2646 
2647   /* If we still have a conversion at the toplevel,
2648      then canonicalize some constructs.  */
2649   if (CONVERT_EXPR_P (*expr_p))
2650     {
2651       tree sub = TREE_OPERAND (*expr_p, 0);
2652 
2653       /* If a NOP conversion is changing the type of a COMPONENT_REF
2654 	 expression, then canonicalize its type now in order to expose more
2655 	 redundant conversions.  */
2656       if (TREE_CODE (sub) == COMPONENT_REF)
2657 	canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2658 
2659       /* If a NOP conversion is changing a pointer to array of foo
2660 	 to a pointer to foo, embed that change in the ADDR_EXPR.  */
2661       else if (TREE_CODE (sub) == ADDR_EXPR)
2662 	canonicalize_addr_expr (expr_p);
2663     }
2664 
2665   /* If we have a conversion to a non-register type force the
2666      use of a VIEW_CONVERT_EXPR instead.  */
2667   if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2668     *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2669 			       TREE_OPERAND (*expr_p, 0));
2670 
2671   /* Canonicalize CONVERT_EXPR to NOP_EXPR.  */
2672   if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2673     TREE_SET_CODE (*expr_p, NOP_EXPR);
2674 
2675   return GS_OK;
2676 }
2677 
2678 /* Nonlocal VLAs seen in the current function.  */
2679 static hash_set<tree> *nonlocal_vlas;
2680 
2681 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes.  */
2682 static tree nonlocal_vla_vars;
2683 
2684 /* Gimplify a VAR_DECL or PARM_DECL.  Return GS_OK if we expanded a
2685    DECL_VALUE_EXPR, and it's worth re-examining things.  */
2686 
2687 static enum gimplify_status
2688 gimplify_var_or_parm_decl (tree *expr_p)
2689 {
2690   tree decl = *expr_p;
2691 
2692   /* ??? If this is a local variable, and it has not been seen in any
2693      outer BIND_EXPR, then it's probably the result of a duplicate
2694      declaration, for which we've already issued an error.  It would
2695      be really nice if the front end wouldn't leak these at all.
2696      Currently the only known culprit is C++ destructors, as seen
2697      in g++.old-deja/g++.jason/binding.C.  */
2698   if (VAR_P (decl)
2699       && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2700       && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2701       && decl_function_context (decl) == current_function_decl)
2702     {
2703       gcc_assert (seen_error ());
2704       return GS_ERROR;
2705     }
2706 
2707   /* When within an OMP context, notice uses of variables.  */
2708   if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2709     return GS_ALL_DONE;
2710 
2711   /* If the decl is an alias for another expression, substitute it now.  */
2712   if (DECL_HAS_VALUE_EXPR_P (decl))
2713     {
2714       tree value_expr = DECL_VALUE_EXPR (decl);
2715 
2716       /* For referenced nonlocal VLAs add a decl for debugging purposes
2717 	 to the current function.  */
2718       if (VAR_P (decl)
2719 	  && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2720 	  && nonlocal_vlas != NULL
2721 	  && TREE_CODE (value_expr) == INDIRECT_REF
2722 	  && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2723 	  && decl_function_context (decl) != current_function_decl)
2724 	{
2725 	  struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2726 	  while (ctx
2727 		 && (ctx->region_type == ORT_WORKSHARE
2728 		     || ctx->region_type == ORT_SIMD
2729 		     || ctx->region_type == ORT_ACC))
2730 	    ctx = ctx->outer_context;
2731 	  if (!ctx && !nonlocal_vlas->add (decl))
2732 	    {
2733 	      tree copy = copy_node (decl);
2734 
2735 	      lang_hooks.dup_lang_specific_decl (copy);
2736 	      SET_DECL_RTL (copy, 0);
2737 	      TREE_USED (copy) = 1;
2738 	      DECL_CHAIN (copy) = nonlocal_vla_vars;
2739 	      nonlocal_vla_vars = copy;
2740 	      SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2741 	      DECL_HAS_VALUE_EXPR_P (copy) = 1;
2742 	    }
2743 	}
2744 
2745       *expr_p = unshare_expr (value_expr);
2746       return GS_OK;
2747     }
2748 
2749   return GS_ALL_DONE;
2750 }
2751 
2752 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T.  */
2753 
2754 static void
2755 recalculate_side_effects (tree t)
2756 {
2757   enum tree_code code = TREE_CODE (t);
2758   int len = TREE_OPERAND_LENGTH (t);
2759   int i;
2760 
2761   switch (TREE_CODE_CLASS (code))
2762     {
2763     case tcc_expression:
2764       switch (code)
2765 	{
2766 	case INIT_EXPR:
2767 	case MODIFY_EXPR:
2768 	case VA_ARG_EXPR:
2769 	case PREDECREMENT_EXPR:
2770 	case PREINCREMENT_EXPR:
2771 	case POSTDECREMENT_EXPR:
2772 	case POSTINCREMENT_EXPR:
2773 	  /* All of these have side-effects, no matter what their
2774 	     operands are.  */
2775 	  return;
2776 
2777 	default:
2778 	  break;
2779 	}
2780       /* Fall through.  */
2781 
2782     case tcc_comparison:  /* a comparison expression */
2783     case tcc_unary:       /* a unary arithmetic expression */
2784     case tcc_binary:      /* a binary arithmetic expression */
2785     case tcc_reference:   /* a reference */
2786     case tcc_vl_exp:        /* a function call */
2787       TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2788       for (i = 0; i < len; ++i)
2789 	{
2790 	  tree op = TREE_OPERAND (t, i);
2791 	  if (op && TREE_SIDE_EFFECTS (op))
2792 	    TREE_SIDE_EFFECTS (t) = 1;
2793 	}
2794       break;
2795 
2796     case tcc_constant:
2797       /* No side-effects.  */
2798       return;
2799 
2800     default:
2801       gcc_unreachable ();
2802    }
2803 }
2804 
2805 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2806    node *EXPR_P.
2807 
2808       compound_lval
2809 	      : min_lval '[' val ']'
2810 	      | min_lval '.' ID
2811 	      | compound_lval '[' val ']'
2812 	      | compound_lval '.' ID
2813 
2814    This is not part of the original SIMPLE definition, which separates
2815    array and member references, but it seems reasonable to handle them
2816    together.  Also, this way we don't run into problems with union
2817    aliasing; gcc requires that for accesses through a union to alias, the
2818    union reference must be explicit, which was not always the case when we
2819    were splitting up array and member refs.
2820 
2821    PRE_P points to the sequence where side effects that must happen before
2822      *EXPR_P should be stored.
2823 
2824    POST_P points to the sequence where side effects that must happen after
2825      *EXPR_P should be stored.  */
2826 
2827 static enum gimplify_status
2828 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2829 			fallback_t fallback)
2830 {
2831   tree *p;
2832   enum gimplify_status ret = GS_ALL_DONE, tret;
2833   int i;
2834   location_t loc = EXPR_LOCATION (*expr_p);
2835   tree expr = *expr_p;
2836 
2837   /* Create a stack of the subexpressions so later we can walk them in
2838      order from inner to outer.  */
2839   auto_vec<tree, 10> expr_stack;
2840 
2841   /* We can handle anything that get_inner_reference can deal with.  */
2842   for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2843     {
2844     restart:
2845       /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs.  */
2846       if (TREE_CODE (*p) == INDIRECT_REF)
2847 	*p = fold_indirect_ref_loc (loc, *p);
2848 
2849       if (handled_component_p (*p))
2850 	;
2851       /* Expand DECL_VALUE_EXPR now.  In some cases that may expose
2852 	 additional COMPONENT_REFs.  */
2853       else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2854 	       && gimplify_var_or_parm_decl (p) == GS_OK)
2855 	goto restart;
2856       else
2857 	break;
2858 
2859       expr_stack.safe_push (*p);
2860     }
2861 
2862   gcc_assert (expr_stack.length ());
2863 
2864   /* Now EXPR_STACK is a stack of pointers to all the refs we've
2865      walked through and P points to the innermost expression.
2866 
2867      Java requires that we elaborated nodes in source order.  That
2868      means we must gimplify the inner expression followed by each of
2869      the indices, in order.  But we can't gimplify the inner
2870      expression until we deal with any variable bounds, sizes, or
2871      positions in order to deal with PLACEHOLDER_EXPRs.
2872 
2873      So we do this in three steps.  First we deal with the annotations
2874      for any variables in the components, then we gimplify the base,
2875      then we gimplify any indices, from left to right.  */
2876   for (i = expr_stack.length () - 1; i >= 0; i--)
2877     {
2878       tree t = expr_stack[i];
2879 
2880       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2881 	{
2882 	  /* Gimplify the low bound and element type size and put them into
2883 	     the ARRAY_REF.  If these values are set, they have already been
2884 	     gimplified.  */
2885 	  if (TREE_OPERAND (t, 2) == NULL_TREE)
2886 	    {
2887 	      tree low = unshare_expr (array_ref_low_bound (t));
2888 	      if (!is_gimple_min_invariant (low))
2889 		{
2890 		  TREE_OPERAND (t, 2) = low;
2891 		  tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2892 					post_p, is_gimple_reg,
2893 					fb_rvalue);
2894 		  ret = MIN (ret, tret);
2895 		}
2896 	    }
2897 	  else
2898 	    {
2899 	      tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2900 				    is_gimple_reg, fb_rvalue);
2901 	      ret = MIN (ret, tret);
2902 	    }
2903 
2904 	  if (TREE_OPERAND (t, 3) == NULL_TREE)
2905 	    {
2906 	      tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2907 	      tree elmt_size = unshare_expr (array_ref_element_size (t));
2908 	      tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2909 
2910 	      /* Divide the element size by the alignment of the element
2911 		 type (above).  */
2912 	      elmt_size
2913 		= size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2914 
2915 	      if (!is_gimple_min_invariant (elmt_size))
2916 		{
2917 		  TREE_OPERAND (t, 3) = elmt_size;
2918 		  tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2919 					post_p, is_gimple_reg,
2920 					fb_rvalue);
2921 		  ret = MIN (ret, tret);
2922 		}
2923 	    }
2924 	  else
2925 	    {
2926 	      tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2927 				    is_gimple_reg, fb_rvalue);
2928 	      ret = MIN (ret, tret);
2929 	    }
2930 	}
2931       else if (TREE_CODE (t) == COMPONENT_REF)
2932 	{
2933 	  /* Set the field offset into T and gimplify it.  */
2934 	  if (TREE_OPERAND (t, 2) == NULL_TREE)
2935 	    {
2936 	      tree offset = unshare_expr (component_ref_field_offset (t));
2937 	      tree field = TREE_OPERAND (t, 1);
2938 	      tree factor
2939 		= size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2940 
2941 	      /* Divide the offset by its alignment.  */
2942 	      offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2943 
2944 	      if (!is_gimple_min_invariant (offset))
2945 		{
2946 		  TREE_OPERAND (t, 2) = offset;
2947 		  tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2948 					post_p, is_gimple_reg,
2949 					fb_rvalue);
2950 		  ret = MIN (ret, tret);
2951 		}
2952 	    }
2953 	  else
2954 	    {
2955 	      tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2956 				    is_gimple_reg, fb_rvalue);
2957 	      ret = MIN (ret, tret);
2958 	    }
2959 	}
2960     }
2961 
2962   /* Step 2 is to gimplify the base expression.  Make sure lvalue is set
2963      so as to match the min_lval predicate.  Failure to do so may result
2964      in the creation of large aggregate temporaries.  */
2965   tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2966 			fallback | fb_lvalue);
2967   ret = MIN (ret, tret);
2968 
2969   /* And finally, the indices and operands of ARRAY_REF.  During this
2970      loop we also remove any useless conversions.  */
2971   for (; expr_stack.length () > 0; )
2972     {
2973       tree t = expr_stack.pop ();
2974 
2975       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2976 	{
2977 	  /* Gimplify the dimension.  */
2978 	  if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2979 	    {
2980 	      tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2981 				    is_gimple_val, fb_rvalue);
2982 	      ret = MIN (ret, tret);
2983 	    }
2984 	}
2985 
2986       STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2987 
2988       /* The innermost expression P may have originally had
2989 	 TREE_SIDE_EFFECTS set which would have caused all the outer
2990 	 expressions in *EXPR_P leading to P to also have had
2991 	 TREE_SIDE_EFFECTS set.  */
2992       recalculate_side_effects (t);
2993     }
2994 
2995   /* If the outermost expression is a COMPONENT_REF, canonicalize its type.  */
2996   if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2997     {
2998       canonicalize_component_ref (expr_p);
2999     }
3000 
3001   expr_stack.release ();
3002 
3003   gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3004 
3005   return ret;
3006 }
3007 
3008 /*  Gimplify the self modifying expression pointed to by EXPR_P
3009     (++, --, +=, -=).
3010 
3011     PRE_P points to the list where side effects that must happen before
3012 	*EXPR_P should be stored.
3013 
3014     POST_P points to the list where side effects that must happen after
3015 	*EXPR_P should be stored.
3016 
3017     WANT_VALUE is nonzero iff we want to use the value of this expression
3018 	in another expression.
3019 
3020     ARITH_TYPE is the type the computation should be performed in.  */
3021 
3022 enum gimplify_status
3023 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3024 			bool want_value, tree arith_type)
3025 {
3026   enum tree_code code;
3027   tree lhs, lvalue, rhs, t1;
3028   gimple_seq post = NULL, *orig_post_p = post_p;
3029   bool postfix;
3030   enum tree_code arith_code;
3031   enum gimplify_status ret;
3032   location_t loc = EXPR_LOCATION (*expr_p);
3033 
3034   code = TREE_CODE (*expr_p);
3035 
3036   gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3037 	      || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3038 
3039   /* Prefix or postfix?  */
3040   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3041     /* Faster to treat as prefix if result is not used.  */
3042     postfix = want_value;
3043   else
3044     postfix = false;
3045 
3046   /* For postfix, make sure the inner expression's post side effects
3047      are executed after side effects from this expression.  */
3048   if (postfix)
3049     post_p = &post;
3050 
3051   /* Add or subtract?  */
3052   if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3053     arith_code = PLUS_EXPR;
3054   else
3055     arith_code = MINUS_EXPR;
3056 
3057   /* Gimplify the LHS into a GIMPLE lvalue.  */
3058   lvalue = TREE_OPERAND (*expr_p, 0);
3059   ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3060   if (ret == GS_ERROR)
3061     return ret;
3062 
3063   /* Extract the operands to the arithmetic operation.  */
3064   lhs = lvalue;
3065   rhs = TREE_OPERAND (*expr_p, 1);
3066 
3067   /* For postfix operator, we evaluate the LHS to an rvalue and then use
3068      that as the result value and in the postqueue operation.  */
3069   if (postfix)
3070     {
3071       ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3072       if (ret == GS_ERROR)
3073 	return ret;
3074 
3075       lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
3076     }
3077 
3078   /* For POINTERs increment, use POINTER_PLUS_EXPR.  */
3079   if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3080     {
3081       rhs = convert_to_ptrofftype_loc (loc, rhs);
3082       if (arith_code == MINUS_EXPR)
3083 	rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3084       t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3085     }
3086   else
3087     t1 = fold_convert (TREE_TYPE (*expr_p),
3088 		       fold_build2 (arith_code, arith_type,
3089 				    fold_convert (arith_type, lhs),
3090 				    fold_convert (arith_type, rhs)));
3091 
3092   if (postfix)
3093     {
3094       gimplify_assign (lvalue, t1, pre_p);
3095       gimplify_seq_add_seq (orig_post_p, post);
3096       *expr_p = lhs;
3097       return GS_ALL_DONE;
3098     }
3099   else
3100     {
3101       *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3102       return GS_OK;
3103     }
3104 }
3105 
3106 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR.  */
3107 
3108 static void
3109 maybe_with_size_expr (tree *expr_p)
3110 {
3111   tree expr = *expr_p;
3112   tree type = TREE_TYPE (expr);
3113   tree size;
3114 
3115   /* If we've already wrapped this or the type is error_mark_node, we can't do
3116      anything.  */
3117   if (TREE_CODE (expr) == WITH_SIZE_EXPR
3118       || type == error_mark_node)
3119     return;
3120 
3121   /* If the size isn't known or is a constant, we have nothing to do.  */
3122   size = TYPE_SIZE_UNIT (type);
3123   if (!size || poly_int_tree_p (size))
3124     return;
3125 
3126   /* Otherwise, make a WITH_SIZE_EXPR.  */
3127   size = unshare_expr (size);
3128   size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3129   *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3130 }
3131 
3132 /* Helper for gimplify_call_expr.  Gimplify a single argument *ARG_P
3133    Store any side-effects in PRE_P.  CALL_LOCATION is the location of
3134    the CALL_EXPR.  If ALLOW_SSA is set the actual parameter may be
3135    gimplified to an SSA name.  */
3136 
3137 enum gimplify_status
3138 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3139 	      bool allow_ssa)
3140 {
3141   bool (*test) (tree);
3142   fallback_t fb;
3143 
3144   /* In general, we allow lvalues for function arguments to avoid
3145      extra overhead of copying large aggregates out of even larger
3146      aggregates into temporaries only to copy the temporaries to
3147      the argument list.  Make optimizers happy by pulling out to
3148      temporaries those types that fit in registers.  */
3149   if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3150     test = is_gimple_val, fb = fb_rvalue;
3151   else
3152     {
3153       test = is_gimple_lvalue, fb = fb_either;
3154       /* Also strip a TARGET_EXPR that would force an extra copy.  */
3155       if (TREE_CODE (*arg_p) == TARGET_EXPR)
3156 	{
3157 	  tree init = TARGET_EXPR_INITIAL (*arg_p);
3158 	  if (init
3159 	      && !VOID_TYPE_P (TREE_TYPE (init)))
3160 	    *arg_p = init;
3161 	}
3162     }
3163 
3164   /* If this is a variable sized type, we must remember the size.  */
3165   maybe_with_size_expr (arg_p);
3166 
3167   /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c.  */
3168   /* Make sure arguments have the same location as the function call
3169      itself.  */
3170   protected_set_expr_location (*arg_p, call_location);
3171 
3172   /* There is a sequence point before a function call.  Side effects in
3173      the argument list must occur before the actual call. So, when
3174      gimplifying arguments, force gimplify_expr to use an internal
3175      post queue which is then appended to the end of PRE_P.  */
3176   return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3177 }
3178 
3179 /* Don't fold inside offloading or taskreg regions: it can break code by
3180    adding decl references that weren't in the source.  We'll do it during
3181    omplower pass instead.  */
3182 
3183 static bool
3184 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3185 {
3186   struct gimplify_omp_ctx *ctx;
3187   for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3188     if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3189       return false;
3190   return fold_stmt (gsi);
3191 }
3192 
3193 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3194    WANT_VALUE is true if the result of the call is desired.  */
3195 
3196 static enum gimplify_status
3197 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3198 {
3199   tree fndecl, parms, p, fnptrtype;
3200   enum gimplify_status ret;
3201   int i, nargs;
3202   gcall *call;
3203   bool builtin_va_start_p = false;
3204   location_t loc = EXPR_LOCATION (*expr_p);
3205 
3206   gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3207 
3208   /* For reliable diagnostics during inlining, it is necessary that
3209      every call_expr be annotated with file and line.  */
3210   if (! EXPR_HAS_LOCATION (*expr_p))
3211     SET_EXPR_LOCATION (*expr_p, input_location);
3212 
3213   /* Gimplify internal functions created in the FEs.  */
3214   if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3215     {
3216       if (want_value)
3217 	return GS_ALL_DONE;
3218 
3219       nargs = call_expr_nargs (*expr_p);
3220       enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3221       auto_vec<tree> vargs (nargs);
3222 
3223       for (i = 0; i < nargs; i++)
3224 	{
3225 	  gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3226 			EXPR_LOCATION (*expr_p));
3227 	  vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3228 	}
3229 
3230       gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3231       gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
3232       gimplify_seq_add_stmt (pre_p, call);
3233       return GS_ALL_DONE;
3234     }
3235 
3236   /* This may be a call to a builtin function.
3237 
3238      Builtin function calls may be transformed into different
3239      (and more efficient) builtin function calls under certain
3240      circumstances.  Unfortunately, gimplification can muck things
3241      up enough that the builtin expanders are not aware that certain
3242      transformations are still valid.
3243 
3244      So we attempt transformation/gimplification of the call before
3245      we gimplify the CALL_EXPR.  At this time we do not manage to
3246      transform all calls in the same manner as the expanders do, but
3247      we do transform most of them.  */
3248   fndecl = get_callee_fndecl (*expr_p);
3249   if (fndecl
3250       && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3251     switch (DECL_FUNCTION_CODE (fndecl))
3252       {
3253       CASE_BUILT_IN_ALLOCA:
3254 	/* If the call has been built for a variable-sized object, then we
3255 	   want to restore the stack level when the enclosing BIND_EXPR is
3256 	   exited to reclaim the allocated space; otherwise, we precisely
3257 	   need to do the opposite and preserve the latest stack level.  */
3258 	if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
3259 	  gimplify_ctxp->save_stack = true;
3260 	else
3261 	  gimplify_ctxp->keep_stack = true;
3262 	break;
3263 
3264       case BUILT_IN_VA_START:
3265         {
3266 	  builtin_va_start_p = TRUE;
3267 	  if (call_expr_nargs (*expr_p) < 2)
3268 	    {
3269 	      error ("too few arguments to function %<va_start%>");
3270 	      *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3271 	      return GS_OK;
3272 	    }
3273 
3274 	  if (fold_builtin_next_arg (*expr_p, true))
3275 	    {
3276 	      *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3277 	      return GS_OK;
3278 	    }
3279 	  break;
3280 	}
3281 
3282       default:
3283         ;
3284       }
3285   if (fndecl && DECL_BUILT_IN (fndecl))
3286     {
3287       tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3288       if (new_tree && new_tree != *expr_p)
3289 	{
3290 	  /* There was a transformation of this call which computes the
3291 	     same value, but in a more efficient way.  Return and try
3292 	     again.  */
3293 	  *expr_p = new_tree;
3294 	  return GS_OK;
3295 	}
3296     }
3297 
3298   /* Remember the original function pointer type.  */
3299   fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
3300 
3301   /* There is a sequence point before the call, so any side effects in
3302      the calling expression must occur before the actual call.  Force
3303      gimplify_expr to use an internal post queue.  */
3304   ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
3305 		       is_gimple_call_addr, fb_rvalue);
3306 
3307   nargs = call_expr_nargs (*expr_p);
3308 
3309   /* Get argument types for verification.  */
3310   fndecl = get_callee_fndecl (*expr_p);
3311   parms = NULL_TREE;
3312   if (fndecl)
3313     parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3314   else
3315     parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3316 
3317   if (fndecl && DECL_ARGUMENTS (fndecl))
3318     p = DECL_ARGUMENTS (fndecl);
3319   else if (parms)
3320     p = parms;
3321   else
3322     p = NULL_TREE;
3323   for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3324     ;
3325 
3326   /* If the last argument is __builtin_va_arg_pack () and it is not
3327      passed as a named argument, decrease the number of CALL_EXPR
3328      arguments and set instead the CALL_EXPR_VA_ARG_PACK flag.  */
3329   if (!p
3330       && i < nargs
3331       && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3332     {
3333       tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3334       tree last_arg_fndecl = get_callee_fndecl (last_arg);
3335 
3336       if (last_arg_fndecl
3337 	  && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
3338 	  && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
3339 	  && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
3340 	{
3341 	  tree call = *expr_p;
3342 
3343 	  --nargs;
3344 	  *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3345 					  CALL_EXPR_FN (call),
3346 					  nargs, CALL_EXPR_ARGP (call));
3347 
3348 	  /* Copy all CALL_EXPR flags, location and block, except
3349 	     CALL_EXPR_VA_ARG_PACK flag.  */
3350 	  CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3351 	  CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3352 	  CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3353 	    = CALL_EXPR_RETURN_SLOT_OPT (call);
3354 	  CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3355 	  SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3356 
3357 	  /* Set CALL_EXPR_VA_ARG_PACK.  */
3358 	  CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3359 	}
3360     }
3361 
3362   /* If the call returns twice then after building the CFG the call
3363      argument computations will no longer dominate the call because
3364      we add an abnormal incoming edge to the call.  So do not use SSA
3365      vars there.  */
3366   bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3367 
3368   /* Gimplify the function arguments.  */
3369   if (nargs > 0)
3370     {
3371       for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3372            PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3373            PUSH_ARGS_REVERSED ? i-- : i++)
3374         {
3375           enum gimplify_status t;
3376 
3377           /* Avoid gimplifying the second argument to va_start, which needs to
3378              be the plain PARM_DECL.  */
3379           if ((i != 1) || !builtin_va_start_p)
3380             {
3381               t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3382 				EXPR_LOCATION (*expr_p), ! returns_twice);
3383 
3384               if (t == GS_ERROR)
3385                 ret = GS_ERROR;
3386             }
3387         }
3388     }
3389 
3390   /* Gimplify the static chain.  */
3391   if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3392     {
3393       if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3394 	CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3395       else
3396 	{
3397 	  enum gimplify_status t;
3398 	  t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3399 			    EXPR_LOCATION (*expr_p), ! returns_twice);
3400 	  if (t == GS_ERROR)
3401 	    ret = GS_ERROR;
3402 	}
3403     }
3404 
3405   /* Verify the function result.  */
3406   if (want_value && fndecl
3407       && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3408     {
3409       error_at (loc, "using result of function returning %<void%>");
3410       ret = GS_ERROR;
3411     }
3412 
3413   /* Try this again in case gimplification exposed something.  */
3414   if (ret != GS_ERROR)
3415     {
3416       tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3417 
3418       if (new_tree && new_tree != *expr_p)
3419 	{
3420 	  /* There was a transformation of this call which computes the
3421 	     same value, but in a more efficient way.  Return and try
3422 	     again.  */
3423 	  *expr_p = new_tree;
3424 	  return GS_OK;
3425 	}
3426     }
3427   else
3428     {
3429       *expr_p = error_mark_node;
3430       return GS_ERROR;
3431     }
3432 
3433   /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3434      decl.  This allows us to eliminate redundant or useless
3435      calls to "const" functions.  */
3436   if (TREE_CODE (*expr_p) == CALL_EXPR)
3437     {
3438       int flags = call_expr_flags (*expr_p);
3439       if (flags & (ECF_CONST | ECF_PURE)
3440 	  /* An infinite loop is considered a side effect.  */
3441 	  && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3442 	TREE_SIDE_EFFECTS (*expr_p) = 0;
3443     }
3444 
3445   /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3446      and clear *EXPR_P.  Otherwise, leave *EXPR_P in its gimplified
3447      form and delegate the creation of a GIMPLE_CALL to
3448      gimplify_modify_expr.  This is always possible because when
3449      WANT_VALUE is true, the caller wants the result of this call into
3450      a temporary, which means that we will emit an INIT_EXPR in
3451      internal_get_tmp_var which will then be handled by
3452      gimplify_modify_expr.  */
3453   if (!want_value)
3454     {
3455       /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3456 	 have to do is replicate it as a GIMPLE_CALL tuple.  */
3457       gimple_stmt_iterator gsi;
3458       call = gimple_build_call_from_tree (*expr_p, fnptrtype);
3459       notice_special_calls (call);
3460       gimplify_seq_add_stmt (pre_p, call);
3461       gsi = gsi_last (*pre_p);
3462       maybe_fold_stmt (&gsi);
3463       *expr_p = NULL_TREE;
3464     }
3465   else
3466     /* Remember the original function type.  */
3467     CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3468 				     CALL_EXPR_FN (*expr_p));
3469 
3470   return ret;
3471 }
3472 
3473 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3474    rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3475 
3476    TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3477    condition is true or false, respectively.  If null, we should generate
3478    our own to skip over the evaluation of this specific expression.
3479 
3480    LOCUS is the source location of the COND_EXPR.
3481 
3482    This function is the tree equivalent of do_jump.
3483 
3484    shortcut_cond_r should only be called by shortcut_cond_expr.  */
3485 
3486 static tree
3487 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3488 		 location_t locus)
3489 {
3490   tree local_label = NULL_TREE;
3491   tree t, expr = NULL;
3492 
3493   /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3494      retain the shortcut semantics.  Just insert the gotos here;
3495      shortcut_cond_expr will append the real blocks later.  */
3496   if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3497     {
3498       location_t new_locus;
3499 
3500       /* Turn if (a && b) into
3501 
3502 	 if (a); else goto no;
3503 	 if (b) goto yes; else goto no;
3504 	 (no:) */
3505 
3506       if (false_label_p == NULL)
3507 	false_label_p = &local_label;
3508 
3509       /* Keep the original source location on the first 'if'.  */
3510       t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3511       append_to_statement_list (t, &expr);
3512 
3513       /* Set the source location of the && on the second 'if'.  */
3514       new_locus = rexpr_location (pred, locus);
3515       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3516 			   new_locus);
3517       append_to_statement_list (t, &expr);
3518     }
3519   else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3520     {
3521       location_t new_locus;
3522 
3523       /* Turn if (a || b) into
3524 
3525 	 if (a) goto yes;
3526 	 if (b) goto yes; else goto no;
3527 	 (yes:) */
3528 
3529       if (true_label_p == NULL)
3530 	true_label_p = &local_label;
3531 
3532       /* Keep the original source location on the first 'if'.  */
3533       t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3534       append_to_statement_list (t, &expr);
3535 
3536       /* Set the source location of the || on the second 'if'.  */
3537       new_locus = rexpr_location (pred, locus);
3538       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3539 			   new_locus);
3540       append_to_statement_list (t, &expr);
3541     }
3542   else if (TREE_CODE (pred) == COND_EXPR
3543 	   && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3544 	   && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3545     {
3546       location_t new_locus;
3547 
3548       /* As long as we're messing with gotos, turn if (a ? b : c) into
3549 	 if (a)
3550 	   if (b) goto yes; else goto no;
3551 	 else
3552 	   if (c) goto yes; else goto no;
3553 
3554 	 Don't do this if one of the arms has void type, which can happen
3555 	 in C++ when the arm is throw.  */
3556 
3557       /* Keep the original source location on the first 'if'.  Set the source
3558 	 location of the ? on the second 'if'.  */
3559       new_locus = rexpr_location (pred, locus);
3560       expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3561 		     shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3562 				      false_label_p, locus),
3563 		     shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3564 				      false_label_p, new_locus));
3565     }
3566   else
3567     {
3568       expr = build3 (COND_EXPR, void_type_node, pred,
3569 		     build_and_jump (true_label_p),
3570 		     build_and_jump (false_label_p));
3571       SET_EXPR_LOCATION (expr, locus);
3572     }
3573 
3574   if (local_label)
3575     {
3576       t = build1 (LABEL_EXPR, void_type_node, local_label);
3577       append_to_statement_list (t, &expr);
3578     }
3579 
3580   return expr;
3581 }
3582 
3583 /* If EXPR is a GOTO_EXPR, return it.  If it is a STATEMENT_LIST, skip
3584    any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
3585    statement, if it is the last one.  Otherwise, return NULL.  */
3586 
3587 static tree
3588 find_goto (tree expr)
3589 {
3590   if (!expr)
3591     return NULL_TREE;
3592 
3593   if (TREE_CODE (expr) == GOTO_EXPR)
3594     return expr;
3595 
3596   if (TREE_CODE (expr) != STATEMENT_LIST)
3597     return NULL_TREE;
3598 
3599   tree_stmt_iterator i = tsi_start (expr);
3600 
3601   while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
3602     tsi_next (&i);
3603 
3604   if (!tsi_one_before_end_p (i))
3605     return NULL_TREE;
3606 
3607   return find_goto (tsi_stmt (i));
3608 }
3609 
3610 /* Same as find_goto, except that it returns NULL if the destination
3611    is not a LABEL_DECL.  */
3612 
3613 static inline tree
3614 find_goto_label (tree expr)
3615 {
3616   tree dest = find_goto (expr);
3617   if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
3618     return dest;
3619   return NULL_TREE;
3620 }
3621 
3622 /* Given a conditional expression EXPR with short-circuit boolean
3623    predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3624    predicate apart into the equivalent sequence of conditionals.  */
3625 
3626 static tree
3627 shortcut_cond_expr (tree expr)
3628 {
3629   tree pred = TREE_OPERAND (expr, 0);
3630   tree then_ = TREE_OPERAND (expr, 1);
3631   tree else_ = TREE_OPERAND (expr, 2);
3632   tree true_label, false_label, end_label, t;
3633   tree *true_label_p;
3634   tree *false_label_p;
3635   bool emit_end, emit_false, jump_over_else;
3636   bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3637   bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3638 
3639   /* First do simple transformations.  */
3640   if (!else_se)
3641     {
3642       /* If there is no 'else', turn
3643 	   if (a && b) then c
3644 	 into
3645 	   if (a) if (b) then c.  */
3646       while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3647 	{
3648 	  /* Keep the original source location on the first 'if'.  */
3649 	  location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3650 	  TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3651 	  /* Set the source location of the && on the second 'if'.  */
3652 	  if (rexpr_has_location (pred))
3653 	    SET_EXPR_LOCATION (expr, rexpr_location (pred));
3654 	  then_ = shortcut_cond_expr (expr);
3655 	  then_se = then_ && TREE_SIDE_EFFECTS (then_);
3656 	  pred = TREE_OPERAND (pred, 0);
3657 	  expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3658 	  SET_EXPR_LOCATION (expr, locus);
3659 	}
3660     }
3661 
3662   if (!then_se)
3663     {
3664       /* If there is no 'then', turn
3665 	   if (a || b); else d
3666 	 into
3667 	   if (a); else if (b); else d.  */
3668       while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3669 	{
3670 	  /* Keep the original source location on the first 'if'.  */
3671 	  location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3672 	  TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3673 	  /* Set the source location of the || on the second 'if'.  */
3674 	  if (rexpr_has_location (pred))
3675 	    SET_EXPR_LOCATION (expr, rexpr_location (pred));
3676 	  else_ = shortcut_cond_expr (expr);
3677 	  else_se = else_ && TREE_SIDE_EFFECTS (else_);
3678 	  pred = TREE_OPERAND (pred, 0);
3679 	  expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3680 	  SET_EXPR_LOCATION (expr, locus);
3681 	}
3682     }
3683 
3684   /* If we're done, great.  */
3685   if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3686       && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3687     return expr;
3688 
3689   /* Otherwise we need to mess with gotos.  Change
3690        if (a) c; else d;
3691      to
3692        if (a); else goto no;
3693        c; goto end;
3694        no: d; end:
3695      and recursively gimplify the condition.  */
3696 
3697   true_label = false_label = end_label = NULL_TREE;
3698 
3699   /* If our arms just jump somewhere, hijack those labels so we don't
3700      generate jumps to jumps.  */
3701 
3702   if (tree then_goto = find_goto_label (then_))
3703     {
3704       true_label = GOTO_DESTINATION (then_goto);
3705       then_ = NULL;
3706       then_se = false;
3707     }
3708 
3709   if (tree else_goto = find_goto_label (else_))
3710     {
3711       false_label = GOTO_DESTINATION (else_goto);
3712       else_ = NULL;
3713       else_se = false;
3714     }
3715 
3716   /* If we aren't hijacking a label for the 'then' branch, it falls through.  */
3717   if (true_label)
3718     true_label_p = &true_label;
3719   else
3720     true_label_p = NULL;
3721 
3722   /* The 'else' branch also needs a label if it contains interesting code.  */
3723   if (false_label || else_se)
3724     false_label_p = &false_label;
3725   else
3726     false_label_p = NULL;
3727 
3728   /* If there was nothing else in our arms, just forward the label(s).  */
3729   if (!then_se && !else_se)
3730     return shortcut_cond_r (pred, true_label_p, false_label_p,
3731 			    EXPR_LOC_OR_LOC (expr, input_location));
3732 
3733   /* If our last subexpression already has a terminal label, reuse it.  */
3734   if (else_se)
3735     t = expr_last (else_);
3736   else if (then_se)
3737     t = expr_last (then_);
3738   else
3739     t = NULL;
3740   if (t && TREE_CODE (t) == LABEL_EXPR)
3741     end_label = LABEL_EXPR_LABEL (t);
3742 
3743   /* If we don't care about jumping to the 'else' branch, jump to the end
3744      if the condition is false.  */
3745   if (!false_label_p)
3746     false_label_p = &end_label;
3747 
3748   /* We only want to emit these labels if we aren't hijacking them.  */
3749   emit_end = (end_label == NULL_TREE);
3750   emit_false = (false_label == NULL_TREE);
3751 
3752   /* We only emit the jump over the else clause if we have to--if the
3753      then clause may fall through.  Otherwise we can wind up with a
3754      useless jump and a useless label at the end of gimplified code,
3755      which will cause us to think that this conditional as a whole
3756      falls through even if it doesn't.  If we then inline a function
3757      which ends with such a condition, that can cause us to issue an
3758      inappropriate warning about control reaching the end of a
3759      non-void function.  */
3760   jump_over_else = block_may_fallthru (then_);
3761 
3762   pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3763 			  EXPR_LOC_OR_LOC (expr, input_location));
3764 
3765   expr = NULL;
3766   append_to_statement_list (pred, &expr);
3767 
3768   append_to_statement_list (then_, &expr);
3769   if (else_se)
3770     {
3771       if (jump_over_else)
3772 	{
3773 	  tree last = expr_last (expr);
3774 	  t = build_and_jump (&end_label);
3775 	  if (rexpr_has_location (last))
3776 	    SET_EXPR_LOCATION (t, rexpr_location (last));
3777 	  append_to_statement_list (t, &expr);
3778 	}
3779       if (emit_false)
3780 	{
3781 	  t = build1 (LABEL_EXPR, void_type_node, false_label);
3782 	  append_to_statement_list (t, &expr);
3783 	}
3784       append_to_statement_list (else_, &expr);
3785     }
3786   if (emit_end && end_label)
3787     {
3788       t = build1 (LABEL_EXPR, void_type_node, end_label);
3789       append_to_statement_list (t, &expr);
3790     }
3791 
3792   return expr;
3793 }
3794 
3795 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE.  */
3796 
3797 tree
3798 gimple_boolify (tree expr)
3799 {
3800   tree type = TREE_TYPE (expr);
3801   location_t loc = EXPR_LOCATION (expr);
3802 
3803   if (TREE_CODE (expr) == NE_EXPR
3804       && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3805       && integer_zerop (TREE_OPERAND (expr, 1)))
3806     {
3807       tree call = TREE_OPERAND (expr, 0);
3808       tree fn = get_callee_fndecl (call);
3809 
3810       /* For __builtin_expect ((long) (x), y) recurse into x as well
3811 	 if x is truth_value_p.  */
3812       if (fn
3813 	  && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3814 	  && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3815 	  && call_expr_nargs (call) == 2)
3816 	{
3817 	  tree arg = CALL_EXPR_ARG (call, 0);
3818 	  if (arg)
3819 	    {
3820 	      if (TREE_CODE (arg) == NOP_EXPR
3821 		  && TREE_TYPE (arg) == TREE_TYPE (call))
3822 		arg = TREE_OPERAND (arg, 0);
3823 	      if (truth_value_p (TREE_CODE (arg)))
3824 		{
3825 		  arg = gimple_boolify (arg);
3826 		  CALL_EXPR_ARG (call, 0)
3827 		    = fold_convert_loc (loc, TREE_TYPE (call), arg);
3828 		}
3829 	    }
3830 	}
3831     }
3832 
3833   switch (TREE_CODE (expr))
3834     {
3835     case TRUTH_AND_EXPR:
3836     case TRUTH_OR_EXPR:
3837     case TRUTH_XOR_EXPR:
3838     case TRUTH_ANDIF_EXPR:
3839     case TRUTH_ORIF_EXPR:
3840       /* Also boolify the arguments of truth exprs.  */
3841       TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3842       /* FALLTHRU */
3843 
3844     case TRUTH_NOT_EXPR:
3845       TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3846 
3847       /* These expressions always produce boolean results.  */
3848       if (TREE_CODE (type) != BOOLEAN_TYPE)
3849 	TREE_TYPE (expr) = boolean_type_node;
3850       return expr;
3851 
3852     case ANNOTATE_EXPR:
3853       switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3854 	{
3855 	case annot_expr_ivdep_kind:
3856 	case annot_expr_unroll_kind:
3857 	case annot_expr_no_vector_kind:
3858 	case annot_expr_vector_kind:
3859 	case annot_expr_parallel_kind:
3860 	  TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3861 	  if (TREE_CODE (type) != BOOLEAN_TYPE)
3862 	    TREE_TYPE (expr) = boolean_type_node;
3863 	  return expr;
3864 	default:
3865 	  gcc_unreachable ();
3866 	}
3867 
3868     default:
3869       if (COMPARISON_CLASS_P (expr))
3870 	{
3871 	  /* There expressions always prduce boolean results.  */
3872 	  if (TREE_CODE (type) != BOOLEAN_TYPE)
3873 	    TREE_TYPE (expr) = boolean_type_node;
3874 	  return expr;
3875 	}
3876       /* Other expressions that get here must have boolean values, but
3877 	 might need to be converted to the appropriate mode.  */
3878       if (TREE_CODE (type) == BOOLEAN_TYPE)
3879 	return expr;
3880       return fold_convert_loc (loc, boolean_type_node, expr);
3881     }
3882 }
3883 
3884 /* Given a conditional expression *EXPR_P without side effects, gimplify
3885    its operands.  New statements are inserted to PRE_P.  */
3886 
3887 static enum gimplify_status
3888 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3889 {
3890   tree expr = *expr_p, cond;
3891   enum gimplify_status ret, tret;
3892   enum tree_code code;
3893 
3894   cond = gimple_boolify (COND_EXPR_COND (expr));
3895 
3896   /* We need to handle && and || specially, as their gimplification
3897      creates pure cond_expr, thus leading to an infinite cycle otherwise.  */
3898   code = TREE_CODE (cond);
3899   if (code == TRUTH_ANDIF_EXPR)
3900     TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3901   else if (code == TRUTH_ORIF_EXPR)
3902     TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3903   ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3904   COND_EXPR_COND (*expr_p) = cond;
3905 
3906   tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3907 				   is_gimple_val, fb_rvalue);
3908   ret = MIN (ret, tret);
3909   tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3910 				   is_gimple_val, fb_rvalue);
3911 
3912   return MIN (ret, tret);
3913 }
3914 
3915 /* Return true if evaluating EXPR could trap.
3916    EXPR is GENERIC, while tree_could_trap_p can be called
3917    only on GIMPLE.  */
3918 
3919 static bool
3920 generic_expr_could_trap_p (tree expr)
3921 {
3922   unsigned i, n;
3923 
3924   if (!expr || is_gimple_val (expr))
3925     return false;
3926 
3927   if (!EXPR_P (expr) || tree_could_trap_p (expr))
3928     return true;
3929 
3930   n = TREE_OPERAND_LENGTH (expr);
3931   for (i = 0; i < n; i++)
3932     if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3933       return true;
3934 
3935   return false;
3936 }
3937 
3938 /*  Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3939     into
3940 
3941     if (p)			if (p)
3942       t1 = a;			  a;
3943     else		or	else
3944       t1 = b;			  b;
3945     t1;
3946 
3947     The second form is used when *EXPR_P is of type void.
3948 
3949     PRE_P points to the list where side effects that must happen before
3950       *EXPR_P should be stored.  */
3951 
3952 static enum gimplify_status
3953 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3954 {
3955   tree expr = *expr_p;
3956   tree type = TREE_TYPE (expr);
3957   location_t loc = EXPR_LOCATION (expr);
3958   tree tmp, arm1, arm2;
3959   enum gimplify_status ret;
3960   tree label_true, label_false, label_cont;
3961   bool have_then_clause_p, have_else_clause_p;
3962   gcond *cond_stmt;
3963   enum tree_code pred_code;
3964   gimple_seq seq = NULL;
3965 
3966   /* If this COND_EXPR has a value, copy the values into a temporary within
3967      the arms.  */
3968   if (!VOID_TYPE_P (type))
3969     {
3970       tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3971       tree result;
3972 
3973       /* If either an rvalue is ok or we do not require an lvalue, create the
3974 	 temporary.  But we cannot do that if the type is addressable.  */
3975       if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3976 	  && !TREE_ADDRESSABLE (type))
3977 	{
3978 	  if (gimplify_ctxp->allow_rhs_cond_expr
3979 	      /* If either branch has side effects or could trap, it can't be
3980 		 evaluated unconditionally.  */
3981 	      && !TREE_SIDE_EFFECTS (then_)
3982 	      && !generic_expr_could_trap_p (then_)
3983 	      && !TREE_SIDE_EFFECTS (else_)
3984 	      && !generic_expr_could_trap_p (else_))
3985 	    return gimplify_pure_cond_expr (expr_p, pre_p);
3986 
3987 	  tmp = create_tmp_var (type, "iftmp");
3988 	  result = tmp;
3989 	}
3990 
3991       /* Otherwise, only create and copy references to the values.  */
3992       else
3993 	{
3994 	  type = build_pointer_type (type);
3995 
3996 	  if (!VOID_TYPE_P (TREE_TYPE (then_)))
3997 	    then_ = build_fold_addr_expr_loc (loc, then_);
3998 
3999 	  if (!VOID_TYPE_P (TREE_TYPE (else_)))
4000 	    else_ = build_fold_addr_expr_loc (loc, else_);
4001 
4002 	  expr
4003 	    = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
4004 
4005 	  tmp = create_tmp_var (type, "iftmp");
4006 	  result = build_simple_mem_ref_loc (loc, tmp);
4007 	}
4008 
4009       /* Build the new then clause, `tmp = then_;'.  But don't build the
4010 	 assignment if the value is void; in C++ it can be if it's a throw.  */
4011       if (!VOID_TYPE_P (TREE_TYPE (then_)))
4012 	TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
4013 
4014       /* Similarly, build the new else clause, `tmp = else_;'.  */
4015       if (!VOID_TYPE_P (TREE_TYPE (else_)))
4016 	TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
4017 
4018       TREE_TYPE (expr) = void_type_node;
4019       recalculate_side_effects (expr);
4020 
4021       /* Move the COND_EXPR to the prequeue.  */
4022       gimplify_stmt (&expr, pre_p);
4023 
4024       *expr_p = result;
4025       return GS_ALL_DONE;
4026     }
4027 
4028   /* Remove any COMPOUND_EXPR so the following cases will be caught.  */
4029   STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
4030   if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
4031     gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
4032 
4033   /* Make sure the condition has BOOLEAN_TYPE.  */
4034   TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
4035 
4036   /* Break apart && and || conditions.  */
4037   if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
4038       || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
4039     {
4040       expr = shortcut_cond_expr (expr);
4041 
4042       if (expr != *expr_p)
4043 	{
4044 	  *expr_p = expr;
4045 
4046 	  /* We can't rely on gimplify_expr to re-gimplify the expanded
4047 	     form properly, as cleanups might cause the target labels to be
4048 	     wrapped in a TRY_FINALLY_EXPR.  To prevent that, we need to
4049 	     set up a conditional context.  */
4050 	  gimple_push_condition ();
4051 	  gimplify_stmt (expr_p, &seq);
4052 	  gimple_pop_condition (pre_p);
4053 	  gimple_seq_add_seq (pre_p, seq);
4054 
4055 	  return GS_ALL_DONE;
4056 	}
4057     }
4058 
4059   /* Now do the normal gimplification.  */
4060 
4061   /* Gimplify condition.  */
4062   ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
4063 		       fb_rvalue);
4064   if (ret == GS_ERROR)
4065     return GS_ERROR;
4066   gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
4067 
4068   gimple_push_condition ();
4069 
4070   have_then_clause_p = have_else_clause_p = false;
4071   label_true = find_goto_label (TREE_OPERAND (expr, 1));
4072   if (label_true
4073       && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
4074       /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4075 	 have different locations, otherwise we end up with incorrect
4076 	 location information on the branches.  */
4077       && (optimize
4078 	  || !EXPR_HAS_LOCATION (expr)
4079 	  || !rexpr_has_location (label_true)
4080 	  || EXPR_LOCATION (expr) == rexpr_location (label_true)))
4081     {
4082       have_then_clause_p = true;
4083       label_true = GOTO_DESTINATION (label_true);
4084     }
4085   else
4086     label_true = create_artificial_label (UNKNOWN_LOCATION);
4087   label_false = find_goto_label (TREE_OPERAND (expr, 2));
4088   if (label_false
4089       && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
4090       /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4091 	 have different locations, otherwise we end up with incorrect
4092 	 location information on the branches.  */
4093       && (optimize
4094 	  || !EXPR_HAS_LOCATION (expr)
4095 	  || !rexpr_has_location (label_false)
4096 	  || EXPR_LOCATION (expr) == rexpr_location (label_false)))
4097     {
4098       have_else_clause_p = true;
4099       label_false = GOTO_DESTINATION (label_false);
4100     }
4101   else
4102     label_false = create_artificial_label (UNKNOWN_LOCATION);
4103 
4104   gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
4105 				 &arm2);
4106   cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
4107 				 label_false);
4108   gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
4109   gimplify_seq_add_stmt (&seq, cond_stmt);
4110   gimple_stmt_iterator gsi = gsi_last (seq);
4111   maybe_fold_stmt (&gsi);
4112 
4113   label_cont = NULL_TREE;
4114   if (!have_then_clause_p)
4115     {
4116       /* For if (...) {} else { code; } put label_true after
4117 	 the else block.  */
4118       if (TREE_OPERAND (expr, 1) == NULL_TREE
4119 	  && !have_else_clause_p
4120 	  && TREE_OPERAND (expr, 2) != NULL_TREE)
4121 	label_cont = label_true;
4122       else
4123 	{
4124 	  gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
4125 	  have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
4126 	  /* For if (...) { code; } else {} or
4127 	     if (...) { code; } else goto label; or
4128 	     if (...) { code; return; } else { ... }
4129 	     label_cont isn't needed.  */
4130 	  if (!have_else_clause_p
4131 	      && TREE_OPERAND (expr, 2) != NULL_TREE
4132 	      && gimple_seq_may_fallthru (seq))
4133 	    {
4134 	      gimple *g;
4135 	      label_cont = create_artificial_label (UNKNOWN_LOCATION);
4136 
4137 	      g = gimple_build_goto (label_cont);
4138 
4139 	      /* GIMPLE_COND's are very low level; they have embedded
4140 		 gotos.  This particular embedded goto should not be marked
4141 		 with the location of the original COND_EXPR, as it would
4142 		 correspond to the COND_EXPR's condition, not the ELSE or the
4143 		 THEN arms.  To avoid marking it with the wrong location, flag
4144 		 it as "no location".  */
4145 	      gimple_set_do_not_emit_location (g);
4146 
4147 	      gimplify_seq_add_stmt (&seq, g);
4148 	    }
4149 	}
4150     }
4151   if (!have_else_clause_p)
4152     {
4153       gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
4154       have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
4155     }
4156   if (label_cont)
4157     gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
4158 
4159   gimple_pop_condition (pre_p);
4160   gimple_seq_add_seq (pre_p, seq);
4161 
4162   if (ret == GS_ERROR)
4163     ; /* Do nothing.  */
4164   else if (have_then_clause_p || have_else_clause_p)
4165     ret = GS_ALL_DONE;
4166   else
4167     {
4168       /* Both arms are empty; replace the COND_EXPR with its predicate.  */
4169       expr = TREE_OPERAND (expr, 0);
4170       gimplify_stmt (&expr, pre_p);
4171     }
4172 
4173   *expr_p = NULL;
4174   return ret;
4175 }
4176 
4177 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4178    to be marked addressable.
4179 
4180    We cannot rely on such an expression being directly markable if a temporary
4181    has been created by the gimplification.  In this case, we create another
4182    temporary and initialize it with a copy, which will become a store after we
4183    mark it addressable.  This can happen if the front-end passed us something
4184    that it could not mark addressable yet, like a Fortran pass-by-reference
4185    parameter (int) floatvar.  */
4186 
4187 static void
4188 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4189 {
4190   while (handled_component_p (*expr_p))
4191     expr_p = &TREE_OPERAND (*expr_p, 0);
4192   if (is_gimple_reg (*expr_p))
4193     {
4194       /* Do not allow an SSA name as the temporary.  */
4195       tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4196       DECL_GIMPLE_REG_P (var) = 0;
4197       *expr_p = var;
4198     }
4199 }
4200 
4201 /* A subroutine of gimplify_modify_expr.  Replace a MODIFY_EXPR with
4202    a call to __builtin_memcpy.  */
4203 
4204 static enum gimplify_status
4205 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4206     				gimple_seq *seq_p)
4207 {
4208   tree t, to, to_ptr, from, from_ptr;
4209   gcall *gs;
4210   location_t loc = EXPR_LOCATION (*expr_p);
4211 
4212   to = TREE_OPERAND (*expr_p, 0);
4213   from = TREE_OPERAND (*expr_p, 1);
4214 
4215   /* Mark the RHS addressable.  Beware that it may not be possible to do so
4216      directly if a temporary has been created by the gimplification.  */
4217   prepare_gimple_addressable (&from, seq_p);
4218 
4219   mark_addressable (from);
4220   from_ptr = build_fold_addr_expr_loc (loc, from);
4221   gimplify_arg (&from_ptr, seq_p, loc);
4222 
4223   mark_addressable (to);
4224   to_ptr = build_fold_addr_expr_loc (loc, to);
4225   gimplify_arg (&to_ptr, seq_p, loc);
4226 
4227   t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4228 
4229   gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4230 
4231   if (want_value)
4232     {
4233       /* tmp = memcpy() */
4234       t = create_tmp_var (TREE_TYPE (to_ptr));
4235       gimple_call_set_lhs (gs, t);
4236       gimplify_seq_add_stmt (seq_p, gs);
4237 
4238       *expr_p = build_simple_mem_ref (t);
4239       return GS_ALL_DONE;
4240     }
4241 
4242   gimplify_seq_add_stmt (seq_p, gs);
4243   *expr_p = NULL;
4244   return GS_ALL_DONE;
4245 }
4246 
4247 /* A subroutine of gimplify_modify_expr.  Replace a MODIFY_EXPR with
4248    a call to __builtin_memset.  In this case we know that the RHS is
4249    a CONSTRUCTOR with an empty element list.  */
4250 
4251 static enum gimplify_status
4252 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4253     				gimple_seq *seq_p)
4254 {
4255   tree t, from, to, to_ptr;
4256   gcall *gs;
4257   location_t loc = EXPR_LOCATION (*expr_p);
4258 
4259   /* Assert our assumptions, to abort instead of producing wrong code
4260      silently if they are not met.  Beware that the RHS CONSTRUCTOR might
4261      not be immediately exposed.  */
4262   from = TREE_OPERAND (*expr_p, 1);
4263   if (TREE_CODE (from) == WITH_SIZE_EXPR)
4264     from = TREE_OPERAND (from, 0);
4265 
4266   gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4267 	      && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4268 
4269   /* Now proceed.  */
4270   to = TREE_OPERAND (*expr_p, 0);
4271 
4272   to_ptr = build_fold_addr_expr_loc (loc, to);
4273   gimplify_arg (&to_ptr, seq_p, loc);
4274   t = builtin_decl_implicit (BUILT_IN_MEMSET);
4275 
4276   gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4277 
4278   if (want_value)
4279     {
4280       /* tmp = memset() */
4281       t = create_tmp_var (TREE_TYPE (to_ptr));
4282       gimple_call_set_lhs (gs, t);
4283       gimplify_seq_add_stmt (seq_p, gs);
4284 
4285       *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4286       return GS_ALL_DONE;
4287     }
4288 
4289   gimplify_seq_add_stmt (seq_p, gs);
4290   *expr_p = NULL;
4291   return GS_ALL_DONE;
4292 }
4293 
4294 /* A subroutine of gimplify_init_ctor_preeval.  Called via walk_tree,
4295    determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4296    assignment.  Return non-null if we detect a potential overlap.  */
4297 
4298 struct gimplify_init_ctor_preeval_data
4299 {
4300   /* The base decl of the lhs object.  May be NULL, in which case we
4301      have to assume the lhs is indirect.  */
4302   tree lhs_base_decl;
4303 
4304   /* The alias set of the lhs object.  */
4305   alias_set_type lhs_alias_set;
4306 };
4307 
4308 static tree
4309 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4310 {
4311   struct gimplify_init_ctor_preeval_data *data
4312     = (struct gimplify_init_ctor_preeval_data *) xdata;
4313   tree t = *tp;
4314 
4315   /* If we find the base object, obviously we have overlap.  */
4316   if (data->lhs_base_decl == t)
4317     return t;
4318 
4319   /* If the constructor component is indirect, determine if we have a
4320      potential overlap with the lhs.  The only bits of information we
4321      have to go on at this point are addressability and alias sets.  */
4322   if ((INDIRECT_REF_P (t)
4323        || TREE_CODE (t) == MEM_REF)
4324       && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4325       && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4326     return t;
4327 
4328   /* If the constructor component is a call, determine if it can hide a
4329      potential overlap with the lhs through an INDIRECT_REF like above.
4330      ??? Ugh - this is completely broken.  In fact this whole analysis
4331      doesn't look conservative.  */
4332   if (TREE_CODE (t) == CALL_EXPR)
4333     {
4334       tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4335 
4336       for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4337 	if (POINTER_TYPE_P (TREE_VALUE (type))
4338 	    && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4339 	    && alias_sets_conflict_p (data->lhs_alias_set,
4340 				      get_alias_set
4341 				        (TREE_TYPE (TREE_VALUE (type)))))
4342 	  return t;
4343     }
4344 
4345   if (IS_TYPE_OR_DECL_P (t))
4346     *walk_subtrees = 0;
4347   return NULL;
4348 }
4349 
4350 /* A subroutine of gimplify_init_constructor.  Pre-evaluate EXPR,
4351    force values that overlap with the lhs (as described by *DATA)
4352    into temporaries.  */
4353 
4354 static void
4355 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4356 			    struct gimplify_init_ctor_preeval_data *data)
4357 {
4358   enum gimplify_status one;
4359 
4360   /* If the value is constant, then there's nothing to pre-evaluate.  */
4361   if (TREE_CONSTANT (*expr_p))
4362     {
4363       /* Ensure it does not have side effects, it might contain a reference to
4364 	 the object we're initializing.  */
4365       gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4366       return;
4367     }
4368 
4369   /* If the type has non-trivial constructors, we can't pre-evaluate.  */
4370   if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4371     return;
4372 
4373   /* Recurse for nested constructors.  */
4374   if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4375     {
4376       unsigned HOST_WIDE_INT ix;
4377       constructor_elt *ce;
4378       vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4379 
4380       FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4381 	gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4382 
4383       return;
4384     }
4385 
4386   /* If this is a variable sized type, we must remember the size.  */
4387   maybe_with_size_expr (expr_p);
4388 
4389   /* Gimplify the constructor element to something appropriate for the rhs
4390      of a MODIFY_EXPR.  Given that we know the LHS is an aggregate, we know
4391      the gimplifier will consider this a store to memory.  Doing this
4392      gimplification now means that we won't have to deal with complicated
4393      language-specific trees, nor trees like SAVE_EXPR that can induce
4394      exponential search behavior.  */
4395   one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4396   if (one == GS_ERROR)
4397     {
4398       *expr_p = NULL;
4399       return;
4400     }
4401 
4402   /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4403      with the lhs, since "a = { .x=a }" doesn't make sense.  This will
4404      always be true for all scalars, since is_gimple_mem_rhs insists on a
4405      temporary variable for them.  */
4406   if (DECL_P (*expr_p))
4407     return;
4408 
4409   /* If this is of variable size, we have no choice but to assume it doesn't
4410      overlap since we can't make a temporary for it.  */
4411   if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4412     return;
4413 
4414   /* Otherwise, we must search for overlap ...  */
4415   if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4416     return;
4417 
4418   /* ... and if found, force the value into a temporary.  */
4419   *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4420 }
4421 
4422 /* A subroutine of gimplify_init_ctor_eval.  Create a loop for
4423    a RANGE_EXPR in a CONSTRUCTOR for an array.
4424 
4425       var = lower;
4426     loop_entry:
4427       object[var] = value;
4428       if (var == upper)
4429 	goto loop_exit;
4430       var = var + 1;
4431       goto loop_entry;
4432     loop_exit:
4433 
4434    We increment var _after_ the loop exit check because we might otherwise
4435    fail if upper == TYPE_MAX_VALUE (type for upper).
4436 
4437    Note that we never have to deal with SAVE_EXPRs here, because this has
4438    already been taken care of for us, in gimplify_init_ctor_preeval().  */
4439 
4440 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4441 				     gimple_seq *, bool);
4442 
4443 static void
4444 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4445 			       tree value, tree array_elt_type,
4446 			       gimple_seq *pre_p, bool cleared)
4447 {
4448   tree loop_entry_label, loop_exit_label, fall_thru_label;
4449   tree var, var_type, cref, tmp;
4450 
4451   loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4452   loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4453   fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4454 
4455   /* Create and initialize the index variable.  */
4456   var_type = TREE_TYPE (upper);
4457   var = create_tmp_var (var_type);
4458   gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4459 
4460   /* Add the loop entry label.  */
4461   gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4462 
4463   /* Build the reference.  */
4464   cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4465 		 var, NULL_TREE, NULL_TREE);
4466 
4467   /* If we are a constructor, just call gimplify_init_ctor_eval to do
4468      the store.  Otherwise just assign value to the reference.  */
4469 
4470   if (TREE_CODE (value) == CONSTRUCTOR)
4471     /* NB we might have to call ourself recursively through
4472        gimplify_init_ctor_eval if the value is a constructor.  */
4473     gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4474 			     pre_p, cleared);
4475   else
4476     gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4477 
4478   /* We exit the loop when the index var is equal to the upper bound.  */
4479   gimplify_seq_add_stmt (pre_p,
4480 			 gimple_build_cond (EQ_EXPR, var, upper,
4481 					    loop_exit_label, fall_thru_label));
4482 
4483   gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4484 
4485   /* Otherwise, increment the index var...  */
4486   tmp = build2 (PLUS_EXPR, var_type, var,
4487 		fold_convert (var_type, integer_one_node));
4488   gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4489 
4490   /* ...and jump back to the loop entry.  */
4491   gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4492 
4493   /* Add the loop exit label.  */
4494   gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4495 }
4496 
4497 /* Return true if FDECL is accessing a field that is zero sized.  */
4498 
4499 static bool
4500 zero_sized_field_decl (const_tree fdecl)
4501 {
4502   if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4503       && integer_zerop (DECL_SIZE (fdecl)))
4504     return true;
4505   return false;
4506 }
4507 
4508 /* Return true if TYPE is zero sized.  */
4509 
4510 static bool
4511 zero_sized_type (const_tree type)
4512 {
4513   if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4514       && integer_zerop (TYPE_SIZE (type)))
4515     return true;
4516   return false;
4517 }
4518 
4519 /* A subroutine of gimplify_init_constructor.  Generate individual
4520    MODIFY_EXPRs for a CONSTRUCTOR.  OBJECT is the LHS against which the
4521    assignments should happen.  ELTS is the CONSTRUCTOR_ELTS of the
4522    CONSTRUCTOR.  CLEARED is true if the entire LHS object has been
4523    zeroed first.  */
4524 
4525 static void
4526 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4527 			 gimple_seq *pre_p, bool cleared)
4528 {
4529   tree array_elt_type = NULL;
4530   unsigned HOST_WIDE_INT ix;
4531   tree purpose, value;
4532 
4533   if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4534     array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4535 
4536   FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4537     {
4538       tree cref;
4539 
4540       /* NULL values are created above for gimplification errors.  */
4541       if (value == NULL)
4542 	continue;
4543 
4544       if (cleared && initializer_zerop (value))
4545 	continue;
4546 
4547       /* ??? Here's to hoping the front end fills in all of the indices,
4548 	 so we don't have to figure out what's missing ourselves.  */
4549       gcc_assert (purpose);
4550 
4551       /* Skip zero-sized fields, unless value has side-effects.  This can
4552 	 happen with calls to functions returning a zero-sized type, which
4553 	 we shouldn't discard.  As a number of downstream passes don't
4554 	 expect sets of zero-sized fields, we rely on the gimplification of
4555 	 the MODIFY_EXPR we make below to drop the assignment statement.  */
4556       if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4557 	continue;
4558 
4559       /* If we have a RANGE_EXPR, we have to build a loop to assign the
4560 	 whole range.  */
4561       if (TREE_CODE (purpose) == RANGE_EXPR)
4562 	{
4563 	  tree lower = TREE_OPERAND (purpose, 0);
4564 	  tree upper = TREE_OPERAND (purpose, 1);
4565 
4566 	  /* If the lower bound is equal to upper, just treat it as if
4567 	     upper was the index.  */
4568 	  if (simple_cst_equal (lower, upper))
4569 	    purpose = upper;
4570 	  else
4571 	    {
4572 	      gimplify_init_ctor_eval_range (object, lower, upper, value,
4573 					     array_elt_type, pre_p, cleared);
4574 	      continue;
4575 	    }
4576 	}
4577 
4578       if (array_elt_type)
4579 	{
4580 	  /* Do not use bitsizetype for ARRAY_REF indices.  */
4581 	  if (TYPE_DOMAIN (TREE_TYPE (object)))
4582 	    purpose
4583 	      = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4584 			      purpose);
4585 	  cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4586 			 purpose, NULL_TREE, NULL_TREE);
4587 	}
4588       else
4589 	{
4590 	  gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4591 	  cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4592 			 unshare_expr (object), purpose, NULL_TREE);
4593 	}
4594 
4595       if (TREE_CODE (value) == CONSTRUCTOR
4596 	  && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4597 	gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4598 				 pre_p, cleared);
4599       else
4600 	{
4601 	  tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4602 	  gimplify_and_add (init, pre_p);
4603 	  ggc_free (init);
4604 	}
4605     }
4606 }
4607 
4608 /* Return the appropriate RHS predicate for this LHS.  */
4609 
4610 gimple_predicate
4611 rhs_predicate_for (tree lhs)
4612 {
4613   if (is_gimple_reg (lhs))
4614     return is_gimple_reg_rhs_or_call;
4615   else
4616     return is_gimple_mem_rhs_or_call;
4617 }
4618 
4619 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4620    before the LHS has been gimplified.  */
4621 
4622 static gimple_predicate
4623 initial_rhs_predicate_for (tree lhs)
4624 {
4625   if (is_gimple_reg_type (TREE_TYPE (lhs)))
4626     return is_gimple_reg_rhs_or_call;
4627   else
4628     return is_gimple_mem_rhs_or_call;
4629 }
4630 
4631 /* Gimplify a C99 compound literal expression.  This just means adding
4632    the DECL_EXPR before the current statement and using its anonymous
4633    decl instead.  */
4634 
4635 static enum gimplify_status
4636 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4637 				bool (*gimple_test_f) (tree),
4638 				fallback_t fallback)
4639 {
4640   tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4641   tree decl = DECL_EXPR_DECL (decl_s);
4642   tree init = DECL_INITIAL (decl);
4643   /* Mark the decl as addressable if the compound literal
4644      expression is addressable now, otherwise it is marked too late
4645      after we gimplify the initialization expression.  */
4646   if (TREE_ADDRESSABLE (*expr_p))
4647     TREE_ADDRESSABLE (decl) = 1;
4648   /* Otherwise, if we don't need an lvalue and have a literal directly
4649      substitute it.  Check if it matches the gimple predicate, as
4650      otherwise we'd generate a new temporary, and we can as well just
4651      use the decl we already have.  */
4652   else if (!TREE_ADDRESSABLE (decl)
4653 	   && init
4654 	   && (fallback & fb_lvalue) == 0
4655 	   && gimple_test_f (init))
4656     {
4657       *expr_p = init;
4658       return GS_OK;
4659     }
4660 
4661   /* Preliminarily mark non-addressed complex variables as eligible
4662      for promotion to gimple registers.  We'll transform their uses
4663      as we find them.  */
4664   if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4665        || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4666       && !TREE_THIS_VOLATILE (decl)
4667       && !needs_to_live_in_memory (decl))
4668     DECL_GIMPLE_REG_P (decl) = 1;
4669 
4670   /* If the decl is not addressable, then it is being used in some
4671      expression or on the right hand side of a statement, and it can
4672      be put into a readonly data section.  */
4673   if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4674     TREE_READONLY (decl) = 1;
4675 
4676   /* This decl isn't mentioned in the enclosing block, so add it to the
4677      list of temps.  FIXME it seems a bit of a kludge to say that
4678      anonymous artificial vars aren't pushed, but everything else is.  */
4679   if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4680     gimple_add_tmp_var (decl);
4681 
4682   gimplify_and_add (decl_s, pre_p);
4683   *expr_p = decl;
4684   return GS_OK;
4685 }
4686 
4687 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4688    return a new CONSTRUCTOR if something changed.  */
4689 
4690 static tree
4691 optimize_compound_literals_in_ctor (tree orig_ctor)
4692 {
4693   tree ctor = orig_ctor;
4694   vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4695   unsigned int idx, num = vec_safe_length (elts);
4696 
4697   for (idx = 0; idx < num; idx++)
4698     {
4699       tree value = (*elts)[idx].value;
4700       tree newval = value;
4701       if (TREE_CODE (value) == CONSTRUCTOR)
4702 	newval = optimize_compound_literals_in_ctor (value);
4703       else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4704 	{
4705 	  tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4706 	  tree decl = DECL_EXPR_DECL (decl_s);
4707 	  tree init = DECL_INITIAL (decl);
4708 
4709 	  if (!TREE_ADDRESSABLE (value)
4710 	      && !TREE_ADDRESSABLE (decl)
4711 	      && init
4712 	      && TREE_CODE (init) == CONSTRUCTOR)
4713 	    newval = optimize_compound_literals_in_ctor (init);
4714 	}
4715       if (newval == value)
4716 	continue;
4717 
4718       if (ctor == orig_ctor)
4719 	{
4720 	  ctor = copy_node (orig_ctor);
4721 	  CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4722 	  elts = CONSTRUCTOR_ELTS (ctor);
4723 	}
4724       (*elts)[idx].value = newval;
4725     }
4726   return ctor;
4727 }
4728 
4729 /* A subroutine of gimplify_modify_expr.  Break out elements of a
4730    CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4731 
4732    Note that we still need to clear any elements that don't have explicit
4733    initializers, so if not all elements are initialized we keep the
4734    original MODIFY_EXPR, we just remove all of the constructor elements.
4735 
4736    If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4737    GS_ERROR if we would have to create a temporary when gimplifying
4738    this constructor.  Otherwise, return GS_OK.
4739 
4740    If NOTIFY_TEMP_CREATION is false, just do the gimplification.  */
4741 
4742 static enum gimplify_status
4743 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4744 			   bool want_value, bool notify_temp_creation)
4745 {
4746   tree object, ctor, type;
4747   enum gimplify_status ret;
4748   vec<constructor_elt, va_gc> *elts;
4749 
4750   gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4751 
4752   if (!notify_temp_creation)
4753     {
4754       ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4755 			   is_gimple_lvalue, fb_lvalue);
4756       if (ret == GS_ERROR)
4757 	return ret;
4758     }
4759 
4760   object = TREE_OPERAND (*expr_p, 0);
4761   ctor = TREE_OPERAND (*expr_p, 1)
4762     = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4763   type = TREE_TYPE (ctor);
4764   elts = CONSTRUCTOR_ELTS (ctor);
4765   ret = GS_ALL_DONE;
4766 
4767   switch (TREE_CODE (type))
4768     {
4769     case RECORD_TYPE:
4770     case UNION_TYPE:
4771     case QUAL_UNION_TYPE:
4772     case ARRAY_TYPE:
4773       {
4774 	struct gimplify_init_ctor_preeval_data preeval_data;
4775 	HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4776 	bool cleared, complete_p, valid_const_initializer;
4777 
4778 	/* Aggregate types must lower constructors to initialization of
4779 	   individual elements.  The exception is that a CONSTRUCTOR node
4780 	   with no elements indicates zero-initialization of the whole.  */
4781 	if (vec_safe_is_empty (elts))
4782 	  {
4783 	    if (notify_temp_creation)
4784 	      return GS_OK;
4785 	    break;
4786 	  }
4787 
4788 	/* Fetch information about the constructor to direct later processing.
4789 	   We might want to make static versions of it in various cases, and
4790 	   can only do so if it known to be a valid constant initializer.  */
4791 	valid_const_initializer
4792 	  = categorize_ctor_elements (ctor, &num_nonzero_elements,
4793 				      &num_ctor_elements, &complete_p);
4794 
4795 	/* If a const aggregate variable is being initialized, then it
4796 	   should never be a lose to promote the variable to be static.  */
4797 	if (valid_const_initializer
4798 	    && num_nonzero_elements > 1
4799 	    && TREE_READONLY (object)
4800 	    && VAR_P (object)
4801 	    && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4802 	  {
4803 	    if (notify_temp_creation)
4804 	      return GS_ERROR;
4805 	    DECL_INITIAL (object) = ctor;
4806 	    TREE_STATIC (object) = 1;
4807 	    if (!DECL_NAME (object))
4808 	      DECL_NAME (object) = create_tmp_var_name ("C");
4809 	    walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4810 
4811 	    /* ??? C++ doesn't automatically append a .<number> to the
4812 	       assembler name, and even when it does, it looks at FE private
4813 	       data structures to figure out what that number should be,
4814 	       which are not set for this variable.  I suppose this is
4815 	       important for local statics for inline functions, which aren't
4816 	       "local" in the object file sense.  So in order to get a unique
4817 	       TU-local symbol, we must invoke the lhd version now.  */
4818 	    lhd_set_decl_assembler_name (object);
4819 
4820 	    *expr_p = NULL_TREE;
4821 	    break;
4822 	  }
4823 
4824 	/* If there are "lots" of initialized elements, even discounting
4825 	   those that are not address constants (and thus *must* be
4826 	   computed at runtime), then partition the constructor into
4827 	   constant and non-constant parts.  Block copy the constant
4828 	   parts in, then generate code for the non-constant parts.  */
4829 	/* TODO.  There's code in cp/typeck.c to do this.  */
4830 
4831 	if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4832 	  /* store_constructor will ignore the clearing of variable-sized
4833 	     objects.  Initializers for such objects must explicitly set
4834 	     every field that needs to be set.  */
4835 	  cleared = false;
4836 	else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
4837 	  /* If the constructor isn't complete, clear the whole object
4838 	     beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4839 
4840 	     ??? This ought not to be needed.  For any element not present
4841 	     in the initializer, we should simply set them to zero.  Except
4842 	     we'd need to *find* the elements that are not present, and that
4843 	     requires trickery to avoid quadratic compile-time behavior in
4844 	     large cases or excessive memory use in small cases.  */
4845 	  cleared = true;
4846 	else if (num_ctor_elements - num_nonzero_elements
4847 		 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4848 		 && num_nonzero_elements < num_ctor_elements / 4)
4849 	  /* If there are "lots" of zeros, it's more efficient to clear
4850 	     the memory and then set the nonzero elements.  */
4851 	  cleared = true;
4852 	else
4853 	  cleared = false;
4854 
4855 	/* If there are "lots" of initialized elements, and all of them
4856 	   are valid address constants, then the entire initializer can
4857 	   be dropped to memory, and then memcpy'd out.  Don't do this
4858 	   for sparse arrays, though, as it's more efficient to follow
4859 	   the standard CONSTRUCTOR behavior of memset followed by
4860 	   individual element initialization.  Also don't do this for small
4861 	   all-zero initializers (which aren't big enough to merit
4862 	   clearing), and don't try to make bitwise copies of
4863 	   TREE_ADDRESSABLE types.
4864 
4865 	   We cannot apply such transformation when compiling chkp static
4866 	   initializer because creation of initializer image in the memory
4867 	   will require static initialization of bounds for it.  It should
4868 	   result in another gimplification of similar initializer and we
4869 	   may fall into infinite loop.  */
4870 	if (valid_const_initializer
4871 	    && !(cleared || num_nonzero_elements == 0)
4872 	    && !TREE_ADDRESSABLE (type)
4873 	    && (!current_function_decl
4874 		|| !lookup_attribute ("chkp ctor",
4875 				      DECL_ATTRIBUTES (current_function_decl))))
4876 	  {
4877 	    HOST_WIDE_INT size = int_size_in_bytes (type);
4878 	    unsigned int align;
4879 
4880 	    /* ??? We can still get unbounded array types, at least
4881 	       from the C++ front end.  This seems wrong, but attempt
4882 	       to work around it for now.  */
4883 	    if (size < 0)
4884 	      {
4885 		size = int_size_in_bytes (TREE_TYPE (object));
4886 		if (size >= 0)
4887 		  TREE_TYPE (ctor) = type = TREE_TYPE (object);
4888 	      }
4889 
4890 	    /* Find the maximum alignment we can assume for the object.  */
4891 	    /* ??? Make use of DECL_OFFSET_ALIGN.  */
4892 	    if (DECL_P (object))
4893 	      align = DECL_ALIGN (object);
4894 	    else
4895 	      align = TYPE_ALIGN (type);
4896 
4897 	    /* Do a block move either if the size is so small as to make
4898 	       each individual move a sub-unit move on average, or if it
4899 	       is so large as to make individual moves inefficient.  */
4900 	    if (size > 0
4901 		&& num_nonzero_elements > 1
4902 		&& (size < num_nonzero_elements
4903 		    || !can_move_by_pieces (size, align)))
4904 	      {
4905 		if (notify_temp_creation)
4906 		  return GS_ERROR;
4907 
4908 		walk_tree (&ctor, force_labels_r, NULL, NULL);
4909 		ctor = tree_output_constant_def (ctor);
4910 		if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4911 		  ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4912 		TREE_OPERAND (*expr_p, 1) = ctor;
4913 
4914 		/* This is no longer an assignment of a CONSTRUCTOR, but
4915 		   we still may have processing to do on the LHS.  So
4916 		   pretend we didn't do anything here to let that happen.  */
4917 		return GS_UNHANDLED;
4918 	      }
4919 	  }
4920 
4921 	/* If the target is volatile, we have non-zero elements and more than
4922 	   one field to assign, initialize the target from a temporary.  */
4923 	if (TREE_THIS_VOLATILE (object)
4924 	    && !TREE_ADDRESSABLE (type)
4925 	    && num_nonzero_elements > 0
4926 	    && vec_safe_length (elts) > 1)
4927 	  {
4928 	    tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4929 	    TREE_OPERAND (*expr_p, 0) = temp;
4930 	    *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4931 			      *expr_p,
4932 			      build2 (MODIFY_EXPR, void_type_node,
4933 				      object, temp));
4934 	    return GS_OK;
4935 	  }
4936 
4937 	if (notify_temp_creation)
4938 	  return GS_OK;
4939 
4940 	/* If there are nonzero elements and if needed, pre-evaluate to capture
4941 	   elements overlapping with the lhs into temporaries.  We must do this
4942 	   before clearing to fetch the values before they are zeroed-out.  */
4943 	if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4944 	  {
4945 	    preeval_data.lhs_base_decl = get_base_address (object);
4946 	    if (!DECL_P (preeval_data.lhs_base_decl))
4947 	      preeval_data.lhs_base_decl = NULL;
4948 	    preeval_data.lhs_alias_set = get_alias_set (object);
4949 
4950 	    gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4951 					pre_p, post_p, &preeval_data);
4952 	  }
4953 
4954 	bool ctor_has_side_effects_p
4955 	  = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4956 
4957 	if (cleared)
4958 	  {
4959 	    /* Zap the CONSTRUCTOR element list, which simplifies this case.
4960 	       Note that we still have to gimplify, in order to handle the
4961 	       case of variable sized types.  Avoid shared tree structures.  */
4962 	    CONSTRUCTOR_ELTS (ctor) = NULL;
4963 	    TREE_SIDE_EFFECTS (ctor) = 0;
4964 	    object = unshare_expr (object);
4965 	    gimplify_stmt (expr_p, pre_p);
4966 	  }
4967 
4968 	/* If we have not block cleared the object, or if there are nonzero
4969 	   elements in the constructor, or if the constructor has side effects,
4970 	   add assignments to the individual scalar fields of the object.  */
4971 	if (!cleared
4972 	    || num_nonzero_elements > 0
4973 	    || ctor_has_side_effects_p)
4974 	  gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4975 
4976 	*expr_p = NULL_TREE;
4977       }
4978       break;
4979 
4980     case COMPLEX_TYPE:
4981       {
4982 	tree r, i;
4983 
4984 	if (notify_temp_creation)
4985 	  return GS_OK;
4986 
4987 	/* Extract the real and imaginary parts out of the ctor.  */
4988 	gcc_assert (elts->length () == 2);
4989 	r = (*elts)[0].value;
4990 	i = (*elts)[1].value;
4991 	if (r == NULL || i == NULL)
4992 	  {
4993 	    tree zero = build_zero_cst (TREE_TYPE (type));
4994 	    if (r == NULL)
4995 	      r = zero;
4996 	    if (i == NULL)
4997 	      i = zero;
4998 	  }
4999 
5000 	/* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
5001 	   represent creation of a complex value.  */
5002 	if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
5003 	  {
5004 	    ctor = build_complex (type, r, i);
5005 	    TREE_OPERAND (*expr_p, 1) = ctor;
5006 	  }
5007 	else
5008 	  {
5009 	    ctor = build2 (COMPLEX_EXPR, type, r, i);
5010 	    TREE_OPERAND (*expr_p, 1) = ctor;
5011 	    ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
5012 				 pre_p,
5013 				 post_p,
5014 				 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
5015 				 fb_rvalue);
5016 	  }
5017       }
5018       break;
5019 
5020     case VECTOR_TYPE:
5021       {
5022 	unsigned HOST_WIDE_INT ix;
5023 	constructor_elt *ce;
5024 
5025 	if (notify_temp_creation)
5026 	  return GS_OK;
5027 
5028 	/* Go ahead and simplify constant constructors to VECTOR_CST.  */
5029 	if (TREE_CONSTANT (ctor))
5030 	  {
5031 	    bool constant_p = true;
5032 	    tree value;
5033 
5034 	    /* Even when ctor is constant, it might contain non-*_CST
5035 	       elements, such as addresses or trapping values like
5036 	       1.0/0.0 - 1.0/0.0.  Such expressions don't belong
5037 	       in VECTOR_CST nodes.  */
5038 	    FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
5039 	      if (!CONSTANT_CLASS_P (value))
5040 		{
5041 		  constant_p = false;
5042 		  break;
5043 		}
5044 
5045 	    if (constant_p)
5046 	      {
5047 		TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
5048 		break;
5049 	      }
5050 
5051 	    TREE_CONSTANT (ctor) = 0;
5052 	  }
5053 
5054 	/* Vector types use CONSTRUCTOR all the way through gimple
5055 	   compilation as a general initializer.  */
5056 	FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5057 	  {
5058 	    enum gimplify_status tret;
5059 	    tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5060 				  fb_rvalue);
5061 	    if (tret == GS_ERROR)
5062 	      ret = GS_ERROR;
5063 	    else if (TREE_STATIC (ctor)
5064 		     && !initializer_constant_valid_p (ce->value,
5065 						       TREE_TYPE (ce->value)))
5066 	      TREE_STATIC (ctor) = 0;
5067 	  }
5068 	if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5069 	  TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5070       }
5071       break;
5072 
5073     default:
5074       /* So how did we get a CONSTRUCTOR for a scalar type?  */
5075       gcc_unreachable ();
5076     }
5077 
5078   if (ret == GS_ERROR)
5079     return GS_ERROR;
5080   /* If we have gimplified both sides of the initializer but have
5081      not emitted an assignment, do so now.  */
5082   if (*expr_p)
5083     {
5084       tree lhs = TREE_OPERAND (*expr_p, 0);
5085       tree rhs = TREE_OPERAND (*expr_p, 1);
5086       if (want_value && object == lhs)
5087 	lhs = unshare_expr (lhs);
5088       gassign *init = gimple_build_assign (lhs, rhs);
5089       gimplify_seq_add_stmt (pre_p, init);
5090     }
5091   if (want_value)
5092     {
5093       *expr_p = object;
5094       return GS_OK;
5095     }
5096   else
5097     {
5098       *expr_p = NULL;
5099       return GS_ALL_DONE;
5100     }
5101 }
5102 
5103 /* Given a pointer value OP0, return a simplified version of an
5104    indirection through OP0, or NULL_TREE if no simplification is
5105    possible.  This may only be applied to a rhs of an expression.
5106    Note that the resulting type may be different from the type pointed
5107    to in the sense that it is still compatible from the langhooks
5108    point of view. */
5109 
5110 static tree
5111 gimple_fold_indirect_ref_rhs (tree t)
5112 {
5113   return gimple_fold_indirect_ref (t);
5114 }
5115 
5116 /* Subroutine of gimplify_modify_expr to do simplifications of
5117    MODIFY_EXPRs based on the code of the RHS.  We loop for as long as
5118    something changes.  */
5119 
5120 static enum gimplify_status
5121 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5122 			  gimple_seq *pre_p, gimple_seq *post_p,
5123 			  bool want_value)
5124 {
5125   enum gimplify_status ret = GS_UNHANDLED;
5126   bool changed;
5127 
5128   do
5129     {
5130       changed = false;
5131       switch (TREE_CODE (*from_p))
5132 	{
5133 	case VAR_DECL:
5134 	  /* If we're assigning from a read-only variable initialized with
5135 	     a constructor, do the direct assignment from the constructor,
5136 	     but only if neither source nor target are volatile since this
5137 	     latter assignment might end up being done on a per-field basis.  */
5138 	  if (DECL_INITIAL (*from_p)
5139 	      && TREE_READONLY (*from_p)
5140 	      && !TREE_THIS_VOLATILE (*from_p)
5141 	      && !TREE_THIS_VOLATILE (*to_p)
5142 	      && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5143 	    {
5144 	      tree old_from = *from_p;
5145 	      enum gimplify_status subret;
5146 
5147 	      /* Move the constructor into the RHS.  */
5148 	      *from_p = unshare_expr (DECL_INITIAL (*from_p));
5149 
5150 	      /* Let's see if gimplify_init_constructor will need to put
5151 		 it in memory.  */
5152 	      subret = gimplify_init_constructor (expr_p, NULL, NULL,
5153 						  false, true);
5154 	      if (subret == GS_ERROR)
5155 		{
5156 		  /* If so, revert the change.  */
5157 		  *from_p = old_from;
5158 		}
5159 	      else
5160 		{
5161 		  ret = GS_OK;
5162 		  changed = true;
5163 		}
5164 	    }
5165 	  break;
5166 	case INDIRECT_REF:
5167 	  {
5168 	    /* If we have code like
5169 
5170 	     *(const A*)(A*)&x
5171 
5172 	     where the type of "x" is a (possibly cv-qualified variant
5173 	     of "A"), treat the entire expression as identical to "x".
5174 	     This kind of code arises in C++ when an object is bound
5175 	     to a const reference, and if "x" is a TARGET_EXPR we want
5176 	     to take advantage of the optimization below.  */
5177 	    bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5178 	    tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5179 	    if (t)
5180 	      {
5181 		if (TREE_THIS_VOLATILE (t) != volatile_p)
5182 		  {
5183 		    if (DECL_P (t))
5184 		      t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5185 						    build_fold_addr_expr (t));
5186 		    if (REFERENCE_CLASS_P (t))
5187 		      TREE_THIS_VOLATILE (t) = volatile_p;
5188 		  }
5189 		*from_p = t;
5190 		ret = GS_OK;
5191 		changed = true;
5192 	      }
5193 	    break;
5194 	  }
5195 
5196 	case TARGET_EXPR:
5197 	  {
5198 	    /* If we are initializing something from a TARGET_EXPR, strip the
5199 	       TARGET_EXPR and initialize it directly, if possible.  This can't
5200 	       be done if the initializer is void, since that implies that the
5201 	       temporary is set in some non-trivial way.
5202 
5203 	       ??? What about code that pulls out the temp and uses it
5204 	       elsewhere? I think that such code never uses the TARGET_EXPR as
5205 	       an initializer.  If I'm wrong, we'll die because the temp won't
5206 	       have any RTL.  In that case, I guess we'll need to replace
5207 	       references somehow.  */
5208 	    tree init = TARGET_EXPR_INITIAL (*from_p);
5209 
5210 	    if (init
5211 		&& (TREE_CODE (*expr_p) != MODIFY_EXPR
5212 		    || !TARGET_EXPR_NO_ELIDE (*from_p))
5213 		&& !VOID_TYPE_P (TREE_TYPE (init)))
5214 	      {
5215 		*from_p = init;
5216 		ret = GS_OK;
5217 		changed = true;
5218 	      }
5219 	  }
5220 	  break;
5221 
5222 	case COMPOUND_EXPR:
5223 	  /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5224 	     caught.  */
5225 	  gimplify_compound_expr (from_p, pre_p, true);
5226 	  ret = GS_OK;
5227 	  changed = true;
5228 	  break;
5229 
5230 	case CONSTRUCTOR:
5231 	  /* If we already made some changes, let the front end have a
5232 	     crack at this before we break it down.  */
5233 	  if (ret != GS_UNHANDLED)
5234 	    break;
5235 	  /* If we're initializing from a CONSTRUCTOR, break this into
5236 	     individual MODIFY_EXPRs.  */
5237 	  return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5238 					    false);
5239 
5240 	case COND_EXPR:
5241 	  /* If we're assigning to a non-register type, push the assignment
5242 	     down into the branches.  This is mandatory for ADDRESSABLE types,
5243 	     since we cannot generate temporaries for such, but it saves a
5244 	     copy in other cases as well.  */
5245 	  if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5246 	    {
5247 	      /* This code should mirror the code in gimplify_cond_expr. */
5248 	      enum tree_code code = TREE_CODE (*expr_p);
5249 	      tree cond = *from_p;
5250 	      tree result = *to_p;
5251 
5252 	      ret = gimplify_expr (&result, pre_p, post_p,
5253 				   is_gimple_lvalue, fb_lvalue);
5254 	      if (ret != GS_ERROR)
5255 		ret = GS_OK;
5256 
5257 	      /* If we are going to write RESULT more than once, clear
5258 		 TREE_READONLY flag, otherwise we might incorrectly promote
5259 		 the variable to static const and initialize it at compile
5260 		 time in one of the branches.  */
5261 	      if (VAR_P (result)
5262 		  && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5263 		  && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5264 		TREE_READONLY (result) = 0;
5265 	      if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5266 		TREE_OPERAND (cond, 1)
5267 		  = build2 (code, void_type_node, result,
5268 			    TREE_OPERAND (cond, 1));
5269 	      if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5270 		TREE_OPERAND (cond, 2)
5271 		  = build2 (code, void_type_node, unshare_expr (result),
5272 			    TREE_OPERAND (cond, 2));
5273 
5274 	      TREE_TYPE (cond) = void_type_node;
5275 	      recalculate_side_effects (cond);
5276 
5277 	      if (want_value)
5278 		{
5279 		  gimplify_and_add (cond, pre_p);
5280 		  *expr_p = unshare_expr (result);
5281 		}
5282 	      else
5283 		*expr_p = cond;
5284 	      return ret;
5285 	    }
5286 	  break;
5287 
5288 	case CALL_EXPR:
5289 	  /* For calls that return in memory, give *to_p as the CALL_EXPR's
5290 	     return slot so that we don't generate a temporary.  */
5291 	  if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5292 	      && aggregate_value_p (*from_p, *from_p))
5293 	    {
5294 	      bool use_target;
5295 
5296 	      if (!(rhs_predicate_for (*to_p))(*from_p))
5297 		/* If we need a temporary, *to_p isn't accurate.  */
5298 		use_target = false;
5299 	      /* It's OK to use the return slot directly unless it's an NRV. */
5300 	      else if (TREE_CODE (*to_p) == RESULT_DECL
5301 		       && DECL_NAME (*to_p) == NULL_TREE
5302 		       && needs_to_live_in_memory (*to_p))
5303 		use_target = true;
5304 	      else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5305 		       || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5306 		/* Don't force regs into memory.  */
5307 		use_target = false;
5308 	      else if (TREE_CODE (*expr_p) == INIT_EXPR)
5309 		/* It's OK to use the target directly if it's being
5310 		   initialized. */
5311 		use_target = true;
5312 	      else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5313 		       != INTEGER_CST)
5314 		/* Always use the target and thus RSO for variable-sized types.
5315 		   GIMPLE cannot deal with a variable-sized assignment
5316 		   embedded in a call statement.  */
5317 		use_target = true;
5318 	      else if (TREE_CODE (*to_p) != SSA_NAME
5319 		      && (!is_gimple_variable (*to_p)
5320 			  || needs_to_live_in_memory (*to_p)))
5321 		/* Don't use the original target if it's already addressable;
5322 		   if its address escapes, and the called function uses the
5323 		   NRV optimization, a conforming program could see *to_p
5324 		   change before the called function returns; see c++/19317.
5325 		   When optimizing, the return_slot pass marks more functions
5326 		   as safe after we have escape info.  */
5327 		use_target = false;
5328 	      else
5329 		use_target = true;
5330 
5331 	      if (use_target)
5332 		{
5333 		  CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5334 		  mark_addressable (*to_p);
5335 		}
5336 	    }
5337 	  break;
5338 
5339 	case WITH_SIZE_EXPR:
5340 	  /* Likewise for calls that return an aggregate of non-constant size,
5341 	     since we would not be able to generate a temporary at all.  */
5342 	  if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5343 	    {
5344 	      *from_p = TREE_OPERAND (*from_p, 0);
5345 	      /* We don't change ret in this case because the
5346 		 WITH_SIZE_EXPR might have been added in
5347 		 gimplify_modify_expr, so returning GS_OK would lead to an
5348 		 infinite loop.  */
5349 	      changed = true;
5350 	    }
5351 	  break;
5352 
5353 	  /* If we're initializing from a container, push the initialization
5354 	     inside it.  */
5355 	case CLEANUP_POINT_EXPR:
5356 	case BIND_EXPR:
5357 	case STATEMENT_LIST:
5358 	  {
5359 	    tree wrap = *from_p;
5360 	    tree t;
5361 
5362 	    ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5363 				 fb_lvalue);
5364 	    if (ret != GS_ERROR)
5365 	      ret = GS_OK;
5366 
5367 	    t = voidify_wrapper_expr (wrap, *expr_p);
5368 	    gcc_assert (t == *expr_p);
5369 
5370 	    if (want_value)
5371 	      {
5372 		gimplify_and_add (wrap, pre_p);
5373 		*expr_p = unshare_expr (*to_p);
5374 	      }
5375 	    else
5376 	      *expr_p = wrap;
5377 	    return GS_OK;
5378 	  }
5379 
5380 	case COMPOUND_LITERAL_EXPR:
5381 	  {
5382 	    tree complit = TREE_OPERAND (*expr_p, 1);
5383 	    tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5384 	    tree decl = DECL_EXPR_DECL (decl_s);
5385 	    tree init = DECL_INITIAL (decl);
5386 
5387 	    /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5388 	       into struct T x = { 0, 1, 2 } if the address of the
5389 	       compound literal has never been taken.  */
5390 	    if (!TREE_ADDRESSABLE (complit)
5391 		&& !TREE_ADDRESSABLE (decl)
5392 		&& init)
5393 	      {
5394 		*expr_p = copy_node (*expr_p);
5395 		TREE_OPERAND (*expr_p, 1) = init;
5396 		return GS_OK;
5397 	      }
5398 	  }
5399 
5400 	default:
5401 	  break;
5402 	}
5403     }
5404   while (changed);
5405 
5406   return ret;
5407 }
5408 
5409 
5410 /* Return true if T looks like a valid GIMPLE statement.  */
5411 
5412 static bool
5413 is_gimple_stmt (tree t)
5414 {
5415   const enum tree_code code = TREE_CODE (t);
5416 
5417   switch (code)
5418     {
5419     case NOP_EXPR:
5420       /* The only valid NOP_EXPR is the empty statement.  */
5421       return IS_EMPTY_STMT (t);
5422 
5423     case BIND_EXPR:
5424     case COND_EXPR:
5425       /* These are only valid if they're void.  */
5426       return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5427 
5428     case SWITCH_EXPR:
5429     case GOTO_EXPR:
5430     case RETURN_EXPR:
5431     case LABEL_EXPR:
5432     case CASE_LABEL_EXPR:
5433     case TRY_CATCH_EXPR:
5434     case TRY_FINALLY_EXPR:
5435     case EH_FILTER_EXPR:
5436     case CATCH_EXPR:
5437     case ASM_EXPR:
5438     case STATEMENT_LIST:
5439     case OACC_PARALLEL:
5440     case OACC_KERNELS:
5441     case OACC_DATA:
5442     case OACC_HOST_DATA:
5443     case OACC_DECLARE:
5444     case OACC_UPDATE:
5445     case OACC_ENTER_DATA:
5446     case OACC_EXIT_DATA:
5447     case OACC_CACHE:
5448     case OMP_PARALLEL:
5449     case OMP_FOR:
5450     case OMP_SIMD:
5451     case OMP_DISTRIBUTE:
5452     case OACC_LOOP:
5453     case OMP_SECTIONS:
5454     case OMP_SECTION:
5455     case OMP_SINGLE:
5456     case OMP_MASTER:
5457     case OMP_TASKGROUP:
5458     case OMP_ORDERED:
5459     case OMP_CRITICAL:
5460     case OMP_TASK:
5461     case OMP_TARGET:
5462     case OMP_TARGET_DATA:
5463     case OMP_TARGET_UPDATE:
5464     case OMP_TARGET_ENTER_DATA:
5465     case OMP_TARGET_EXIT_DATA:
5466     case OMP_TASKLOOP:
5467     case OMP_TEAMS:
5468       /* These are always void.  */
5469       return true;
5470 
5471     case CALL_EXPR:
5472     case MODIFY_EXPR:
5473     case PREDICT_EXPR:
5474       /* These are valid regardless of their type.  */
5475       return true;
5476 
5477     default:
5478       return false;
5479     }
5480 }
5481 
5482 
5483 /* Promote partial stores to COMPLEX variables to total stores.  *EXPR_P is
5484    a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5485    DECL_GIMPLE_REG_P set.
5486 
5487    IMPORTANT NOTE: This promotion is performed by introducing a load of the
5488    other, unmodified part of the complex object just before the total store.
5489    As a consequence, if the object is still uninitialized, an undefined value
5490    will be loaded into a register, which may result in a spurious exception
5491    if the register is floating-point and the value happens to be a signaling
5492    NaN for example.  Then the fully-fledged complex operations lowering pass
5493    followed by a DCE pass are necessary in order to fix things up.  */
5494 
5495 static enum gimplify_status
5496 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5497                                    bool want_value)
5498 {
5499   enum tree_code code, ocode;
5500   tree lhs, rhs, new_rhs, other, realpart, imagpart;
5501 
5502   lhs = TREE_OPERAND (*expr_p, 0);
5503   rhs = TREE_OPERAND (*expr_p, 1);
5504   code = TREE_CODE (lhs);
5505   lhs = TREE_OPERAND (lhs, 0);
5506 
5507   ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5508   other = build1 (ocode, TREE_TYPE (rhs), lhs);
5509   TREE_NO_WARNING (other) = 1;
5510   other = get_formal_tmp_var (other, pre_p);
5511 
5512   realpart = code == REALPART_EXPR ? rhs : other;
5513   imagpart = code == REALPART_EXPR ? other : rhs;
5514 
5515   if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5516     new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5517   else
5518     new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5519 
5520   gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5521   *expr_p = (want_value) ? rhs : NULL_TREE;
5522 
5523   return GS_ALL_DONE;
5524 }
5525 
5526 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5527 
5528       modify_expr
5529 	      : varname '=' rhs
5530 	      | '*' ID '=' rhs
5531 
5532     PRE_P points to the list where side effects that must happen before
5533 	*EXPR_P should be stored.
5534 
5535     POST_P points to the list where side effects that must happen after
5536 	*EXPR_P should be stored.
5537 
5538     WANT_VALUE is nonzero iff we want to use the value of this expression
5539 	in another expression.  */
5540 
5541 static enum gimplify_status
5542 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5543 		      bool want_value)
5544 {
5545   tree *from_p = &TREE_OPERAND (*expr_p, 1);
5546   tree *to_p = &TREE_OPERAND (*expr_p, 0);
5547   enum gimplify_status ret = GS_UNHANDLED;
5548   gimple *assign;
5549   location_t loc = EXPR_LOCATION (*expr_p);
5550   gimple_stmt_iterator gsi;
5551 
5552   gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5553 	      || TREE_CODE (*expr_p) == INIT_EXPR);
5554 
5555   /* Trying to simplify a clobber using normal logic doesn't work,
5556      so handle it here.  */
5557   if (TREE_CLOBBER_P (*from_p))
5558     {
5559       ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5560       if (ret == GS_ERROR)
5561 	return ret;
5562       gcc_assert (!want_value
5563 		  && (VAR_P (*to_p) || TREE_CODE (*to_p) == MEM_REF));
5564       gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5565       *expr_p = NULL;
5566       return GS_ALL_DONE;
5567     }
5568 
5569   /* Insert pointer conversions required by the middle-end that are not
5570      required by the frontend.  This fixes middle-end type checking for
5571      for example gcc.dg/redecl-6.c.  */
5572   if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5573     {
5574       STRIP_USELESS_TYPE_CONVERSION (*from_p);
5575       if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5576 	*from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5577     }
5578 
5579   /* See if any simplifications can be done based on what the RHS is.  */
5580   ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5581 				  want_value);
5582   if (ret != GS_UNHANDLED)
5583     return ret;
5584 
5585   /* For zero sized types only gimplify the left hand side and right hand
5586      side as statements and throw away the assignment.  Do this after
5587      gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5588      types properly.  */
5589   if (zero_sized_type (TREE_TYPE (*from_p))
5590       && !want_value
5591       /* Don't do this for calls that return addressable types, expand_call
5592 	 relies on those having a lhs.  */
5593       && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5594 	   && TREE_CODE (*from_p) == CALL_EXPR))
5595     {
5596       gimplify_stmt (from_p, pre_p);
5597       gimplify_stmt (to_p, pre_p);
5598       *expr_p = NULL_TREE;
5599       return GS_ALL_DONE;
5600     }
5601 
5602   /* If the value being copied is of variable width, compute the length
5603      of the copy into a WITH_SIZE_EXPR.   Note that we need to do this
5604      before gimplifying any of the operands so that we can resolve any
5605      PLACEHOLDER_EXPRs in the size.  Also note that the RTL expander uses
5606      the size of the expression to be copied, not of the destination, so
5607      that is what we must do here.  */
5608   maybe_with_size_expr (from_p);
5609 
5610   /* As a special case, we have to temporarily allow for assignments
5611      with a CALL_EXPR on the RHS.  Since in GIMPLE a function call is
5612      a toplevel statement, when gimplifying the GENERIC expression
5613      MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5614      GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5615 
5616      Instead, we need to create the tuple GIMPLE_CALL <a, foo>.  To
5617      prevent gimplify_expr from trying to create a new temporary for
5618      foo's LHS, we tell it that it should only gimplify until it
5619      reaches the CALL_EXPR.  On return from gimplify_expr, the newly
5620      created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5621      and all we need to do here is set 'a' to be its LHS.  */
5622 
5623   /* Gimplify the RHS first for C++17 and bug 71104.  */
5624   gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5625   ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5626   if (ret == GS_ERROR)
5627     return ret;
5628 
5629   /* Then gimplify the LHS.  */
5630   /* If we gimplified the RHS to a CALL_EXPR and that call may return
5631      twice we have to make sure to gimplify into non-SSA as otherwise
5632      the abnormal edge added later will make those defs not dominate
5633      their uses.
5634      ???  Technically this applies only to the registers used in the
5635      resulting non-register *TO_P.  */
5636   bool saved_into_ssa = gimplify_ctxp->into_ssa;
5637   if (saved_into_ssa
5638       && TREE_CODE (*from_p) == CALL_EXPR
5639       && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5640     gimplify_ctxp->into_ssa = false;
5641   ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5642   gimplify_ctxp->into_ssa = saved_into_ssa;
5643   if (ret == GS_ERROR)
5644     return ret;
5645 
5646   /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5647      guess for the predicate was wrong.  */
5648   gimple_predicate final_pred = rhs_predicate_for (*to_p);
5649   if (final_pred != initial_pred)
5650     {
5651       ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5652       if (ret == GS_ERROR)
5653 	return ret;
5654     }
5655 
5656   /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5657      size as argument to the call.  */
5658   if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5659     {
5660       tree call = TREE_OPERAND (*from_p, 0);
5661       tree vlasize = TREE_OPERAND (*from_p, 1);
5662 
5663       if (TREE_CODE (call) == CALL_EXPR
5664 	  && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5665 	{
5666 	  int nargs = call_expr_nargs (call);
5667 	  tree type = TREE_TYPE (call);
5668 	  tree ap = CALL_EXPR_ARG (call, 0);
5669 	  tree tag = CALL_EXPR_ARG (call, 1);
5670 	  tree aptag = CALL_EXPR_ARG (call, 2);
5671 	  tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5672 						       IFN_VA_ARG, type,
5673 						       nargs + 1, ap, tag,
5674 						       aptag, vlasize);
5675 	  TREE_OPERAND (*from_p, 0) = newcall;
5676 	}
5677     }
5678 
5679   /* Now see if the above changed *from_p to something we handle specially.  */
5680   ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5681 				  want_value);
5682   if (ret != GS_UNHANDLED)
5683     return ret;
5684 
5685   /* If we've got a variable sized assignment between two lvalues (i.e. does
5686      not involve a call), then we can make things a bit more straightforward
5687      by converting the assignment to memcpy or memset.  */
5688   if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5689     {
5690       tree from = TREE_OPERAND (*from_p, 0);
5691       tree size = TREE_OPERAND (*from_p, 1);
5692 
5693       if (TREE_CODE (from) == CONSTRUCTOR)
5694 	return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5695 
5696       if (is_gimple_addressable (from))
5697 	{
5698 	  *from_p = from;
5699 	  return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5700 	      					 pre_p);
5701 	}
5702     }
5703 
5704   /* Transform partial stores to non-addressable complex variables into
5705      total stores.  This allows us to use real instead of virtual operands
5706      for these variables, which improves optimization.  */
5707   if ((TREE_CODE (*to_p) == REALPART_EXPR
5708        || TREE_CODE (*to_p) == IMAGPART_EXPR)
5709       && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5710     return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5711 
5712   /* Try to alleviate the effects of the gimplification creating artificial
5713      temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5714      make sure not to create DECL_DEBUG_EXPR links across functions.  */
5715   if (!gimplify_ctxp->into_ssa
5716       && VAR_P (*from_p)
5717       && DECL_IGNORED_P (*from_p)
5718       && DECL_P (*to_p)
5719       && !DECL_IGNORED_P (*to_p)
5720       && decl_function_context (*to_p) == current_function_decl
5721       && decl_function_context (*from_p) == current_function_decl)
5722     {
5723       if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5724 	DECL_NAME (*from_p)
5725 	  = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5726       DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5727       SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5728    }
5729 
5730   if (want_value && TREE_THIS_VOLATILE (*to_p))
5731     *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5732 
5733   if (TREE_CODE (*from_p) == CALL_EXPR)
5734     {
5735       /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5736 	 instead of a GIMPLE_ASSIGN.  */
5737       gcall *call_stmt;
5738       if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5739 	{
5740 	  /* Gimplify internal functions created in the FEs.  */
5741 	  int nargs = call_expr_nargs (*from_p), i;
5742 	  enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5743 	  auto_vec<tree> vargs (nargs);
5744 
5745 	  for (i = 0; i < nargs; i++)
5746 	    {
5747 	      gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5748 			    EXPR_LOCATION (*from_p));
5749 	      vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5750 	    }
5751 	  call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5752 	  gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5753 	  gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5754 	}
5755       else
5756 	{
5757 	  tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5758 	  CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5759 	  STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5760 	  tree fndecl = get_callee_fndecl (*from_p);
5761 	  if (fndecl
5762 	      && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5763 	      && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
5764 	      && call_expr_nargs (*from_p) == 3)
5765 	    call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5766 						    CALL_EXPR_ARG (*from_p, 0),
5767 						    CALL_EXPR_ARG (*from_p, 1),
5768 						    CALL_EXPR_ARG (*from_p, 2));
5769 	  else
5770 	    {
5771 	      call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5772 	    }
5773 	}
5774       notice_special_calls (call_stmt);
5775       if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5776 	gimple_call_set_lhs (call_stmt, *to_p);
5777       else if (TREE_CODE (*to_p) == SSA_NAME)
5778 	/* The above is somewhat premature, avoid ICEing later for a
5779 	   SSA name w/o a definition.  We may have uses in the GIMPLE IL.
5780 	   ???  This doesn't make it a default-def.  */
5781 	SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5782 
5783       assign = call_stmt;
5784     }
5785   else
5786     {
5787       assign = gimple_build_assign (*to_p, *from_p);
5788       gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5789       if (COMPARISON_CLASS_P (*from_p))
5790 	gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5791     }
5792 
5793   if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5794     {
5795       /* We should have got an SSA name from the start.  */
5796       gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5797 		  || ! gimple_in_ssa_p (cfun));
5798     }
5799 
5800   gimplify_seq_add_stmt (pre_p, assign);
5801   gsi = gsi_last (*pre_p);
5802   maybe_fold_stmt (&gsi);
5803 
5804   if (want_value)
5805     {
5806       *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5807       return GS_OK;
5808     }
5809   else
5810     *expr_p = NULL;
5811 
5812   return GS_ALL_DONE;
5813 }
5814 
5815 /* Gimplify a comparison between two variable-sized objects.  Do this
5816    with a call to BUILT_IN_MEMCMP.  */
5817 
5818 static enum gimplify_status
5819 gimplify_variable_sized_compare (tree *expr_p)
5820 {
5821   location_t loc = EXPR_LOCATION (*expr_p);
5822   tree op0 = TREE_OPERAND (*expr_p, 0);
5823   tree op1 = TREE_OPERAND (*expr_p, 1);
5824   tree t, arg, dest, src, expr;
5825 
5826   arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5827   arg = unshare_expr (arg);
5828   arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5829   src = build_fold_addr_expr_loc (loc, op1);
5830   dest = build_fold_addr_expr_loc (loc, op0);
5831   t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5832   t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5833 
5834   expr
5835     = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5836   SET_EXPR_LOCATION (expr, loc);
5837   *expr_p = expr;
5838 
5839   return GS_OK;
5840 }
5841 
5842 /* Gimplify a comparison between two aggregate objects of integral scalar
5843    mode as a comparison between the bitwise equivalent scalar values.  */
5844 
5845 static enum gimplify_status
5846 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5847 {
5848   location_t loc = EXPR_LOCATION (*expr_p);
5849   tree op0 = TREE_OPERAND (*expr_p, 0);
5850   tree op1 = TREE_OPERAND (*expr_p, 1);
5851 
5852   tree type = TREE_TYPE (op0);
5853   tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5854 
5855   op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5856   op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5857 
5858   *expr_p
5859     = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5860 
5861   return GS_OK;
5862 }
5863 
5864 /* Gimplify an expression sequence.  This function gimplifies each
5865    expression and rewrites the original expression with the last
5866    expression of the sequence in GIMPLE form.
5867 
5868    PRE_P points to the list where the side effects for all the
5869        expressions in the sequence will be emitted.
5870 
5871    WANT_VALUE is true when the result of the last COMPOUND_EXPR is used.  */
5872 
5873 static enum gimplify_status
5874 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5875 {
5876   tree t = *expr_p;
5877 
5878   do
5879     {
5880       tree *sub_p = &TREE_OPERAND (t, 0);
5881 
5882       if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5883 	gimplify_compound_expr (sub_p, pre_p, false);
5884       else
5885 	gimplify_stmt (sub_p, pre_p);
5886 
5887       t = TREE_OPERAND (t, 1);
5888     }
5889   while (TREE_CODE (t) == COMPOUND_EXPR);
5890 
5891   *expr_p = t;
5892   if (want_value)
5893     return GS_OK;
5894   else
5895     {
5896       gimplify_stmt (expr_p, pre_p);
5897       return GS_ALL_DONE;
5898     }
5899 }
5900 
5901 /* Gimplify a SAVE_EXPR node.  EXPR_P points to the expression to
5902    gimplify.  After gimplification, EXPR_P will point to a new temporary
5903    that holds the original value of the SAVE_EXPR node.
5904 
5905    PRE_P points to the list where side effects that must happen before
5906    *EXPR_P should be stored.  */
5907 
5908 static enum gimplify_status
5909 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5910 {
5911   enum gimplify_status ret = GS_ALL_DONE;
5912   tree val;
5913 
5914   gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5915   val = TREE_OPERAND (*expr_p, 0);
5916 
5917   /* If the SAVE_EXPR has not been resolved, then evaluate it once.  */
5918   if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5919     {
5920       /* The operand may be a void-valued expression.  It is
5921 	 being executed only for its side-effects.  */
5922       if (TREE_TYPE (val) == void_type_node)
5923 	{
5924 	  ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5925 			       is_gimple_stmt, fb_none);
5926 	  val = NULL;
5927 	}
5928       else
5929 	/* The temporary may not be an SSA name as later abnormal and EH
5930 	   control flow may invalidate use/def domination.  */
5931 	val = get_initialized_tmp_var (val, pre_p, post_p, false);
5932 
5933       TREE_OPERAND (*expr_p, 0) = val;
5934       SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5935     }
5936 
5937   *expr_p = val;
5938 
5939   return ret;
5940 }
5941 
5942 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5943 
5944       unary_expr
5945 	      : ...
5946 	      | '&' varname
5947 	      ...
5948 
5949     PRE_P points to the list where side effects that must happen before
5950 	*EXPR_P should be stored.
5951 
5952     POST_P points to the list where side effects that must happen after
5953 	*EXPR_P should be stored.  */
5954 
5955 static enum gimplify_status
5956 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5957 {
5958   tree expr = *expr_p;
5959   tree op0 = TREE_OPERAND (expr, 0);
5960   enum gimplify_status ret;
5961   location_t loc = EXPR_LOCATION (*expr_p);
5962 
5963   switch (TREE_CODE (op0))
5964     {
5965     case INDIRECT_REF:
5966     do_indirect_ref:
5967       /* Check if we are dealing with an expression of the form '&*ptr'.
5968 	 While the front end folds away '&*ptr' into 'ptr', these
5969 	 expressions may be generated internally by the compiler (e.g.,
5970 	 builtins like __builtin_va_end).  */
5971       /* Caution: the silent array decomposition semantics we allow for
5972 	 ADDR_EXPR means we can't always discard the pair.  */
5973       /* Gimplification of the ADDR_EXPR operand may drop
5974 	 cv-qualification conversions, so make sure we add them if
5975 	 needed.  */
5976       {
5977 	tree op00 = TREE_OPERAND (op0, 0);
5978 	tree t_expr = TREE_TYPE (expr);
5979 	tree t_op00 = TREE_TYPE (op00);
5980 
5981         if (!useless_type_conversion_p (t_expr, t_op00))
5982 	  op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5983         *expr_p = op00;
5984         ret = GS_OK;
5985       }
5986       break;
5987 
5988     case VIEW_CONVERT_EXPR:
5989       /* Take the address of our operand and then convert it to the type of
5990 	 this ADDR_EXPR.
5991 
5992 	 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5993 	 all clear.  The impact of this transformation is even less clear.  */
5994 
5995       /* If the operand is a useless conversion, look through it.  Doing so
5996 	 guarantees that the ADDR_EXPR and its operand will remain of the
5997 	 same type.  */
5998       if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5999 	op0 = TREE_OPERAND (op0, 0);
6000 
6001       *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
6002 				  build_fold_addr_expr_loc (loc,
6003 							TREE_OPERAND (op0, 0)));
6004       ret = GS_OK;
6005       break;
6006 
6007     case MEM_REF:
6008       if (integer_zerop (TREE_OPERAND (op0, 1)))
6009 	goto do_indirect_ref;
6010 
6011       /* fall through */
6012 
6013     default:
6014       /* If we see a call to a declared builtin or see its address
6015 	 being taken (we can unify those cases here) then we can mark
6016 	 the builtin for implicit generation by GCC.  */
6017       if (TREE_CODE (op0) == FUNCTION_DECL
6018 	  && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
6019 	  && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
6020 	set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
6021 
6022       /* We use fb_either here because the C frontend sometimes takes
6023 	 the address of a call that returns a struct; see
6024 	 gcc.dg/c99-array-lval-1.c.  The gimplifier will correctly make
6025 	 the implied temporary explicit.  */
6026 
6027       /* Make the operand addressable.  */
6028       ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
6029 			   is_gimple_addressable, fb_either);
6030       if (ret == GS_ERROR)
6031 	break;
6032 
6033       /* Then mark it.  Beware that it may not be possible to do so directly
6034 	 if a temporary has been created by the gimplification.  */
6035       prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
6036 
6037       op0 = TREE_OPERAND (expr, 0);
6038 
6039       /* For various reasons, the gimplification of the expression
6040 	 may have made a new INDIRECT_REF.  */
6041       if (TREE_CODE (op0) == INDIRECT_REF)
6042 	goto do_indirect_ref;
6043 
6044       mark_addressable (TREE_OPERAND (expr, 0));
6045 
6046       /* The FEs may end up building ADDR_EXPRs early on a decl with
6047 	 an incomplete type.  Re-build ADDR_EXPRs in canonical form
6048 	 here.  */
6049       if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
6050 	*expr_p = build_fold_addr_expr (op0);
6051 
6052       /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly.  */
6053       recompute_tree_invariant_for_addr_expr (*expr_p);
6054 
6055       /* If we re-built the ADDR_EXPR add a conversion to the original type
6056          if required.  */
6057       if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
6058 	*expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
6059 
6060       break;
6061     }
6062 
6063   return ret;
6064 }
6065 
6066 /* Gimplify the operands of an ASM_EXPR.  Input operands should be a gimple
6067    value; output operands should be a gimple lvalue.  */
6068 
6069 static enum gimplify_status
6070 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6071 {
6072   tree expr;
6073   int noutputs;
6074   const char **oconstraints;
6075   int i;
6076   tree link;
6077   const char *constraint;
6078   bool allows_mem, allows_reg, is_inout;
6079   enum gimplify_status ret, tret;
6080   gasm *stmt;
6081   vec<tree, va_gc> *inputs;
6082   vec<tree, va_gc> *outputs;
6083   vec<tree, va_gc> *clobbers;
6084   vec<tree, va_gc> *labels;
6085   tree link_next;
6086 
6087   expr = *expr_p;
6088   noutputs = list_length (ASM_OUTPUTS (expr));
6089   oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
6090 
6091   inputs = NULL;
6092   outputs = NULL;
6093   clobbers = NULL;
6094   labels = NULL;
6095 
6096   ret = GS_ALL_DONE;
6097   link_next = NULL_TREE;
6098   for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
6099     {
6100       bool ok;
6101       size_t constraint_len;
6102 
6103       link_next = TREE_CHAIN (link);
6104 
6105       oconstraints[i]
6106 	= constraint
6107 	= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6108       constraint_len = strlen (constraint);
6109       if (constraint_len == 0)
6110         continue;
6111 
6112       ok = parse_output_constraint (&constraint, i, 0, 0,
6113 				    &allows_mem, &allows_reg, &is_inout);
6114       if (!ok)
6115 	{
6116 	  ret = GS_ERROR;
6117 	  is_inout = false;
6118 	}
6119 
6120       if (!allows_reg && allows_mem)
6121 	mark_addressable (TREE_VALUE (link));
6122 
6123       tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6124 			    is_inout ? is_gimple_min_lval : is_gimple_lvalue,
6125 			    fb_lvalue | fb_mayfail);
6126       if (tret == GS_ERROR)
6127 	{
6128 	  error ("invalid lvalue in asm output %d", i);
6129 	  ret = tret;
6130 	}
6131 
6132       /* If the constraint does not allow memory make sure we gimplify
6133          it to a register if it is not already but its base is.  This
6134 	 happens for complex and vector components.  */
6135       if (!allows_mem)
6136 	{
6137 	  tree op = TREE_VALUE (link);
6138 	  if (! is_gimple_val (op)
6139 	      && is_gimple_reg_type (TREE_TYPE (op))
6140 	      && is_gimple_reg (get_base_address (op)))
6141 	    {
6142 	      tree tem = create_tmp_reg (TREE_TYPE (op));
6143 	      tree ass;
6144 	      if (is_inout)
6145 		{
6146 		  ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
6147 				tem, unshare_expr (op));
6148 		  gimplify_and_add (ass, pre_p);
6149 		}
6150 	      ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
6151 	      gimplify_and_add (ass, post_p);
6152 
6153 	      TREE_VALUE (link) = tem;
6154 	      tret = GS_OK;
6155 	    }
6156 	}
6157 
6158       vec_safe_push (outputs, link);
6159       TREE_CHAIN (link) = NULL_TREE;
6160 
6161       if (is_inout)
6162 	{
6163 	  /* An input/output operand.  To give the optimizers more
6164 	     flexibility, split it into separate input and output
6165  	     operands.  */
6166 	  tree input;
6167 	  /* Buffer big enough to format a 32-bit UINT_MAX into.  */
6168 	  char buf[11];
6169 
6170 	  /* Turn the in/out constraint into an output constraint.  */
6171 	  char *p = xstrdup (constraint);
6172 	  p[0] = '=';
6173 	  TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
6174 
6175 	  /* And add a matching input constraint.  */
6176 	  if (allows_reg)
6177 	    {
6178 	      sprintf (buf, "%u", i);
6179 
6180 	      /* If there are multiple alternatives in the constraint,
6181 		 handle each of them individually.  Those that allow register
6182 		 will be replaced with operand number, the others will stay
6183 		 unchanged.  */
6184 	      if (strchr (p, ',') != NULL)
6185 		{
6186 		  size_t len = 0, buflen = strlen (buf);
6187 		  char *beg, *end, *str, *dst;
6188 
6189 		  for (beg = p + 1;;)
6190 		    {
6191 		      end = strchr (beg, ',');
6192 		      if (end == NULL)
6193 			end = strchr (beg, '\0');
6194 		      if ((size_t) (end - beg) < buflen)
6195 			len += buflen + 1;
6196 		      else
6197 			len += end - beg + 1;
6198 		      if (*end)
6199 			beg = end + 1;
6200 		      else
6201 			break;
6202 		    }
6203 
6204 		  str = (char *) alloca (len);
6205 		  for (beg = p + 1, dst = str;;)
6206 		    {
6207 		      const char *tem;
6208 		      bool mem_p, reg_p, inout_p;
6209 
6210 		      end = strchr (beg, ',');
6211 		      if (end)
6212 			*end = '\0';
6213 		      beg[-1] = '=';
6214 		      tem = beg - 1;
6215 		      parse_output_constraint (&tem, i, 0, 0,
6216 					       &mem_p, &reg_p, &inout_p);
6217 		      if (dst != str)
6218 			*dst++ = ',';
6219 		      if (reg_p)
6220 			{
6221 			  memcpy (dst, buf, buflen);
6222 			  dst += buflen;
6223 			}
6224 		      else
6225 			{
6226 			  if (end)
6227 			    len = end - beg;
6228 			  else
6229 			    len = strlen (beg);
6230 			  memcpy (dst, beg, len);
6231 			  dst += len;
6232 			}
6233 		      if (end)
6234 			beg = end + 1;
6235 		      else
6236 			break;
6237 		    }
6238 		  *dst = '\0';
6239 		  input = build_string (dst - str, str);
6240 		}
6241 	      else
6242 		input = build_string (strlen (buf), buf);
6243 	    }
6244 	  else
6245 	    input = build_string (constraint_len - 1, constraint + 1);
6246 
6247 	  free (p);
6248 
6249 	  input = build_tree_list (build_tree_list (NULL_TREE, input),
6250 				   unshare_expr (TREE_VALUE (link)));
6251 	  ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6252 	}
6253     }
6254 
6255   link_next = NULL_TREE;
6256   for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6257     {
6258       link_next = TREE_CHAIN (link);
6259       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6260       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6261 			      oconstraints, &allows_mem, &allows_reg);
6262 
6263       /* If we can't make copies, we can only accept memory.  */
6264       if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6265 	{
6266 	  if (allows_mem)
6267 	    allows_reg = 0;
6268 	  else
6269 	    {
6270 	      error ("impossible constraint in %<asm%>");
6271 	      error ("non-memory input %d must stay in memory", i);
6272 	      return GS_ERROR;
6273 	    }
6274 	}
6275 
6276       /* If the operand is a memory input, it should be an lvalue.  */
6277       if (!allows_reg && allows_mem)
6278 	{
6279 	  tree inputv = TREE_VALUE (link);
6280 	  STRIP_NOPS (inputv);
6281 	  if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6282 	      || TREE_CODE (inputv) == PREINCREMENT_EXPR
6283 	      || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6284 	      || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6285 	      || TREE_CODE (inputv) == MODIFY_EXPR)
6286 	    TREE_VALUE (link) = error_mark_node;
6287 	  tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6288 				is_gimple_lvalue, fb_lvalue | fb_mayfail);
6289 	  if (tret != GS_ERROR)
6290 	    {
6291 	      /* Unlike output operands, memory inputs are not guaranteed
6292 		 to be lvalues by the FE, and while the expressions are
6293 		 marked addressable there, if it is e.g. a statement
6294 		 expression, temporaries in it might not end up being
6295 		 addressable.  They might be already used in the IL and thus
6296 		 it is too late to make them addressable now though.  */
6297 	      tree x = TREE_VALUE (link);
6298 	      while (handled_component_p (x))
6299 		x = TREE_OPERAND (x, 0);
6300 	      if (TREE_CODE (x) == MEM_REF
6301 		  && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6302 		x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6303 	      if ((VAR_P (x)
6304 		   || TREE_CODE (x) == PARM_DECL
6305 		   || TREE_CODE (x) == RESULT_DECL)
6306 		  && !TREE_ADDRESSABLE (x)
6307 		  && is_gimple_reg (x))
6308 		{
6309 		  warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6310 					       input_location), 0,
6311 			      "memory input %d is not directly addressable",
6312 			      i);
6313 		  prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6314 		}
6315 	    }
6316 	  mark_addressable (TREE_VALUE (link));
6317 	  if (tret == GS_ERROR)
6318 	    {
6319 	      error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6320 			"memory input %d is not directly addressable", i);
6321 	      ret = tret;
6322 	    }
6323 	}
6324       else
6325 	{
6326 	  tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6327 				is_gimple_asm_val, fb_rvalue);
6328 	  if (tret == GS_ERROR)
6329 	    ret = tret;
6330 	}
6331 
6332       TREE_CHAIN (link) = NULL_TREE;
6333       vec_safe_push (inputs, link);
6334     }
6335 
6336   link_next = NULL_TREE;
6337   for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6338     {
6339       link_next = TREE_CHAIN (link);
6340       TREE_CHAIN (link) = NULL_TREE;
6341       vec_safe_push (clobbers, link);
6342     }
6343 
6344   link_next = NULL_TREE;
6345   for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6346     {
6347       link_next = TREE_CHAIN (link);
6348       TREE_CHAIN (link) = NULL_TREE;
6349       vec_safe_push (labels, link);
6350     }
6351 
6352   /* Do not add ASMs with errors to the gimple IL stream.  */
6353   if (ret != GS_ERROR)
6354     {
6355       stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6356 				   inputs, outputs, clobbers, labels);
6357 
6358       gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6359       gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6360 
6361       gimplify_seq_add_stmt (pre_p, stmt);
6362     }
6363 
6364   return ret;
6365 }
6366 
6367 /* Gimplify a CLEANUP_POINT_EXPR.  Currently this works by adding
6368    GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6369    gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6370    return to this function.
6371 
6372    FIXME should we complexify the prequeue handling instead?  Or use flags
6373    for all the cleanups and let the optimizer tighten them up?  The current
6374    code seems pretty fragile; it will break on a cleanup within any
6375    non-conditional nesting.  But any such nesting would be broken, anyway;
6376    we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6377    and continues out of it.  We can do that at the RTL level, though, so
6378    having an optimizer to tighten up try/finally regions would be a Good
6379    Thing.  */
6380 
6381 static enum gimplify_status
6382 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6383 {
6384   gimple_stmt_iterator iter;
6385   gimple_seq body_sequence = NULL;
6386 
6387   tree temp = voidify_wrapper_expr (*expr_p, NULL);
6388 
6389   /* We only care about the number of conditions between the innermost
6390      CLEANUP_POINT_EXPR and the cleanup.  So save and reset the count and
6391      any cleanups collected outside the CLEANUP_POINT_EXPR.  */
6392   int old_conds = gimplify_ctxp->conditions;
6393   gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6394   bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6395   gimplify_ctxp->conditions = 0;
6396   gimplify_ctxp->conditional_cleanups = NULL;
6397   gimplify_ctxp->in_cleanup_point_expr = true;
6398 
6399   gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6400 
6401   gimplify_ctxp->conditions = old_conds;
6402   gimplify_ctxp->conditional_cleanups = old_cleanups;
6403   gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6404 
6405   for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6406     {
6407       gimple *wce = gsi_stmt (iter);
6408 
6409       if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6410 	{
6411 	  if (gsi_one_before_end_p (iter))
6412 	    {
6413               /* Note that gsi_insert_seq_before and gsi_remove do not
6414                  scan operands, unlike some other sequence mutators.  */
6415 	      if (!gimple_wce_cleanup_eh_only (wce))
6416 		gsi_insert_seq_before_without_update (&iter,
6417 						      gimple_wce_cleanup (wce),
6418 						      GSI_SAME_STMT);
6419 	      gsi_remove (&iter, true);
6420 	      break;
6421 	    }
6422 	  else
6423 	    {
6424 	      gtry *gtry;
6425 	      gimple_seq seq;
6426 	      enum gimple_try_flags kind;
6427 
6428 	      if (gimple_wce_cleanup_eh_only (wce))
6429 		kind = GIMPLE_TRY_CATCH;
6430 	      else
6431 		kind = GIMPLE_TRY_FINALLY;
6432 	      seq = gsi_split_seq_after (iter);
6433 
6434 	      gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6435               /* Do not use gsi_replace here, as it may scan operands.
6436                  We want to do a simple structural modification only.  */
6437 	      gsi_set_stmt (&iter, gtry);
6438 	      iter = gsi_start (gtry->eval);
6439 	    }
6440 	}
6441       else
6442 	gsi_next (&iter);
6443     }
6444 
6445   gimplify_seq_add_seq (pre_p, body_sequence);
6446   if (temp)
6447     {
6448       *expr_p = temp;
6449       return GS_OK;
6450     }
6451   else
6452     {
6453       *expr_p = NULL;
6454       return GS_ALL_DONE;
6455     }
6456 }
6457 
6458 /* Insert a cleanup marker for gimplify_cleanup_point_expr.  CLEANUP
6459    is the cleanup action required.  EH_ONLY is true if the cleanup should
6460    only be executed if an exception is thrown, not on normal exit.
6461    If FORCE_UNCOND is true perform the cleanup unconditionally;  this is
6462    only valid for clobbers.  */
6463 
6464 static void
6465 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
6466 		     bool force_uncond = false)
6467 {
6468   gimple *wce;
6469   gimple_seq cleanup_stmts = NULL;
6470 
6471   /* Errors can result in improperly nested cleanups.  Which results in
6472      confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR.  */
6473   if (seen_error ())
6474     return;
6475 
6476   if (gimple_conditional_context ())
6477     {
6478       /* If we're in a conditional context, this is more complex.  We only
6479 	 want to run the cleanup if we actually ran the initialization that
6480 	 necessitates it, but we want to run it after the end of the
6481 	 conditional context.  So we wrap the try/finally around the
6482 	 condition and use a flag to determine whether or not to actually
6483 	 run the destructor.  Thus
6484 
6485 	   test ? f(A()) : 0
6486 
6487 	 becomes (approximately)
6488 
6489 	   flag = 0;
6490 	   try {
6491 	     if (test) { A::A(temp); flag = 1; val = f(temp); }
6492 	     else { val = 0; }
6493 	   } finally {
6494 	     if (flag) A::~A(temp);
6495 	   }
6496 	   val
6497       */
6498       if (force_uncond)
6499 	{
6500 	  gimplify_stmt (&cleanup, &cleanup_stmts);
6501 	  wce = gimple_build_wce (cleanup_stmts);
6502 	  gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6503 	}
6504       else
6505 	{
6506 	  tree flag = create_tmp_var (boolean_type_node, "cleanup");
6507 	  gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6508 	  gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6509 
6510 	  cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6511 	  gimplify_stmt (&cleanup, &cleanup_stmts);
6512 	  wce = gimple_build_wce (cleanup_stmts);
6513 
6514 	  gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6515 	  gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6516 	  gimplify_seq_add_stmt (pre_p, ftrue);
6517 
6518 	  /* Because of this manipulation, and the EH edges that jump
6519 	     threading cannot redirect, the temporary (VAR) will appear
6520 	     to be used uninitialized.  Don't warn.  */
6521 	  TREE_NO_WARNING (var) = 1;
6522 	}
6523     }
6524   else
6525     {
6526       gimplify_stmt (&cleanup, &cleanup_stmts);
6527       wce = gimple_build_wce (cleanup_stmts);
6528       gimple_wce_set_cleanup_eh_only (wce, eh_only);
6529       gimplify_seq_add_stmt (pre_p, wce);
6530     }
6531 }
6532 
6533 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR.  */
6534 
6535 static enum gimplify_status
6536 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6537 {
6538   tree targ = *expr_p;
6539   tree temp = TARGET_EXPR_SLOT (targ);
6540   tree init = TARGET_EXPR_INITIAL (targ);
6541   enum gimplify_status ret;
6542 
6543   bool unpoison_empty_seq = false;
6544   gimple_stmt_iterator unpoison_it;
6545 
6546   if (init)
6547     {
6548       tree cleanup = NULL_TREE;
6549 
6550       /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6551 	 to the temps list.  Handle also variable length TARGET_EXPRs.  */
6552       if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6553 	{
6554 	  if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6555 	    gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6556 	  gimplify_vla_decl (temp, pre_p);
6557 	}
6558       else
6559 	{
6560 	  /* Save location where we need to place unpoisoning.  It's possible
6561 	     that a variable will be converted to needs_to_live_in_memory.  */
6562 	  unpoison_it = gsi_last (*pre_p);
6563 	  unpoison_empty_seq = gsi_end_p (unpoison_it);
6564 
6565 	  gimple_add_tmp_var (temp);
6566 	}
6567 
6568       /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6569 	 expression is supposed to initialize the slot.  */
6570       if (VOID_TYPE_P (TREE_TYPE (init)))
6571 	ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6572       else
6573 	{
6574 	  tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6575 	  init = init_expr;
6576 	  ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6577 	  init = NULL;
6578 	  ggc_free (init_expr);
6579 	}
6580       if (ret == GS_ERROR)
6581 	{
6582 	  /* PR c++/28266 Make sure this is expanded only once. */
6583 	  TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6584 	  return GS_ERROR;
6585 	}
6586       if (init)
6587 	gimplify_and_add (init, pre_p);
6588 
6589       /* If needed, push the cleanup for the temp.  */
6590       if (TARGET_EXPR_CLEANUP (targ))
6591 	{
6592 	  if (CLEANUP_EH_ONLY (targ))
6593 	    gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6594 				 CLEANUP_EH_ONLY (targ), pre_p);
6595 	  else
6596 	    cleanup = TARGET_EXPR_CLEANUP (targ);
6597 	}
6598 
6599       /* Add a clobber for the temporary going out of scope, like
6600 	 gimplify_bind_expr.  */
6601       if (gimplify_ctxp->in_cleanup_point_expr
6602 	  && needs_to_live_in_memory (temp))
6603 	{
6604 	  if (flag_stack_reuse == SR_ALL)
6605 	    {
6606 	      tree clobber = build_constructor (TREE_TYPE (temp),
6607 						NULL);
6608 	      TREE_THIS_VOLATILE (clobber) = true;
6609 	      clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6610 	      gimple_push_cleanup (temp, clobber, false, pre_p, true);
6611 	    }
6612 	  if (asan_poisoned_variables
6613 	      && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
6614 	      && dbg_cnt (asan_use_after_scope)
6615 	      && !gimplify_omp_ctxp)
6616 	    {
6617 	      tree asan_cleanup = build_asan_poison_call_expr (temp);
6618 	      if (asan_cleanup)
6619 		{
6620 		  if (unpoison_empty_seq)
6621 		    unpoison_it = gsi_start (*pre_p);
6622 
6623 		  asan_poison_variable (temp, false, &unpoison_it,
6624 					unpoison_empty_seq);
6625 		  gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6626 		}
6627 	    }
6628 	}
6629       if (cleanup)
6630 	gimple_push_cleanup (temp, cleanup, false, pre_p);
6631 
6632       /* Only expand this once.  */
6633       TREE_OPERAND (targ, 3) = init;
6634       TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6635     }
6636   else
6637     /* We should have expanded this before.  */
6638     gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6639 
6640   *expr_p = temp;
6641   return GS_OK;
6642 }
6643 
6644 /* Gimplification of expression trees.  */
6645 
6646 /* Gimplify an expression which appears at statement context.  The
6647    corresponding GIMPLE statements are added to *SEQ_P.  If *SEQ_P is
6648    NULL, a new sequence is allocated.
6649 
6650    Return true if we actually added a statement to the queue.  */
6651 
6652 bool
6653 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6654 {
6655   gimple_seq_node last;
6656 
6657   last = gimple_seq_last (*seq_p);
6658   gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6659   return last != gimple_seq_last (*seq_p);
6660 }
6661 
6662 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6663    to CTX.  If entries already exist, force them to be some flavor of private.
6664    If there is no enclosing parallel, do nothing.  */
6665 
6666 void
6667 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6668 {
6669   splay_tree_node n;
6670 
6671   if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6672     return;
6673 
6674   do
6675     {
6676       n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6677       if (n != NULL)
6678 	{
6679 	  if (n->value & GOVD_SHARED)
6680 	    n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6681 	  else if (n->value & GOVD_MAP)
6682 	    n->value |= GOVD_MAP_TO_ONLY;
6683 	  else
6684 	    return;
6685 	}
6686       else if ((ctx->region_type & ORT_TARGET) != 0)
6687 	{
6688 	  if (ctx->target_map_scalars_firstprivate)
6689 	    omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6690 	  else
6691 	    omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6692 	}
6693       else if (ctx->region_type != ORT_WORKSHARE
6694 	       && ctx->region_type != ORT_SIMD
6695 	       && ctx->region_type != ORT_ACC
6696 	       && !(ctx->region_type & ORT_TARGET_DATA))
6697 	omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6698 
6699       ctx = ctx->outer_context;
6700     }
6701   while (ctx);
6702 }
6703 
6704 /* Similarly for each of the type sizes of TYPE.  */
6705 
6706 static void
6707 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6708 {
6709   if (type == NULL || type == error_mark_node)
6710     return;
6711   type = TYPE_MAIN_VARIANT (type);
6712 
6713   if (ctx->privatized_types->add (type))
6714     return;
6715 
6716   switch (TREE_CODE (type))
6717     {
6718     case INTEGER_TYPE:
6719     case ENUMERAL_TYPE:
6720     case BOOLEAN_TYPE:
6721     case REAL_TYPE:
6722     case FIXED_POINT_TYPE:
6723       omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6724       omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6725       break;
6726 
6727     case ARRAY_TYPE:
6728       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6729       omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6730       break;
6731 
6732     case RECORD_TYPE:
6733     case UNION_TYPE:
6734     case QUAL_UNION_TYPE:
6735       {
6736 	tree field;
6737 	for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6738 	  if (TREE_CODE (field) == FIELD_DECL)
6739 	    {
6740 	      omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6741 	      omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6742 	    }
6743       }
6744       break;
6745 
6746     case POINTER_TYPE:
6747     case REFERENCE_TYPE:
6748       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6749       break;
6750 
6751     default:
6752       break;
6753     }
6754 
6755   omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6756   omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6757   lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6758 }
6759 
6760 /* Add an entry for DECL in the OMP context CTX with FLAGS.  */
6761 
6762 static void
6763 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6764 {
6765   splay_tree_node n;
6766   unsigned int nflags;
6767   tree t;
6768 
6769   if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6770     return;
6771 
6772   /* Never elide decls whose type has TREE_ADDRESSABLE set.  This means
6773      there are constructors involved somewhere.  Exception is a shared clause,
6774      there is nothing privatized in that case.  */
6775   if ((flags & GOVD_SHARED) == 0
6776       && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6777 	  || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6778     flags |= GOVD_SEEN;
6779 
6780   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6781   if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6782     {
6783       /* We shouldn't be re-adding the decl with the same data
6784 	 sharing class.  */
6785       gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6786       nflags = n->value | flags;
6787       /* The only combination of data sharing classes we should see is
6788 	 FIRSTPRIVATE and LASTPRIVATE.  However, OpenACC permits
6789 	 reduction variables to be used in data sharing clauses.  */
6790       gcc_assert ((ctx->region_type & ORT_ACC) != 0
6791 		  || ((nflags & GOVD_DATA_SHARE_CLASS)
6792 		      == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6793 		  || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6794       n->value = nflags;
6795       return;
6796     }
6797 
6798   /* When adding a variable-sized variable, we have to handle all sorts
6799      of additional bits of data: the pointer replacement variable, and
6800      the parameters of the type.  */
6801   if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6802     {
6803       /* Add the pointer replacement variable as PRIVATE if the variable
6804 	 replacement is private, else FIRSTPRIVATE since we'll need the
6805 	 address of the original variable either for SHARED, or for the
6806 	 copy into or out of the context.  */
6807       if (!(flags & GOVD_LOCAL))
6808 	{
6809 	  if (flags & GOVD_MAP)
6810 	    nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6811 	  else if (flags & GOVD_PRIVATE)
6812 	    nflags = GOVD_PRIVATE;
6813 	  else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6814 		   && (flags & GOVD_FIRSTPRIVATE))
6815 	    nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6816 	  else
6817 	    nflags = GOVD_FIRSTPRIVATE;
6818 	  nflags |= flags & GOVD_SEEN;
6819 	  t = DECL_VALUE_EXPR (decl);
6820 	  gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6821 	  t = TREE_OPERAND (t, 0);
6822 	  gcc_assert (DECL_P (t));
6823 	  omp_add_variable (ctx, t, nflags);
6824 	}
6825 
6826       /* Add all of the variable and type parameters (which should have
6827 	 been gimplified to a formal temporary) as FIRSTPRIVATE.  */
6828       omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6829       omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6830       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6831 
6832       /* The variable-sized variable itself is never SHARED, only some form
6833 	 of PRIVATE.  The sharing would take place via the pointer variable
6834 	 which we remapped above.  */
6835       if (flags & GOVD_SHARED)
6836 	flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6837 		| (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6838 
6839       /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6840 	 alloca statement we generate for the variable, so make sure it
6841 	 is available.  This isn't automatically needed for the SHARED
6842 	 case, since we won't be allocating local storage then.
6843 	 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6844 	 in this case omp_notice_variable will be called later
6845 	 on when it is gimplified.  */
6846       else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6847 	       && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6848 	omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6849     }
6850   else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6851 	   && lang_hooks.decls.omp_privatize_by_reference (decl))
6852     {
6853       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6854 
6855       /* Similar to the direct variable sized case above, we'll need the
6856 	 size of references being privatized.  */
6857       if ((flags & GOVD_SHARED) == 0)
6858 	{
6859 	  t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6860 	  if (DECL_P (t))
6861 	    omp_notice_variable (ctx, t, true);
6862 	}
6863     }
6864 
6865   if (n != NULL)
6866     n->value |= flags;
6867   else
6868     splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6869 
6870   /* For reductions clauses in OpenACC loop directives, by default create a
6871      copy clause on the enclosing parallel construct for carrying back the
6872      results.  */
6873   if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6874     {
6875       struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6876       while (outer_ctx)
6877 	{
6878 	  n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6879 	  if (n != NULL)
6880 	    {
6881 	      /* Ignore local variables and explicitly declared clauses.  */
6882 	      if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6883 		break;
6884 	      else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6885 		{
6886 		  /* According to the OpenACC spec, such a reduction variable
6887 		     should already have a copy map on a kernels construct,
6888 		     verify that here.  */
6889 		  gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6890 			      && (n->value & GOVD_MAP));
6891 		}
6892 	      else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6893 		{
6894 		  /* Remove firstprivate and make it a copy map.  */
6895 		  n->value &= ~GOVD_FIRSTPRIVATE;
6896 		  n->value |= GOVD_MAP;
6897 		}
6898 	    }
6899 	  else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6900 	    {
6901 	      splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6902 				 GOVD_MAP | GOVD_SEEN);
6903 	      break;
6904 	    }
6905 	  outer_ctx = outer_ctx->outer_context;
6906 	}
6907     }
6908 }
6909 
6910 /* Notice a threadprivate variable DECL used in OMP context CTX.
6911    This just prints out diagnostics about threadprivate variable uses
6912    in untied tasks.  If DECL2 is non-NULL, prevent this warning
6913    on that variable.  */
6914 
6915 static bool
6916 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6917 				   tree decl2)
6918 {
6919   splay_tree_node n;
6920   struct gimplify_omp_ctx *octx;
6921 
6922   for (octx = ctx; octx; octx = octx->outer_context)
6923     if ((octx->region_type & ORT_TARGET) != 0)
6924       {
6925 	n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6926 	if (n == NULL)
6927 	  {
6928 	    error ("threadprivate variable %qE used in target region",
6929 		   DECL_NAME (decl));
6930 	    error_at (octx->location, "enclosing target region");
6931 	    splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6932 	  }
6933 	if (decl2)
6934 	  splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6935       }
6936 
6937   if (ctx->region_type != ORT_UNTIED_TASK)
6938     return false;
6939   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6940   if (n == NULL)
6941     {
6942       error ("threadprivate variable %qE used in untied task",
6943 	     DECL_NAME (decl));
6944       error_at (ctx->location, "enclosing task");
6945       splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6946     }
6947   if (decl2)
6948     splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6949   return false;
6950 }
6951 
6952 /* Return true if global var DECL is device resident.  */
6953 
6954 static bool
6955 device_resident_p (tree decl)
6956 {
6957   tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6958 
6959   if (!attr)
6960     return false;
6961 
6962   for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6963     {
6964       tree c = TREE_VALUE (t);
6965       if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6966 	return true;
6967     }
6968 
6969   return false;
6970 }
6971 
6972 /* Return true if DECL has an ACC DECLARE attribute.  */
6973 
6974 static bool
6975 is_oacc_declared (tree decl)
6976 {
6977   tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
6978   tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
6979   return declared != NULL_TREE;
6980 }
6981 
6982 /* Determine outer default flags for DECL mentioned in an OMP region
6983    but not declared in an enclosing clause.
6984 
6985    ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6986    remapped firstprivate instead of shared.  To some extent this is
6987    addressed in omp_firstprivatize_type_sizes, but not
6988    effectively.  */
6989 
6990 static unsigned
6991 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
6992 		    bool in_code, unsigned flags)
6993 {
6994   enum omp_clause_default_kind default_kind = ctx->default_kind;
6995   enum omp_clause_default_kind kind;
6996 
6997   kind = lang_hooks.decls.omp_predetermined_sharing (decl);
6998   if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6999     default_kind = kind;
7000 
7001   switch (default_kind)
7002     {
7003     case OMP_CLAUSE_DEFAULT_NONE:
7004       {
7005 	const char *rtype;
7006 
7007 	if (ctx->region_type & ORT_PARALLEL)
7008 	  rtype = "parallel";
7009 	else if (ctx->region_type & ORT_TASK)
7010 	  rtype = "task";
7011 	else if (ctx->region_type & ORT_TEAMS)
7012 	  rtype = "teams";
7013 	else
7014 	  gcc_unreachable ();
7015 
7016 	error ("%qE not specified in enclosing %qs",
7017 	       DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
7018 	error_at (ctx->location, "enclosing %qs", rtype);
7019       }
7020       /* FALLTHRU */
7021     case OMP_CLAUSE_DEFAULT_SHARED:
7022       flags |= GOVD_SHARED;
7023       break;
7024     case OMP_CLAUSE_DEFAULT_PRIVATE:
7025       flags |= GOVD_PRIVATE;
7026       break;
7027     case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
7028       flags |= GOVD_FIRSTPRIVATE;
7029       break;
7030     case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7031       /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED.  */
7032       gcc_assert ((ctx->region_type & ORT_TASK) != 0);
7033       if (struct gimplify_omp_ctx *octx = ctx->outer_context)
7034 	{
7035 	  omp_notice_variable (octx, decl, in_code);
7036 	  for (; octx; octx = octx->outer_context)
7037 	    {
7038 	      splay_tree_node n2;
7039 
7040 	      n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
7041 	      if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
7042 		  && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
7043 		continue;
7044 	      if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7045 		{
7046 		  flags |= GOVD_FIRSTPRIVATE;
7047 		  goto found_outer;
7048 		}
7049 	      if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7050 		{
7051 		  flags |= GOVD_SHARED;
7052 		  goto found_outer;
7053 		}
7054 	    }
7055 	}
7056 
7057       if (TREE_CODE (decl) == PARM_DECL
7058 	  || (!is_global_var (decl)
7059 	      && DECL_CONTEXT (decl) == current_function_decl))
7060 	flags |= GOVD_FIRSTPRIVATE;
7061       else
7062 	flags |= GOVD_SHARED;
7063     found_outer:
7064       break;
7065 
7066     default:
7067       gcc_unreachable ();
7068     }
7069 
7070   return flags;
7071 }
7072 
7073 
7074 /* Determine outer default flags for DECL mentioned in an OACC region
7075    but not declared in an enclosing clause.  */
7076 
7077 static unsigned
7078 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7079 {
7080   const char *rkind;
7081   bool on_device = false;
7082   bool declared = is_oacc_declared (decl);
7083   tree type = TREE_TYPE (decl);
7084 
7085   if (lang_hooks.decls.omp_privatize_by_reference (decl))
7086     type = TREE_TYPE (type);
7087 
7088   if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7089       && is_global_var (decl)
7090       && device_resident_p (decl))
7091     {
7092       on_device = true;
7093       flags |= GOVD_MAP_TO_ONLY;
7094     }
7095 
7096   switch (ctx->region_type)
7097     {
7098     case ORT_ACC_KERNELS:
7099       rkind = "kernels";
7100 
7101       if (AGGREGATE_TYPE_P (type))
7102 	{
7103 	  /* Aggregates default to 'present_or_copy', or 'present'.  */
7104 	  if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7105 	    flags |= GOVD_MAP;
7106 	  else
7107 	    flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7108 	}
7109       else
7110 	/* Scalars default to 'copy'.  */
7111 	flags |= GOVD_MAP | GOVD_MAP_FORCE;
7112 
7113       break;
7114 
7115     case ORT_ACC_PARALLEL:
7116       rkind = "parallel";
7117 
7118       if (on_device || declared)
7119 	flags |= GOVD_MAP;
7120       else if (AGGREGATE_TYPE_P (type))
7121 	{
7122 	  /* Aggregates default to 'present_or_copy', or 'present'.  */
7123 	  if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7124 	    flags |= GOVD_MAP;
7125 	  else
7126 	    flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7127 	}
7128       else
7129 	/* Scalars default to 'firstprivate'.  */
7130 	flags |= GOVD_FIRSTPRIVATE;
7131 
7132       break;
7133 
7134     default:
7135       gcc_unreachable ();
7136     }
7137 
7138   if (DECL_ARTIFICIAL (decl))
7139     ; /* We can get compiler-generated decls, and should not complain
7140 	 about them.  */
7141   else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7142     {
7143       error ("%qE not specified in enclosing OpenACC %qs construct",
7144 	     DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7145       inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7146     }
7147   else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7148     ; /* Handled above.  */
7149   else
7150     gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7151 
7152   return flags;
7153 }
7154 
7155 /* Record the fact that DECL was used within the OMP context CTX.
7156    IN_CODE is true when real code uses DECL, and false when we should
7157    merely emit default(none) errors.  Return true if DECL is going to
7158    be remapped and thus DECL shouldn't be gimplified into its
7159    DECL_VALUE_EXPR (if any).  */
7160 
7161 static bool
7162 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7163 {
7164   splay_tree_node n;
7165   unsigned flags = in_code ? GOVD_SEEN : 0;
7166   bool ret = false, shared;
7167 
7168   if (error_operand_p (decl))
7169     return false;
7170 
7171   if (ctx->region_type == ORT_NONE)
7172     return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7173 
7174   if (is_global_var (decl))
7175     {
7176       /* Threadprivate variables are predetermined.  */
7177       if (DECL_THREAD_LOCAL_P (decl))
7178 	return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7179 
7180       if (DECL_HAS_VALUE_EXPR_P (decl))
7181 	{
7182 	  tree value = get_base_address (DECL_VALUE_EXPR (decl));
7183 
7184 	  if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7185 	    return omp_notice_threadprivate_variable (ctx, decl, value);
7186 	}
7187 
7188       if (gimplify_omp_ctxp->outer_context == NULL
7189 	  && VAR_P (decl)
7190 	  && oacc_get_fn_attrib (current_function_decl))
7191 	{
7192 	  location_t loc = DECL_SOURCE_LOCATION (decl);
7193 
7194 	  if (lookup_attribute ("omp declare target link",
7195 				DECL_ATTRIBUTES (decl)))
7196 	    {
7197 	      error_at (loc,
7198 			"%qE with %<link%> clause used in %<routine%> function",
7199 			DECL_NAME (decl));
7200 	      return false;
7201 	    }
7202 	  else if (!lookup_attribute ("omp declare target",
7203 				      DECL_ATTRIBUTES (decl)))
7204 	    {
7205 	      error_at (loc,
7206 			"%qE requires a %<declare%> directive for use "
7207 			"in a %<routine%> function", DECL_NAME (decl));
7208 	      return false;
7209 	    }
7210 	}
7211     }
7212 
7213   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7214   if ((ctx->region_type & ORT_TARGET) != 0)
7215     {
7216       ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7217       if (n == NULL)
7218 	{
7219 	  unsigned nflags = flags;
7220 	  if (ctx->target_map_pointers_as_0len_arrays
7221 	      || ctx->target_map_scalars_firstprivate)
7222 	    {
7223 	      bool is_declare_target = false;
7224 	      bool is_scalar = false;
7225 	      if (is_global_var (decl)
7226 		  && varpool_node::get_create (decl)->offloadable)
7227 		{
7228 		  struct gimplify_omp_ctx *octx;
7229 		  for (octx = ctx->outer_context;
7230 		       octx; octx = octx->outer_context)
7231 		    {
7232 		      n = splay_tree_lookup (octx->variables,
7233 					     (splay_tree_key)decl);
7234 		      if (n
7235 			  && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7236 			  && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7237 			break;
7238 		    }
7239 		  is_declare_target = octx == NULL;
7240 		}
7241 	      if (!is_declare_target && ctx->target_map_scalars_firstprivate)
7242 		is_scalar = lang_hooks.decls.omp_scalar_p (decl);
7243 	      if (is_declare_target)
7244 		;
7245 	      else if (ctx->target_map_pointers_as_0len_arrays
7246 		       && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7247 			   || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7248 			       && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7249 				  == POINTER_TYPE)))
7250 		nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
7251 	      else if (is_scalar)
7252 		nflags |= GOVD_FIRSTPRIVATE;
7253 	    }
7254 
7255 	  struct gimplify_omp_ctx *octx = ctx->outer_context;
7256 	  if ((ctx->region_type & ORT_ACC) && octx)
7257 	    {
7258 	      /* Look in outer OpenACC contexts, to see if there's a
7259 		 data attribute for this variable.  */
7260 	      omp_notice_variable (octx, decl, in_code);
7261 
7262 	      for (; octx; octx = octx->outer_context)
7263 		{
7264 		  if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7265 		    break;
7266 		  splay_tree_node n2
7267 		    = splay_tree_lookup (octx->variables,
7268 					 (splay_tree_key) decl);
7269 		  if (n2)
7270 		    {
7271 		      if (octx->region_type == ORT_ACC_HOST_DATA)
7272 		        error ("variable %qE declared in enclosing "
7273 			       "%<host_data%> region", DECL_NAME (decl));
7274 		      nflags |= GOVD_MAP;
7275 		      if (octx->region_type == ORT_ACC_DATA
7276 			  && (n2->value & GOVD_MAP_0LEN_ARRAY))
7277 			nflags |= GOVD_MAP_0LEN_ARRAY;
7278 		      goto found_outer;
7279 		    }
7280 		}
7281 	    }
7282 
7283 	  {
7284 	    tree type = TREE_TYPE (decl);
7285 
7286 	    if (nflags == flags
7287 		&& gimplify_omp_ctxp->target_firstprivatize_array_bases
7288 		&& lang_hooks.decls.omp_privatize_by_reference (decl))
7289 	      type = TREE_TYPE (type);
7290 	    if (nflags == flags
7291 		&& !lang_hooks.types.omp_mappable_type (type))
7292 	      {
7293 		error ("%qD referenced in target region does not have "
7294 		       "a mappable type", decl);
7295 		nflags |= GOVD_MAP | GOVD_EXPLICIT;
7296 	      }
7297 	    else if (nflags == flags)
7298 	      {
7299 		if ((ctx->region_type & ORT_ACC) != 0)
7300 		  nflags = oacc_default_clause (ctx, decl, flags);
7301 		else
7302 		  nflags |= GOVD_MAP;
7303 	      }
7304 	  }
7305 	found_outer:
7306 	  omp_add_variable (ctx, decl, nflags);
7307 	}
7308       else
7309 	{
7310 	  /* If nothing changed, there's nothing left to do.  */
7311 	  if ((n->value & flags) == flags)
7312 	    return ret;
7313 	  flags |= n->value;
7314 	  n->value = flags;
7315 	}
7316       goto do_outer;
7317     }
7318 
7319   if (n == NULL)
7320     {
7321       if (ctx->region_type == ORT_WORKSHARE
7322 	  || ctx->region_type == ORT_SIMD
7323 	  || ctx->region_type == ORT_ACC
7324 	  || (ctx->region_type & ORT_TARGET_DATA) != 0)
7325 	goto do_outer;
7326 
7327       flags = omp_default_clause (ctx, decl, in_code, flags);
7328 
7329       if ((flags & GOVD_PRIVATE)
7330 	  && lang_hooks.decls.omp_private_outer_ref (decl))
7331 	flags |= GOVD_PRIVATE_OUTER_REF;
7332 
7333       omp_add_variable (ctx, decl, flags);
7334 
7335       shared = (flags & GOVD_SHARED) != 0;
7336       ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7337       goto do_outer;
7338     }
7339 
7340   if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7341       && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7342       && DECL_SIZE (decl))
7343     {
7344       if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7345 	{
7346 	  splay_tree_node n2;
7347 	  tree t = DECL_VALUE_EXPR (decl);
7348 	  gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7349 	  t = TREE_OPERAND (t, 0);
7350 	  gcc_assert (DECL_P (t));
7351 	  n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7352 	  n2->value |= GOVD_SEEN;
7353 	}
7354       else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7355 	       && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7356 	       && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7357 		   != INTEGER_CST))
7358 	{
7359 	  splay_tree_node n2;
7360 	  tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7361 	  gcc_assert (DECL_P (t));
7362 	  n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7363 	  if (n2)
7364 	    omp_notice_variable (ctx, t, true);
7365 	}
7366     }
7367 
7368   shared = ((flags | n->value) & GOVD_SHARED) != 0;
7369   ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7370 
7371   /* If nothing changed, there's nothing left to do.  */
7372   if ((n->value & flags) == flags)
7373     return ret;
7374   flags |= n->value;
7375   n->value = flags;
7376 
7377  do_outer:
7378   /* If the variable is private in the current context, then we don't
7379      need to propagate anything to an outer context.  */
7380   if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7381     return ret;
7382   if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7383       == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7384     return ret;
7385   if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7386 		| GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7387       == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7388     return ret;
7389   if (ctx->outer_context
7390       && omp_notice_variable (ctx->outer_context, decl, in_code))
7391     return true;
7392   return ret;
7393 }
7394 
7395 /* Verify that DECL is private within CTX.  If there's specific information
7396    to the contrary in the innermost scope, generate an error.  */
7397 
7398 static bool
7399 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7400 {
7401   splay_tree_node n;
7402 
7403   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7404   if (n != NULL)
7405     {
7406       if (n->value & GOVD_SHARED)
7407 	{
7408 	  if (ctx == gimplify_omp_ctxp)
7409 	    {
7410 	      if (simd)
7411 		error ("iteration variable %qE is predetermined linear",
7412 		       DECL_NAME (decl));
7413 	      else
7414 		error ("iteration variable %qE should be private",
7415 		       DECL_NAME (decl));
7416 	      n->value = GOVD_PRIVATE;
7417 	      return true;
7418 	    }
7419 	  else
7420 	    return false;
7421 	}
7422       else if ((n->value & GOVD_EXPLICIT) != 0
7423 	       && (ctx == gimplify_omp_ctxp
7424 		   || (ctx->region_type == ORT_COMBINED_PARALLEL
7425 		       && gimplify_omp_ctxp->outer_context == ctx)))
7426 	{
7427 	  if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7428 	    error ("iteration variable %qE should not be firstprivate",
7429 		   DECL_NAME (decl));
7430 	  else if ((n->value & GOVD_REDUCTION) != 0)
7431 	    error ("iteration variable %qE should not be reduction",
7432 		   DECL_NAME (decl));
7433 	  else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
7434 	    error ("iteration variable %qE should not be linear",
7435 		   DECL_NAME (decl));
7436 	  else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
7437 	    error ("iteration variable %qE should not be lastprivate",
7438 		   DECL_NAME (decl));
7439 	  else if (simd && (n->value & GOVD_PRIVATE) != 0)
7440 	    error ("iteration variable %qE should not be private",
7441 		   DECL_NAME (decl));
7442 	  else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
7443 	    error ("iteration variable %qE is predetermined linear",
7444 		   DECL_NAME (decl));
7445 	}
7446       return (ctx == gimplify_omp_ctxp
7447 	      || (ctx->region_type == ORT_COMBINED_PARALLEL
7448 		  && gimplify_omp_ctxp->outer_context == ctx));
7449     }
7450 
7451   if (ctx->region_type != ORT_WORKSHARE
7452       && ctx->region_type != ORT_SIMD
7453       && ctx->region_type != ORT_ACC)
7454     return false;
7455   else if (ctx->outer_context)
7456     return omp_is_private (ctx->outer_context, decl, simd);
7457   return false;
7458 }
7459 
7460 /* Return true if DECL is private within a parallel region
7461    that binds to the current construct's context or in parallel
7462    region's REDUCTION clause.  */
7463 
7464 static bool
7465 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7466 {
7467   splay_tree_node n;
7468 
7469   do
7470     {
7471       ctx = ctx->outer_context;
7472       if (ctx == NULL)
7473 	{
7474 	  if (is_global_var (decl))
7475 	    return false;
7476 
7477 	  /* References might be private, but might be shared too,
7478 	     when checking for copyprivate, assume they might be
7479 	     private, otherwise assume they might be shared.  */
7480 	  if (copyprivate)
7481 	    return true;
7482 
7483 	  if (lang_hooks.decls.omp_privatize_by_reference (decl))
7484 	    return false;
7485 
7486 	  /* Treat C++ privatized non-static data members outside
7487 	     of the privatization the same.  */
7488 	  if (omp_member_access_dummy_var (decl))
7489 	    return false;
7490 
7491 	  return true;
7492 	}
7493 
7494       n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7495 
7496       if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7497 	  && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7498 	continue;
7499 
7500       if (n != NULL)
7501 	{
7502 	  if ((n->value & GOVD_LOCAL) != 0
7503 	      && omp_member_access_dummy_var (decl))
7504 	    return false;
7505 	  return (n->value & GOVD_SHARED) == 0;
7506 	}
7507     }
7508   while (ctx->region_type == ORT_WORKSHARE
7509 	 || ctx->region_type == ORT_SIMD
7510 	 || ctx->region_type == ORT_ACC);
7511   return false;
7512 }
7513 
7514 /* Callback for walk_tree to find a DECL_EXPR for the given DECL.  */
7515 
7516 static tree
7517 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7518 {
7519   tree t = *tp;
7520 
7521   /* If this node has been visited, unmark it and keep looking.  */
7522   if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7523     return t;
7524 
7525   if (IS_TYPE_OR_DECL_P (t))
7526     *walk_subtrees = 0;
7527   return NULL_TREE;
7528 }
7529 
7530 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7531    and previous omp contexts.  */
7532 
7533 static void
7534 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7535 			   enum omp_region_type region_type,
7536 			   enum tree_code code)
7537 {
7538   struct gimplify_omp_ctx *ctx, *outer_ctx;
7539   tree c;
7540   hash_map<tree, tree> *struct_map_to_clause = NULL;
7541   tree *prev_list_p = NULL;
7542 
7543   ctx = new_omp_context (region_type);
7544   outer_ctx = ctx->outer_context;
7545   if (code == OMP_TARGET)
7546     {
7547       if (!lang_GNU_Fortran ())
7548 	ctx->target_map_pointers_as_0len_arrays = true;
7549       ctx->target_map_scalars_firstprivate = true;
7550     }
7551   if (!lang_GNU_Fortran ())
7552     switch (code)
7553       {
7554       case OMP_TARGET:
7555       case OMP_TARGET_DATA:
7556       case OMP_TARGET_ENTER_DATA:
7557       case OMP_TARGET_EXIT_DATA:
7558       case OACC_DECLARE:
7559       case OACC_HOST_DATA:
7560 	ctx->target_firstprivatize_array_bases = true;
7561       default:
7562 	break;
7563       }
7564 
7565   while ((c = *list_p) != NULL)
7566     {
7567       bool remove = false;
7568       bool notice_outer = true;
7569       const char *check_non_private = NULL;
7570       unsigned int flags;
7571       tree decl;
7572 
7573       switch (OMP_CLAUSE_CODE (c))
7574 	{
7575 	case OMP_CLAUSE_PRIVATE:
7576 	  flags = GOVD_PRIVATE | GOVD_EXPLICIT;
7577 	  if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
7578 	    {
7579 	      flags |= GOVD_PRIVATE_OUTER_REF;
7580 	      OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
7581 	    }
7582 	  else
7583 	    notice_outer = false;
7584 	  goto do_add;
7585 	case OMP_CLAUSE_SHARED:
7586 	  flags = GOVD_SHARED | GOVD_EXPLICIT;
7587 	  goto do_add;
7588 	case OMP_CLAUSE_FIRSTPRIVATE:
7589 	  flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7590 	  check_non_private = "firstprivate";
7591 	  goto do_add;
7592 	case OMP_CLAUSE_LASTPRIVATE:
7593 	  flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
7594 	  check_non_private = "lastprivate";
7595 	  decl = OMP_CLAUSE_DECL (c);
7596 	  if (error_operand_p (decl))
7597 	    goto do_add;
7598 	  else if (outer_ctx
7599 		   && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
7600 		       || outer_ctx->region_type == ORT_COMBINED_TEAMS)
7601 		   && splay_tree_lookup (outer_ctx->variables,
7602 					 (splay_tree_key) decl) == NULL)
7603 	    {
7604 	      omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
7605 	      if (outer_ctx->outer_context)
7606 		omp_notice_variable (outer_ctx->outer_context, decl, true);
7607 	    }
7608 	  else if (outer_ctx
7609 		   && (outer_ctx->region_type & ORT_TASK) != 0
7610 		   && outer_ctx->combined_loop
7611 		   && splay_tree_lookup (outer_ctx->variables,
7612 					 (splay_tree_key) decl) == NULL)
7613 	    {
7614 	      omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7615 	      if (outer_ctx->outer_context)
7616 		omp_notice_variable (outer_ctx->outer_context, decl, true);
7617 	    }
7618 	  else if (outer_ctx
7619 		   && (outer_ctx->region_type == ORT_WORKSHARE
7620 		       || outer_ctx->region_type == ORT_ACC)
7621 		   && outer_ctx->combined_loop
7622 		   && splay_tree_lookup (outer_ctx->variables,
7623 					 (splay_tree_key) decl) == NULL
7624 		   && !omp_check_private (outer_ctx, decl, false))
7625 	    {
7626 	      omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7627 	      if (outer_ctx->outer_context
7628 		  && (outer_ctx->outer_context->region_type
7629 		      == ORT_COMBINED_PARALLEL)
7630 		  && splay_tree_lookup (outer_ctx->outer_context->variables,
7631 					(splay_tree_key) decl) == NULL)
7632 		{
7633 		  struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
7634 		  omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
7635 		  if (octx->outer_context)
7636 		    {
7637 		      octx = octx->outer_context;
7638 		      if (octx->region_type == ORT_WORKSHARE
7639 			  && octx->combined_loop
7640 			  && splay_tree_lookup (octx->variables,
7641 						(splay_tree_key) decl) == NULL
7642 			  && !omp_check_private (octx, decl, false))
7643 			{
7644 			  omp_add_variable (octx, decl,
7645 					    GOVD_LASTPRIVATE | GOVD_SEEN);
7646 			  octx = octx->outer_context;
7647 			  if (octx
7648 			      && octx->region_type == ORT_COMBINED_TEAMS
7649 			      && (splay_tree_lookup (octx->variables,
7650 						     (splay_tree_key) decl)
7651 				  == NULL))
7652 			    {
7653 			      omp_add_variable (octx, decl,
7654 						GOVD_SHARED | GOVD_SEEN);
7655 			      octx = octx->outer_context;
7656 			    }
7657 			}
7658 		      if (octx)
7659 			omp_notice_variable (octx, decl, true);
7660 		    }
7661 		}
7662 	      else if (outer_ctx->outer_context)
7663 		omp_notice_variable (outer_ctx->outer_context, decl, true);
7664 	    }
7665 	  goto do_add;
7666 	case OMP_CLAUSE_REDUCTION:
7667 	  flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
7668 	  /* OpenACC permits reductions on private variables.  */
7669 	  if (!(region_type & ORT_ACC))
7670 	    check_non_private = "reduction";
7671 	  decl = OMP_CLAUSE_DECL (c);
7672 	  if (TREE_CODE (decl) == MEM_REF)
7673 	    {
7674 	      tree type = TREE_TYPE (decl);
7675 	      if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
7676 				 NULL, is_gimple_val, fb_rvalue, false)
7677 		  == GS_ERROR)
7678 		{
7679 		  remove = true;
7680 		  break;
7681 		}
7682 	      tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7683 	      if (DECL_P (v))
7684 		{
7685 		  omp_firstprivatize_variable (ctx, v);
7686 		  omp_notice_variable (ctx, v, true);
7687 		}
7688 	      decl = TREE_OPERAND (decl, 0);
7689 	      if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
7690 		{
7691 		  if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
7692 				     NULL, is_gimple_val, fb_rvalue, false)
7693 		      == GS_ERROR)
7694 		    {
7695 		      remove = true;
7696 		      break;
7697 		    }
7698 		  v = TREE_OPERAND (decl, 1);
7699 		  if (DECL_P (v))
7700 		    {
7701 		      omp_firstprivatize_variable (ctx, v);
7702 		      omp_notice_variable (ctx, v, true);
7703 		    }
7704 		  decl = TREE_OPERAND (decl, 0);
7705 		}
7706 	      if (TREE_CODE (decl) == ADDR_EXPR
7707 		  || TREE_CODE (decl) == INDIRECT_REF)
7708 		decl = TREE_OPERAND (decl, 0);
7709 	    }
7710 	  goto do_add_decl;
7711 	case OMP_CLAUSE_LINEAR:
7712 	  if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
7713 			     is_gimple_val, fb_rvalue) == GS_ERROR)
7714 	    {
7715 	      remove = true;
7716 	      break;
7717 	    }
7718 	  else
7719 	    {
7720 	      if (code == OMP_SIMD
7721 		  && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7722 		{
7723 		  struct gimplify_omp_ctx *octx = outer_ctx;
7724 		  if (octx
7725 		      && octx->region_type == ORT_WORKSHARE
7726 		      && octx->combined_loop
7727 		      && !octx->distribute)
7728 		    {
7729 		      if (octx->outer_context
7730 			  && (octx->outer_context->region_type
7731 			      == ORT_COMBINED_PARALLEL))
7732 			octx = octx->outer_context->outer_context;
7733 		      else
7734 			octx = octx->outer_context;
7735 		    }
7736 		  if (octx
7737 		      && octx->region_type == ORT_WORKSHARE
7738 		      && octx->combined_loop
7739 		      && octx->distribute)
7740 		    {
7741 		      error_at (OMP_CLAUSE_LOCATION (c),
7742 				"%<linear%> clause for variable other than "
7743 				"loop iterator specified on construct "
7744 				"combined with %<distribute%>");
7745 		      remove = true;
7746 		      break;
7747 		    }
7748 		}
7749 	      /* For combined #pragma omp parallel for simd, need to put
7750 		 lastprivate and perhaps firstprivate too on the
7751 		 parallel.  Similarly for #pragma omp for simd.  */
7752 	      struct gimplify_omp_ctx *octx = outer_ctx;
7753 	      decl = NULL_TREE;
7754 	      do
7755 		{
7756 		  if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7757 		      && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7758 		    break;
7759 		  decl = OMP_CLAUSE_DECL (c);
7760 		  if (error_operand_p (decl))
7761 		    {
7762 		      decl = NULL_TREE;
7763 		      break;
7764 		    }
7765 		  flags = GOVD_SEEN;
7766 		  if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7767 		    flags |= GOVD_FIRSTPRIVATE;
7768 		  if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7769 		    flags |= GOVD_LASTPRIVATE;
7770 		  if (octx
7771 		      && octx->region_type == ORT_WORKSHARE
7772 		      && octx->combined_loop)
7773 		    {
7774 		      if (octx->outer_context
7775 			  && (octx->outer_context->region_type
7776 			      == ORT_COMBINED_PARALLEL))
7777 			octx = octx->outer_context;
7778 		      else if (omp_check_private (octx, decl, false))
7779 			break;
7780 		    }
7781 		  else if (octx
7782 			   && (octx->region_type & ORT_TASK) != 0
7783 			   && octx->combined_loop)
7784 		    ;
7785 		  else if (octx
7786 			   && octx->region_type == ORT_COMBINED_PARALLEL
7787 			   && ctx->region_type == ORT_WORKSHARE
7788 			   && octx == outer_ctx)
7789 		    flags = GOVD_SEEN | GOVD_SHARED;
7790 		  else if (octx
7791 			   && octx->region_type == ORT_COMBINED_TEAMS)
7792 		    flags = GOVD_SEEN | GOVD_SHARED;
7793 		  else if (octx
7794 			   && octx->region_type == ORT_COMBINED_TARGET)
7795 		    {
7796 		      flags &= ~GOVD_LASTPRIVATE;
7797 		      if (flags == GOVD_SEEN)
7798 			break;
7799 		    }
7800 		  else
7801 		    break;
7802 		  splay_tree_node on
7803 		    = splay_tree_lookup (octx->variables,
7804 					 (splay_tree_key) decl);
7805 		  if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
7806 		    {
7807 		      octx = NULL;
7808 		      break;
7809 		    }
7810 		  omp_add_variable (octx, decl, flags);
7811 		  if (octx->outer_context == NULL)
7812 		    break;
7813 		  octx = octx->outer_context;
7814 		}
7815 	      while (1);
7816 	      if (octx
7817 		  && decl
7818 		  && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7819 		      || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7820 		omp_notice_variable (octx, decl, true);
7821 	    }
7822 	  flags = GOVD_LINEAR | GOVD_EXPLICIT;
7823 	  if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7824 	      && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7825 	    {
7826 	      notice_outer = false;
7827 	      flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7828 	    }
7829 	  goto do_add;
7830 
7831 	case OMP_CLAUSE_MAP:
7832 	  decl = OMP_CLAUSE_DECL (c);
7833 	  if (error_operand_p (decl))
7834 	    remove = true;
7835 	  switch (code)
7836 	    {
7837 	    case OMP_TARGET:
7838 	      break;
7839 	    case OACC_DATA:
7840 	      if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
7841 		break;
7842 	      /* FALLTHRU */
7843 	    case OMP_TARGET_DATA:
7844 	    case OMP_TARGET_ENTER_DATA:
7845 	    case OMP_TARGET_EXIT_DATA:
7846 	    case OACC_ENTER_DATA:
7847 	    case OACC_EXIT_DATA:
7848 	    case OACC_HOST_DATA:
7849 	      if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7850 		  || (OMP_CLAUSE_MAP_KIND (c)
7851 		      == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7852 		/* For target {,enter ,exit }data only the array slice is
7853 		   mapped, but not the pointer to it.  */
7854 		remove = true;
7855 	      break;
7856 	    default:
7857 	      break;
7858 	    }
7859 	  if (remove)
7860 	    break;
7861 	  if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
7862 	    {
7863 	      struct gimplify_omp_ctx *octx;
7864 	      for (octx = outer_ctx; octx; octx = octx->outer_context)
7865 	        {
7866 		  if (octx->region_type != ORT_ACC_HOST_DATA)
7867 		    break;
7868 		  splay_tree_node n2
7869 		    = splay_tree_lookup (octx->variables,
7870 					 (splay_tree_key) decl);
7871 		  if (n2)
7872 		    error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
7873 			      "declared in enclosing %<host_data%> region",
7874 			      DECL_NAME (decl));
7875 		}
7876 	    }
7877 	  if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7878 	    OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7879 				  : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7880 	  if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7881 			     NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7882 	    {
7883 	      remove = true;
7884 	      break;
7885 	    }
7886 	  else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7887 		    || (OMP_CLAUSE_MAP_KIND (c)
7888 			== GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7889 		   && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
7890 	    {
7891 	      OMP_CLAUSE_SIZE (c)
7892 		= get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
7893 					   false);
7894 	      omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
7895 				GOVD_FIRSTPRIVATE | GOVD_SEEN);
7896 	    }
7897 	  if (!DECL_P (decl))
7898 	    {
7899 	      tree d = decl, *pd;
7900 	      if (TREE_CODE (d) == ARRAY_REF)
7901 		{
7902 		  while (TREE_CODE (d) == ARRAY_REF)
7903 		    d = TREE_OPERAND (d, 0);
7904 		  if (TREE_CODE (d) == COMPONENT_REF
7905 		      && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
7906 		    decl = d;
7907 		}
7908 	      pd = &OMP_CLAUSE_DECL (c);
7909 	      if (d == decl
7910 		  && TREE_CODE (decl) == INDIRECT_REF
7911 		  && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7912 		  && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7913 		      == REFERENCE_TYPE))
7914 		{
7915 		  pd = &TREE_OPERAND (decl, 0);
7916 		  decl = TREE_OPERAND (decl, 0);
7917 		}
7918 	      if (TREE_CODE (decl) == COMPONENT_REF)
7919 		{
7920 		  while (TREE_CODE (decl) == COMPONENT_REF)
7921 		    decl = TREE_OPERAND (decl, 0);
7922 		  if (TREE_CODE (decl) == INDIRECT_REF
7923 		      && DECL_P (TREE_OPERAND (decl, 0))
7924 		      && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7925 			  == REFERENCE_TYPE))
7926 		    decl = TREE_OPERAND (decl, 0);
7927 		}
7928 	      if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
7929 		  == GS_ERROR)
7930 		{
7931 		  remove = true;
7932 		  break;
7933 		}
7934 	      if (DECL_P (decl))
7935 		{
7936 		  if (error_operand_p (decl))
7937 		    {
7938 		      remove = true;
7939 		      break;
7940 		    }
7941 
7942 		  tree stype = TREE_TYPE (decl);
7943 		  if (TREE_CODE (stype) == REFERENCE_TYPE)
7944 		    stype = TREE_TYPE (stype);
7945 		  if (TYPE_SIZE_UNIT (stype) == NULL
7946 		      || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
7947 		    {
7948 		      error_at (OMP_CLAUSE_LOCATION (c),
7949 				"mapping field %qE of variable length "
7950 				"structure", OMP_CLAUSE_DECL (c));
7951 		      remove = true;
7952 		      break;
7953 		    }
7954 
7955 		  if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7956 		    {
7957 		      /* Error recovery.  */
7958 		      if (prev_list_p == NULL)
7959 			{
7960 			  remove = true;
7961 			  break;
7962 			}
7963 		      if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7964 			{
7965 			  tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
7966 			  if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
7967 			    {
7968 			      remove = true;
7969 			      break;
7970 			    }
7971 			}
7972 		    }
7973 
7974 		  tree offset;
7975 		  poly_int64 bitsize, bitpos;
7976 		  machine_mode mode;
7977 		  int unsignedp, reversep, volatilep = 0;
7978 		  tree base = OMP_CLAUSE_DECL (c);
7979 		  while (TREE_CODE (base) == ARRAY_REF)
7980 		    base = TREE_OPERAND (base, 0);
7981 		  if (TREE_CODE (base) == INDIRECT_REF)
7982 		    base = TREE_OPERAND (base, 0);
7983 		  base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7984 					      &mode, &unsignedp, &reversep,
7985 					      &volatilep);
7986 		  tree orig_base = base;
7987 		  if ((TREE_CODE (base) == INDIRECT_REF
7988 		       || (TREE_CODE (base) == MEM_REF
7989 			   && integer_zerop (TREE_OPERAND (base, 1))))
7990 		      && DECL_P (TREE_OPERAND (base, 0))
7991 		      && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
7992 			  == REFERENCE_TYPE))
7993 		    base = TREE_OPERAND (base, 0);
7994 		  gcc_assert (base == decl
7995 			      && (offset == NULL_TREE
7996 				  || poly_int_tree_p (offset)));
7997 
7998 		  splay_tree_node n
7999 		    = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8000 		  bool ptr = (OMP_CLAUSE_MAP_KIND (c)
8001 			      == GOMP_MAP_ALWAYS_POINTER);
8002 		  if (n == NULL || (n->value & GOVD_MAP) == 0)
8003 		    {
8004 		      tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8005 						 OMP_CLAUSE_MAP);
8006 		      OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
8007 		      if (orig_base != base)
8008 			OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
8009 		      else
8010 			OMP_CLAUSE_DECL (l) = decl;
8011 		      OMP_CLAUSE_SIZE (l) = size_int (1);
8012 		      if (struct_map_to_clause == NULL)
8013 			struct_map_to_clause = new hash_map<tree, tree>;
8014 		      struct_map_to_clause->put (decl, l);
8015 		      if (ptr)
8016 			{
8017 			  enum gomp_map_kind mkind
8018 			    = code == OMP_TARGET_EXIT_DATA
8019 			      ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8020 			  tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8021 						      OMP_CLAUSE_MAP);
8022 			  OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8023 			  OMP_CLAUSE_DECL (c2)
8024 			    = unshare_expr (OMP_CLAUSE_DECL (c));
8025 			  OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
8026 			  OMP_CLAUSE_SIZE (c2)
8027 			    = TYPE_SIZE_UNIT (ptr_type_node);
8028 			  OMP_CLAUSE_CHAIN (l) = c2;
8029 			  if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8030 			    {
8031 			      tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8032 			      tree c3
8033 				= build_omp_clause (OMP_CLAUSE_LOCATION (c),
8034 						    OMP_CLAUSE_MAP);
8035 			      OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8036 			      OMP_CLAUSE_DECL (c3)
8037 				= unshare_expr (OMP_CLAUSE_DECL (c4));
8038 			      OMP_CLAUSE_SIZE (c3)
8039 				= TYPE_SIZE_UNIT (ptr_type_node);
8040 			      OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8041 			      OMP_CLAUSE_CHAIN (c2) = c3;
8042 			    }
8043 			  *prev_list_p = l;
8044 			  prev_list_p = NULL;
8045 			}
8046 		      else
8047 			{
8048 			  OMP_CLAUSE_CHAIN (l) = c;
8049 			  *list_p = l;
8050 			  list_p = &OMP_CLAUSE_CHAIN (l);
8051 			}
8052 		      if (orig_base != base && code == OMP_TARGET)
8053 			{
8054 			  tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8055 						      OMP_CLAUSE_MAP);
8056 			  enum gomp_map_kind mkind
8057 			    = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8058 			  OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8059 			  OMP_CLAUSE_DECL (c2) = decl;
8060 			  OMP_CLAUSE_SIZE (c2) = size_zero_node;
8061 			  OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8062 			  OMP_CLAUSE_CHAIN (l) = c2;
8063 			}
8064 		      flags = GOVD_MAP | GOVD_EXPLICIT;
8065 		      if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8066 			flags |= GOVD_SEEN;
8067 		      goto do_add_decl;
8068 		    }
8069 		  else
8070 		    {
8071 		      tree *osc = struct_map_to_clause->get (decl);
8072 		      tree *sc = NULL, *scp = NULL;
8073 		      if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8074 			n->value |= GOVD_SEEN;
8075 		      poly_offset_int o1, o2;
8076 		      if (offset)
8077 			o1 = wi::to_poly_offset (offset);
8078 		      else
8079 			o1 = 0;
8080 		      if (maybe_ne (bitpos, 0))
8081 			o1 += bits_to_bytes_round_down (bitpos);
8082 		      sc = &OMP_CLAUSE_CHAIN (*osc);
8083 		      if (*sc != c
8084 			  && (OMP_CLAUSE_MAP_KIND (*sc)
8085 			      == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8086 			sc = &OMP_CLAUSE_CHAIN (*sc);
8087 		      for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8088 			if (ptr && sc == prev_list_p)
8089 			  break;
8090 			else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8091 				 != COMPONENT_REF
8092 				 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8093 				     != INDIRECT_REF)
8094 				 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8095 				     != ARRAY_REF))
8096 			  break;
8097 			else
8098 			  {
8099 			    tree offset2;
8100 			    poly_int64 bitsize2, bitpos2;
8101 			    base = OMP_CLAUSE_DECL (*sc);
8102 			    if (TREE_CODE (base) == ARRAY_REF)
8103 			      {
8104 				while (TREE_CODE (base) == ARRAY_REF)
8105 				  base = TREE_OPERAND (base, 0);
8106 				if (TREE_CODE (base) != COMPONENT_REF
8107 				    || (TREE_CODE (TREE_TYPE (base))
8108 					!= ARRAY_TYPE))
8109 				  break;
8110 			      }
8111 			    else if (TREE_CODE (base) == INDIRECT_REF
8112 				     && (TREE_CODE (TREE_OPERAND (base, 0))
8113 					 == COMPONENT_REF)
8114 				     && (TREE_CODE (TREE_TYPE
8115 						     (TREE_OPERAND (base, 0)))
8116 					 == REFERENCE_TYPE))
8117 			      base = TREE_OPERAND (base, 0);
8118 			    base = get_inner_reference (base, &bitsize2,
8119 							&bitpos2, &offset2,
8120 							&mode, &unsignedp,
8121 							&reversep, &volatilep);
8122 			    if ((TREE_CODE (base) == INDIRECT_REF
8123 				 || (TREE_CODE (base) == MEM_REF
8124 				     && integer_zerop (TREE_OPERAND (base,
8125 								     1))))
8126 				&& DECL_P (TREE_OPERAND (base, 0))
8127 				&& (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8128 									0)))
8129 				    == REFERENCE_TYPE))
8130 			      base = TREE_OPERAND (base, 0);
8131 			    if (base != decl)
8132 			      break;
8133 			    if (scp)
8134 			      continue;
8135 			    gcc_assert (offset == NULL_TREE
8136 					|| poly_int_tree_p (offset));
8137 			    tree d1 = OMP_CLAUSE_DECL (*sc);
8138 			    tree d2 = OMP_CLAUSE_DECL (c);
8139 			    while (TREE_CODE (d1) == ARRAY_REF)
8140 			      d1 = TREE_OPERAND (d1, 0);
8141 			    while (TREE_CODE (d2) == ARRAY_REF)
8142 			      d2 = TREE_OPERAND (d2, 0);
8143 			    if (TREE_CODE (d1) == INDIRECT_REF)
8144 			      d1 = TREE_OPERAND (d1, 0);
8145 			    if (TREE_CODE (d2) == INDIRECT_REF)
8146 			      d2 = TREE_OPERAND (d2, 0);
8147 			    while (TREE_CODE (d1) == COMPONENT_REF)
8148 			      if (TREE_CODE (d2) == COMPONENT_REF
8149 				  && TREE_OPERAND (d1, 1)
8150 				     == TREE_OPERAND (d2, 1))
8151 				{
8152 				  d1 = TREE_OPERAND (d1, 0);
8153 				  d2 = TREE_OPERAND (d2, 0);
8154 				}
8155 			      else
8156 				break;
8157 			    if (d1 == d2)
8158 			      {
8159 				error_at (OMP_CLAUSE_LOCATION (c),
8160 					  "%qE appears more than once in map "
8161 					  "clauses", OMP_CLAUSE_DECL (c));
8162 				remove = true;
8163 				break;
8164 			      }
8165 			    if (offset2)
8166 			      o2 = wi::to_poly_offset (offset2);
8167 			    else
8168 			      o2 = 0;
8169 			    o2 += bits_to_bytes_round_down (bitpos2);
8170 			    if (maybe_lt (o1, o2)
8171 				|| (known_eq (o1, 2)
8172 				    && maybe_lt (bitpos, bitpos2)))
8173 			      {
8174 				if (ptr)
8175 				  scp = sc;
8176 				else
8177 				  break;
8178 			      }
8179 			  }
8180 		      if (remove)
8181 			break;
8182 		      OMP_CLAUSE_SIZE (*osc)
8183 			= size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8184 				      size_one_node);
8185 		      if (ptr)
8186 			{
8187 			  tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8188 						      OMP_CLAUSE_MAP);
8189 			  tree cl = NULL_TREE;
8190 			  enum gomp_map_kind mkind
8191 			    = code == OMP_TARGET_EXIT_DATA
8192 			      ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8193 			  OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8194 			  OMP_CLAUSE_DECL (c2)
8195 			    = unshare_expr (OMP_CLAUSE_DECL (c));
8196 			  OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8197 			  OMP_CLAUSE_SIZE (c2)
8198 			    = TYPE_SIZE_UNIT (ptr_type_node);
8199 			  cl = scp ? *prev_list_p : c2;
8200 			  if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8201 			    {
8202 			      tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8203 			      tree c3
8204 				= build_omp_clause (OMP_CLAUSE_LOCATION (c),
8205 						    OMP_CLAUSE_MAP);
8206 			      OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8207 			      OMP_CLAUSE_DECL (c3)
8208 				= unshare_expr (OMP_CLAUSE_DECL (c4));
8209 			      OMP_CLAUSE_SIZE (c3)
8210 				= TYPE_SIZE_UNIT (ptr_type_node);
8211 			      OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8212 			      if (!scp)
8213 				OMP_CLAUSE_CHAIN (c2) = c3;
8214 			      else
8215 				cl = c3;
8216 			    }
8217 			  if (scp)
8218 			    *scp = c2;
8219 			  if (sc == prev_list_p)
8220 			    {
8221 			      *sc = cl;
8222 			      prev_list_p = NULL;
8223 			    }
8224 			  else
8225 			    {
8226 			      *prev_list_p = OMP_CLAUSE_CHAIN (c);
8227 			      list_p = prev_list_p;
8228 			      prev_list_p = NULL;
8229 			      OMP_CLAUSE_CHAIN (c) = *sc;
8230 			      *sc = cl;
8231 			      continue;
8232 			    }
8233 			}
8234 		      else if (*sc != c)
8235 			{
8236 			  *list_p = OMP_CLAUSE_CHAIN (c);
8237 			  OMP_CLAUSE_CHAIN (c) = *sc;
8238 			  *sc = c;
8239 			  continue;
8240 			}
8241 		    }
8242 		}
8243 	      if (!remove
8244 		  && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8245 		  && OMP_CLAUSE_CHAIN (c)
8246 		  && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8247 		  && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8248 		      == GOMP_MAP_ALWAYS_POINTER))
8249 		prev_list_p = list_p;
8250 	      break;
8251 	    }
8252 	  flags = GOVD_MAP | GOVD_EXPLICIT;
8253 	  if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8254 	      || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8255 	    flags |= GOVD_MAP_ALWAYS_TO;
8256 	  goto do_add;
8257 
8258 	case OMP_CLAUSE_DEPEND:
8259 	  if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8260 	    {
8261 	      tree deps = OMP_CLAUSE_DECL (c);
8262 	      while (deps && TREE_CODE (deps) == TREE_LIST)
8263 		{
8264 		  if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8265 		      && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8266 		    gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8267 				   pre_p, NULL, is_gimple_val, fb_rvalue);
8268 		  deps = TREE_CHAIN (deps);
8269 		}
8270 	      break;
8271 	    }
8272 	  else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8273 	    break;
8274 	  if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8275 	    {
8276 	      gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8277 			     NULL, is_gimple_val, fb_rvalue);
8278 	      OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8279 	    }
8280 	  if (error_operand_p (OMP_CLAUSE_DECL (c)))
8281 	    {
8282 	      remove = true;
8283 	      break;
8284 	    }
8285 	  OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8286 	  if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8287 			     is_gimple_val, fb_rvalue) == GS_ERROR)
8288 	    {
8289 	      remove = true;
8290 	      break;
8291 	    }
8292 	  break;
8293 
8294 	case OMP_CLAUSE_TO:
8295 	case OMP_CLAUSE_FROM:
8296 	case OMP_CLAUSE__CACHE_:
8297 	  decl = OMP_CLAUSE_DECL (c);
8298 	  if (error_operand_p (decl))
8299 	    {
8300 	      remove = true;
8301 	      break;
8302 	    }
8303 	  if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8304 	    OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8305 				  : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8306 	  if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8307 			     NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8308 	    {
8309 	      remove = true;
8310 	      break;
8311 	    }
8312 	  if (!DECL_P (decl))
8313 	    {
8314 	      if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8315 				 NULL, is_gimple_lvalue, fb_lvalue)
8316 		  == GS_ERROR)
8317 		{
8318 		  remove = true;
8319 		  break;
8320 		}
8321 	      break;
8322 	    }
8323 	  goto do_notice;
8324 
8325 	case OMP_CLAUSE_USE_DEVICE_PTR:
8326 	  flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8327 	  goto do_add;
8328 	case OMP_CLAUSE_IS_DEVICE_PTR:
8329 	  flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8330 	  goto do_add;
8331 
8332 	do_add:
8333 	  decl = OMP_CLAUSE_DECL (c);
8334 	do_add_decl:
8335 	  if (error_operand_p (decl))
8336 	    {
8337 	      remove = true;
8338 	      break;
8339 	    }
8340 	  if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8341 	    {
8342 	      tree t = omp_member_access_dummy_var (decl);
8343 	      if (t)
8344 		{
8345 		  tree v = DECL_VALUE_EXPR (decl);
8346 		  DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8347 		  if (outer_ctx)
8348 		    omp_notice_variable (outer_ctx, t, true);
8349 		}
8350 	    }
8351 	  if (code == OACC_DATA
8352 	      && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8353 	      && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8354 	    flags |= GOVD_MAP_0LEN_ARRAY;
8355 	  omp_add_variable (ctx, decl, flags);
8356 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8357 	      && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8358 	    {
8359 	      omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8360 				GOVD_LOCAL | GOVD_SEEN);
8361 	      if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8362 		  && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8363 				find_decl_expr,
8364 				OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8365 				NULL) == NULL_TREE)
8366 		omp_add_variable (ctx,
8367 				  OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8368 				  GOVD_LOCAL | GOVD_SEEN);
8369 	      gimplify_omp_ctxp = ctx;
8370 	      push_gimplify_context ();
8371 
8372 	      OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8373 	      OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8374 
8375 	      gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8376 		  		&OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8377 	      pop_gimplify_context
8378 		(gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8379 	      push_gimplify_context ();
8380 	      gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8381 		  		&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8382 	      pop_gimplify_context
8383 		(gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8384 	      OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8385 	      OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8386 
8387 	      gimplify_omp_ctxp = outer_ctx;
8388 	    }
8389 	  else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8390 		   && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8391 	    {
8392 	      gimplify_omp_ctxp = ctx;
8393 	      push_gimplify_context ();
8394 	      if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8395 		{
8396 		  tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8397 				      NULL, NULL);
8398 		  TREE_SIDE_EFFECTS (bind) = 1;
8399 		  BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8400 		  OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8401 		}
8402 	      gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8403 				&OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8404 	      pop_gimplify_context
8405 		(gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8406 	      OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8407 
8408 	      gimplify_omp_ctxp = outer_ctx;
8409 	    }
8410 	  else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8411 		   && OMP_CLAUSE_LINEAR_STMT (c))
8412 	    {
8413 	      gimplify_omp_ctxp = ctx;
8414 	      push_gimplify_context ();
8415 	      if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8416 		{
8417 		  tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8418 				      NULL, NULL);
8419 		  TREE_SIDE_EFFECTS (bind) = 1;
8420 		  BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8421 		  OMP_CLAUSE_LINEAR_STMT (c) = bind;
8422 		}
8423 	      gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8424 				&OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8425 	      pop_gimplify_context
8426 		(gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8427 	      OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8428 
8429 	      gimplify_omp_ctxp = outer_ctx;
8430 	    }
8431 	  if (notice_outer)
8432 	    goto do_notice;
8433 	  break;
8434 
8435 	case OMP_CLAUSE_COPYIN:
8436 	case OMP_CLAUSE_COPYPRIVATE:
8437 	  decl = OMP_CLAUSE_DECL (c);
8438 	  if (error_operand_p (decl))
8439 	    {
8440 	      remove = true;
8441 	      break;
8442 	    }
8443 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8444 	      && !remove
8445 	      && !omp_check_private (ctx, decl, true))
8446 	    {
8447 	      remove = true;
8448 	      if (is_global_var (decl))
8449 		{
8450 		  if (DECL_THREAD_LOCAL_P (decl))
8451 		    remove = false;
8452 		  else if (DECL_HAS_VALUE_EXPR_P (decl))
8453 		    {
8454 		      tree value = get_base_address (DECL_VALUE_EXPR (decl));
8455 
8456 		      if (value
8457 			  && DECL_P (value)
8458 			  && DECL_THREAD_LOCAL_P (value))
8459 			remove = false;
8460 		    }
8461 		}
8462 	      if (remove)
8463 		error_at (OMP_CLAUSE_LOCATION (c),
8464 			  "copyprivate variable %qE is not threadprivate"
8465 			  " or private in outer context", DECL_NAME (decl));
8466 	    }
8467 	do_notice:
8468 	  if (outer_ctx)
8469 	    omp_notice_variable (outer_ctx, decl, true);
8470 	  if (check_non_private
8471 	      && region_type == ORT_WORKSHARE
8472 	      && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8473 		  || decl == OMP_CLAUSE_DECL (c)
8474 		  || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
8475 		      && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8476 			  == ADDR_EXPR
8477 			  || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8478 			      == POINTER_PLUS_EXPR
8479 			      && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8480 						(OMP_CLAUSE_DECL (c), 0), 0))
8481 				  == ADDR_EXPR)))))
8482 	      && omp_check_private (ctx, decl, false))
8483 	    {
8484 	      error ("%s variable %qE is private in outer context",
8485 		     check_non_private, DECL_NAME (decl));
8486 	      remove = true;
8487 	    }
8488 	  break;
8489 
8490 	case OMP_CLAUSE_IF:
8491 	  if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
8492 	      && OMP_CLAUSE_IF_MODIFIER (c) != code)
8493 	    {
8494 	      const char *p[2];
8495 	      for (int i = 0; i < 2; i++)
8496 		switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
8497 		  {
8498 		  case OMP_PARALLEL: p[i] = "parallel"; break;
8499 		  case OMP_TASK: p[i] = "task"; break;
8500 		  case OMP_TASKLOOP: p[i] = "taskloop"; break;
8501 		  case OMP_TARGET_DATA: p[i] = "target data"; break;
8502 		  case OMP_TARGET: p[i] = "target"; break;
8503 		  case OMP_TARGET_UPDATE: p[i] = "target update"; break;
8504 		  case OMP_TARGET_ENTER_DATA:
8505 		    p[i] = "target enter data"; break;
8506 		  case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
8507 		  default: gcc_unreachable ();
8508 		  }
8509 	      error_at (OMP_CLAUSE_LOCATION (c),
8510 			"expected %qs %<if%> clause modifier rather than %qs",
8511 			p[0], p[1]);
8512 	      remove = true;
8513 	    }
8514 	  /* Fall through.  */
8515 
8516 	case OMP_CLAUSE_FINAL:
8517 	  OMP_CLAUSE_OPERAND (c, 0)
8518 	    = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
8519 	  /* Fall through.  */
8520 
8521 	case OMP_CLAUSE_SCHEDULE:
8522 	case OMP_CLAUSE_NUM_THREADS:
8523 	case OMP_CLAUSE_NUM_TEAMS:
8524 	case OMP_CLAUSE_THREAD_LIMIT:
8525 	case OMP_CLAUSE_DIST_SCHEDULE:
8526 	case OMP_CLAUSE_DEVICE:
8527 	case OMP_CLAUSE_PRIORITY:
8528 	case OMP_CLAUSE_GRAINSIZE:
8529 	case OMP_CLAUSE_NUM_TASKS:
8530 	case OMP_CLAUSE_HINT:
8531 	case OMP_CLAUSE_ASYNC:
8532 	case OMP_CLAUSE_WAIT:
8533 	case OMP_CLAUSE_NUM_GANGS:
8534 	case OMP_CLAUSE_NUM_WORKERS:
8535 	case OMP_CLAUSE_VECTOR_LENGTH:
8536 	case OMP_CLAUSE_WORKER:
8537 	case OMP_CLAUSE_VECTOR:
8538 	  if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8539 			     is_gimple_val, fb_rvalue) == GS_ERROR)
8540 	    remove = true;
8541 	  break;
8542 
8543 	case OMP_CLAUSE_GANG:
8544 	  if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8545 			     is_gimple_val, fb_rvalue) == GS_ERROR)
8546 	    remove = true;
8547 	  if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
8548 			     is_gimple_val, fb_rvalue) == GS_ERROR)
8549 	    remove = true;
8550 	  break;
8551 
8552 	case OMP_CLAUSE_NOWAIT:
8553 	case OMP_CLAUSE_ORDERED:
8554 	case OMP_CLAUSE_UNTIED:
8555 	case OMP_CLAUSE_COLLAPSE:
8556 	case OMP_CLAUSE_TILE:
8557 	case OMP_CLAUSE_AUTO:
8558 	case OMP_CLAUSE_SEQ:
8559 	case OMP_CLAUSE_INDEPENDENT:
8560 	case OMP_CLAUSE_MERGEABLE:
8561 	case OMP_CLAUSE_PROC_BIND:
8562 	case OMP_CLAUSE_SAFELEN:
8563 	case OMP_CLAUSE_SIMDLEN:
8564 	case OMP_CLAUSE_NOGROUP:
8565 	case OMP_CLAUSE_THREADS:
8566 	case OMP_CLAUSE_SIMD:
8567 	  break;
8568 
8569 	case OMP_CLAUSE_DEFAULTMAP:
8570 	  ctx->target_map_scalars_firstprivate = false;
8571 	  break;
8572 
8573 	case OMP_CLAUSE_ALIGNED:
8574 	  decl = OMP_CLAUSE_DECL (c);
8575 	  if (error_operand_p (decl))
8576 	    {
8577 	      remove = true;
8578 	      break;
8579 	    }
8580 	  if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
8581 			     is_gimple_val, fb_rvalue) == GS_ERROR)
8582 	    {
8583 	      remove = true;
8584 	      break;
8585 	    }
8586 	  if (!is_global_var (decl)
8587 	      && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8588 	    omp_add_variable (ctx, decl, GOVD_ALIGNED);
8589 	  break;
8590 
8591 	case OMP_CLAUSE_DEFAULT:
8592 	  ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
8593 	  break;
8594 
8595 	default:
8596 	  gcc_unreachable ();
8597 	}
8598 
8599       if (code == OACC_DATA
8600 	  && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8601 	  && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8602 	remove = true;
8603       if (remove)
8604 	*list_p = OMP_CLAUSE_CHAIN (c);
8605       else
8606 	list_p = &OMP_CLAUSE_CHAIN (c);
8607     }
8608 
8609   gimplify_omp_ctxp = ctx;
8610   if (struct_map_to_clause)
8611     delete struct_map_to_clause;
8612 }
8613 
8614 /* Return true if DECL is a candidate for shared to firstprivate
8615    optimization.  We only consider non-addressable scalars, not
8616    too big, and not references.  */
8617 
8618 static bool
8619 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
8620 {
8621   if (TREE_ADDRESSABLE (decl))
8622     return false;
8623   tree type = TREE_TYPE (decl);
8624   if (!is_gimple_reg_type (type)
8625       || TREE_CODE (type) == REFERENCE_TYPE
8626       || TREE_ADDRESSABLE (type))
8627     return false;
8628   /* Don't optimize too large decls, as each thread/task will have
8629      its own.  */
8630   HOST_WIDE_INT len = int_size_in_bytes (type);
8631   if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
8632     return false;
8633   if (lang_hooks.decls.omp_privatize_by_reference (decl))
8634     return false;
8635   return true;
8636 }
8637 
8638 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8639    For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8640    GOVD_WRITTEN in outer contexts.  */
8641 
8642 static void
8643 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
8644 {
8645   for (; ctx; ctx = ctx->outer_context)
8646     {
8647       splay_tree_node n = splay_tree_lookup (ctx->variables,
8648 					     (splay_tree_key) decl);
8649       if (n == NULL)
8650 	continue;
8651       else if (n->value & GOVD_SHARED)
8652 	{
8653 	  n->value |= GOVD_WRITTEN;
8654 	  return;
8655 	}
8656       else if (n->value & GOVD_DATA_SHARE_CLASS)
8657 	return;
8658     }
8659 }
8660 
8661 /* Helper callback for walk_gimple_seq to discover possible stores
8662    to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8663    GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8664    for those.  */
8665 
8666 static tree
8667 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
8668 {
8669   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
8670 
8671   *walk_subtrees = 0;
8672   if (!wi->is_lhs)
8673     return NULL_TREE;
8674 
8675   tree op = *tp;
8676   do
8677     {
8678       if (handled_component_p (op))
8679 	op = TREE_OPERAND (op, 0);
8680       else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
8681 	       && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
8682 	op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8683       else
8684 	break;
8685     }
8686   while (1);
8687   if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
8688     return NULL_TREE;
8689 
8690   omp_mark_stores (gimplify_omp_ctxp, op);
8691   return NULL_TREE;
8692 }
8693 
8694 /* Helper callback for walk_gimple_seq to discover possible stores
8695    to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8696    GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8697    for those.  */
8698 
8699 static tree
8700 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
8701 		      bool *handled_ops_p,
8702 		      struct walk_stmt_info *wi)
8703 {
8704   gimple *stmt = gsi_stmt (*gsi_p);
8705   switch (gimple_code (stmt))
8706     {
8707     /* Don't recurse on OpenMP constructs for which
8708        gimplify_adjust_omp_clauses already handled the bodies,
8709        except handle gimple_omp_for_pre_body.  */
8710     case GIMPLE_OMP_FOR:
8711       *handled_ops_p = true;
8712       if (gimple_omp_for_pre_body (stmt))
8713 	walk_gimple_seq (gimple_omp_for_pre_body (stmt),
8714 			 omp_find_stores_stmt, omp_find_stores_op, wi);
8715       break;
8716     case GIMPLE_OMP_PARALLEL:
8717     case GIMPLE_OMP_TASK:
8718     case GIMPLE_OMP_SECTIONS:
8719     case GIMPLE_OMP_SINGLE:
8720     case GIMPLE_OMP_TARGET:
8721     case GIMPLE_OMP_TEAMS:
8722     case GIMPLE_OMP_CRITICAL:
8723       *handled_ops_p = true;
8724       break;
8725     default:
8726       break;
8727     }
8728   return NULL_TREE;
8729 }
8730 
8731 struct gimplify_adjust_omp_clauses_data
8732 {
8733   tree *list_p;
8734   gimple_seq *pre_p;
8735 };
8736 
8737 /* For all variables that were not actually used within the context,
8738    remove PRIVATE, SHARED, and FIRSTPRIVATE clauses.  */
8739 
8740 static int
8741 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
8742 {
8743   tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
8744   gimple_seq *pre_p
8745     = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
8746   tree decl = (tree) n->key;
8747   unsigned flags = n->value;
8748   enum omp_clause_code code;
8749   tree clause;
8750   bool private_debug;
8751 
8752   if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
8753     return 0;
8754   if ((flags & GOVD_SEEN) == 0)
8755     return 0;
8756   if (flags & GOVD_DEBUG_PRIVATE)
8757     {
8758       gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
8759       private_debug = true;
8760     }
8761   else if (flags & GOVD_MAP)
8762     private_debug = false;
8763   else
8764     private_debug
8765       = lang_hooks.decls.omp_private_debug_clause (decl,
8766 						   !!(flags & GOVD_SHARED));
8767   if (private_debug)
8768     code = OMP_CLAUSE_PRIVATE;
8769   else if (flags & GOVD_MAP)
8770     {
8771       code = OMP_CLAUSE_MAP;
8772       if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8773 	  && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8774 	{
8775 	  error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
8776 	  return 0;
8777 	}
8778     }
8779   else if (flags & GOVD_SHARED)
8780     {
8781       if (is_global_var (decl))
8782 	{
8783 	  struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8784 	  while (ctx != NULL)
8785 	    {
8786 	      splay_tree_node on
8787 		= splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8788 	      if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
8789 				      | GOVD_PRIVATE | GOVD_REDUCTION
8790 				      | GOVD_LINEAR | GOVD_MAP)) != 0)
8791 		break;
8792 	      ctx = ctx->outer_context;
8793 	    }
8794 	  if (ctx == NULL)
8795 	    return 0;
8796 	}
8797       code = OMP_CLAUSE_SHARED;
8798     }
8799   else if (flags & GOVD_PRIVATE)
8800     code = OMP_CLAUSE_PRIVATE;
8801   else if (flags & GOVD_FIRSTPRIVATE)
8802     {
8803       code = OMP_CLAUSE_FIRSTPRIVATE;
8804       if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
8805 	  && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8806 	  && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8807 	{
8808 	  error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8809 		 "%<target%> construct", decl);
8810 	  return 0;
8811 	}
8812     }
8813   else if (flags & GOVD_LASTPRIVATE)
8814     code = OMP_CLAUSE_LASTPRIVATE;
8815   else if (flags & GOVD_ALIGNED)
8816     return 0;
8817   else
8818     gcc_unreachable ();
8819 
8820   if (((flags & GOVD_LASTPRIVATE)
8821        || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
8822       && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8823     omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8824 
8825   tree chain = *list_p;
8826   clause = build_omp_clause (input_location, code);
8827   OMP_CLAUSE_DECL (clause) = decl;
8828   OMP_CLAUSE_CHAIN (clause) = chain;
8829   if (private_debug)
8830     OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
8831   else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
8832     OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
8833   else if (code == OMP_CLAUSE_SHARED
8834 	   && (flags & GOVD_WRITTEN) == 0
8835 	   && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8836     OMP_CLAUSE_SHARED_READONLY (clause) = 1;
8837   else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
8838     OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
8839   else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
8840     {
8841       tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
8842       OMP_CLAUSE_DECL (nc) = decl;
8843       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
8844 	  && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
8845 	OMP_CLAUSE_DECL (clause)
8846 	  = build_simple_mem_ref_loc (input_location, decl);
8847       OMP_CLAUSE_DECL (clause)
8848 	= build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
8849 		  build_int_cst (build_pointer_type (char_type_node), 0));
8850       OMP_CLAUSE_SIZE (clause) = size_zero_node;
8851       OMP_CLAUSE_SIZE (nc) = size_zero_node;
8852       OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
8853       OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
8854       OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8855       OMP_CLAUSE_CHAIN (nc) = chain;
8856       OMP_CLAUSE_CHAIN (clause) = nc;
8857       struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8858       gimplify_omp_ctxp = ctx->outer_context;
8859       gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
8860 		     pre_p, NULL, is_gimple_val, fb_rvalue);
8861       gimplify_omp_ctxp = ctx;
8862     }
8863   else if (code == OMP_CLAUSE_MAP)
8864     {
8865       int kind;
8866       /* Not all combinations of these GOVD_MAP flags are actually valid.  */
8867       switch (flags & (GOVD_MAP_TO_ONLY
8868 		       | GOVD_MAP_FORCE
8869 		       | GOVD_MAP_FORCE_PRESENT))
8870 	{
8871 	case 0:
8872 	  kind = GOMP_MAP_TOFROM;
8873 	  break;
8874 	case GOVD_MAP_FORCE:
8875 	  kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
8876 	  break;
8877 	case GOVD_MAP_TO_ONLY:
8878 	  kind = GOMP_MAP_TO;
8879 	  break;
8880 	case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
8881 	  kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
8882 	  break;
8883 	case GOVD_MAP_FORCE_PRESENT:
8884 	  kind = GOMP_MAP_FORCE_PRESENT;
8885 	  break;
8886 	default:
8887 	  gcc_unreachable ();
8888 	}
8889       OMP_CLAUSE_SET_MAP_KIND (clause, kind);
8890       if (DECL_SIZE (decl)
8891 	  && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8892 	{
8893 	  tree decl2 = DECL_VALUE_EXPR (decl);
8894 	  gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8895 	  decl2 = TREE_OPERAND (decl2, 0);
8896 	  gcc_assert (DECL_P (decl2));
8897 	  tree mem = build_simple_mem_ref (decl2);
8898 	  OMP_CLAUSE_DECL (clause) = mem;
8899 	  OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8900 	  if (gimplify_omp_ctxp->outer_context)
8901 	    {
8902 	      struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8903 	      omp_notice_variable (ctx, decl2, true);
8904 	      omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
8905 	    }
8906 	  tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8907 				      OMP_CLAUSE_MAP);
8908 	  OMP_CLAUSE_DECL (nc) = decl;
8909 	  OMP_CLAUSE_SIZE (nc) = size_zero_node;
8910 	  if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
8911 	    OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8912 	  else
8913 	    OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8914 	  OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8915 	  OMP_CLAUSE_CHAIN (clause) = nc;
8916 	}
8917       else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
8918 	       && lang_hooks.decls.omp_privatize_by_reference (decl))
8919 	{
8920 	  OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
8921 	  OMP_CLAUSE_SIZE (clause)
8922 	    = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
8923 	  struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8924 	  gimplify_omp_ctxp = ctx->outer_context;
8925 	  gimplify_expr (&OMP_CLAUSE_SIZE (clause),
8926 			 pre_p, NULL, is_gimple_val, fb_rvalue);
8927 	  gimplify_omp_ctxp = ctx;
8928 	  tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8929 				      OMP_CLAUSE_MAP);
8930 	  OMP_CLAUSE_DECL (nc) = decl;
8931 	  OMP_CLAUSE_SIZE (nc) = size_zero_node;
8932 	  OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8933 	  OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8934 	  OMP_CLAUSE_CHAIN (clause) = nc;
8935 	}
8936       else
8937 	OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
8938     }
8939   if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
8940     {
8941       tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
8942       OMP_CLAUSE_DECL (nc) = decl;
8943       OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
8944       OMP_CLAUSE_CHAIN (nc) = chain;
8945       OMP_CLAUSE_CHAIN (clause) = nc;
8946       struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8947       gimplify_omp_ctxp = ctx->outer_context;
8948       lang_hooks.decls.omp_finish_clause (nc, pre_p);
8949       gimplify_omp_ctxp = ctx;
8950     }
8951   *list_p = clause;
8952   struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8953   gimplify_omp_ctxp = ctx->outer_context;
8954   lang_hooks.decls.omp_finish_clause (clause, pre_p);
8955   if (gimplify_omp_ctxp)
8956     for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
8957       if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
8958 	  && DECL_P (OMP_CLAUSE_SIZE (clause)))
8959 	omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
8960 			     true);
8961   gimplify_omp_ctxp = ctx;
8962   return 0;
8963 }
8964 
8965 static void
8966 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
8967 			     enum tree_code code)
8968 {
8969   struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8970   tree c, decl;
8971 
8972   if (body)
8973     {
8974       struct gimplify_omp_ctx *octx;
8975       for (octx = ctx; octx; octx = octx->outer_context)
8976 	if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
8977 	  break;
8978       if (octx)
8979 	{
8980 	  struct walk_stmt_info wi;
8981 	  memset (&wi, 0, sizeof (wi));
8982 	  walk_gimple_seq (body, omp_find_stores_stmt,
8983 			   omp_find_stores_op, &wi);
8984 	}
8985     }
8986   while ((c = *list_p) != NULL)
8987     {
8988       splay_tree_node n;
8989       bool remove = false;
8990 
8991       switch (OMP_CLAUSE_CODE (c))
8992 	{
8993 	case OMP_CLAUSE_FIRSTPRIVATE:
8994 	  if ((ctx->region_type & ORT_TARGET)
8995 	      && (ctx->region_type & ORT_ACC) == 0
8996 	      && TYPE_ATOMIC (strip_array_types
8997 					(TREE_TYPE (OMP_CLAUSE_DECL (c)))))
8998 	    {
8999 	      error_at (OMP_CLAUSE_LOCATION (c),
9000 			"%<_Atomic%> %qD in %<firstprivate%> clause on "
9001 			"%<target%> construct", OMP_CLAUSE_DECL (c));
9002 	      remove = true;
9003 	      break;
9004 	    }
9005 	  /* FALLTHRU */
9006 	case OMP_CLAUSE_PRIVATE:
9007 	case OMP_CLAUSE_SHARED:
9008 	case OMP_CLAUSE_LINEAR:
9009 	  decl = OMP_CLAUSE_DECL (c);
9010 	  n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9011 	  remove = !(n->value & GOVD_SEEN);
9012 	  if (! remove)
9013 	    {
9014 	      bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
9015 	      if ((n->value & GOVD_DEBUG_PRIVATE)
9016 		  || lang_hooks.decls.omp_private_debug_clause (decl, shared))
9017 		{
9018 		  gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
9019 			      || ((n->value & GOVD_DATA_SHARE_CLASS)
9020 				  == GOVD_SHARED));
9021 		  OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
9022 		  OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
9023 		}
9024 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9025 		  && (n->value & GOVD_WRITTEN) == 0
9026 		  && DECL_P (decl)
9027 		  && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9028 		OMP_CLAUSE_SHARED_READONLY (c) = 1;
9029 	      else if (DECL_P (decl)
9030 		       && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9031 			    && (n->value & GOVD_WRITTEN) != 0)
9032 			   || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9033 			       && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
9034 		       && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9035 		omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9036 	    }
9037 	  break;
9038 
9039 	case OMP_CLAUSE_LASTPRIVATE:
9040 	  /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9041 	     accurately reflect the presence of a FIRSTPRIVATE clause.  */
9042 	  decl = OMP_CLAUSE_DECL (c);
9043 	  n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9044 	  OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9045 	    = (n->value & GOVD_FIRSTPRIVATE) != 0;
9046 	  if (code == OMP_DISTRIBUTE
9047 	      && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9048 	    {
9049 	      remove = true;
9050 	      error_at (OMP_CLAUSE_LOCATION (c),
9051 			"same variable used in %<firstprivate%> and "
9052 			"%<lastprivate%> clauses on %<distribute%> "
9053 			"construct");
9054 	    }
9055 	  if (!remove
9056 	      && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9057 	      && DECL_P (decl)
9058 	      && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9059 	    omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9060 	  break;
9061 
9062 	case OMP_CLAUSE_ALIGNED:
9063 	  decl = OMP_CLAUSE_DECL (c);
9064 	  if (!is_global_var (decl))
9065 	    {
9066 	      n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9067 	      remove = n == NULL || !(n->value & GOVD_SEEN);
9068 	      if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9069 		{
9070 		  struct gimplify_omp_ctx *octx;
9071 		  if (n != NULL
9072 		      && (n->value & (GOVD_DATA_SHARE_CLASS
9073 				      & ~GOVD_FIRSTPRIVATE)))
9074 		    remove = true;
9075 		  else
9076 		    for (octx = ctx->outer_context; octx;
9077 			 octx = octx->outer_context)
9078 		      {
9079 			n = splay_tree_lookup (octx->variables,
9080 					       (splay_tree_key) decl);
9081 			if (n == NULL)
9082 			  continue;
9083 			if (n->value & GOVD_LOCAL)
9084 			  break;
9085 			/* We have to avoid assigning a shared variable
9086 			   to itself when trying to add
9087 			   __builtin_assume_aligned.  */
9088 			if (n->value & GOVD_SHARED)
9089 			  {
9090 			    remove = true;
9091 			    break;
9092 			  }
9093 		      }
9094 		}
9095 	    }
9096 	  else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9097 	    {
9098 	      n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9099 	      if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9100 		remove = true;
9101 	    }
9102 	  break;
9103 
9104 	case OMP_CLAUSE_MAP:
9105 	  if (code == OMP_TARGET_EXIT_DATA
9106 	      && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9107 	    {
9108 	      remove = true;
9109 	      break;
9110 	    }
9111 	  decl = OMP_CLAUSE_DECL (c);
9112 	  /* Data clauses associated with acc parallel reductions must be
9113 	     compatible with present_or_copy.  Warn and adjust the clause
9114 	     if that is not the case.  */
9115 	  if (ctx->region_type == ORT_ACC_PARALLEL)
9116 	    {
9117 	      tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9118 	      n = NULL;
9119 
9120 	      if (DECL_P (t))
9121 		n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9122 
9123 	      if (n && (n->value & GOVD_REDUCTION))
9124 		{
9125 		  enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9126 
9127 		  OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9128 		  if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9129 		      && kind != GOMP_MAP_FORCE_PRESENT
9130 		      && kind != GOMP_MAP_POINTER)
9131 		    {
9132 		      warning_at (OMP_CLAUSE_LOCATION (c), 0,
9133 				  "incompatible data clause with reduction "
9134 				  "on %qE; promoting to present_or_copy",
9135 				  DECL_NAME (t));
9136 		      OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9137 		    }
9138 		}
9139 	    }
9140 	  if (!DECL_P (decl))
9141 	    {
9142 	      if ((ctx->region_type & ORT_TARGET) != 0
9143 		  && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9144 		{
9145 		  if (TREE_CODE (decl) == INDIRECT_REF
9146 		      && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
9147 		      && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
9148 			  == REFERENCE_TYPE))
9149 		    decl = TREE_OPERAND (decl, 0);
9150 		  if (TREE_CODE (decl) == COMPONENT_REF)
9151 		    {
9152 		      while (TREE_CODE (decl) == COMPONENT_REF)
9153 			decl = TREE_OPERAND (decl, 0);
9154 		      if (DECL_P (decl))
9155 			{
9156 			  n = splay_tree_lookup (ctx->variables,
9157 						 (splay_tree_key) decl);
9158 			  if (!(n->value & GOVD_SEEN))
9159 			    remove = true;
9160 			}
9161 		    }
9162 		}
9163 	      break;
9164 	    }
9165 	  n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9166 	  if ((ctx->region_type & ORT_TARGET) != 0
9167 	      && !(n->value & GOVD_SEEN)
9168 	      && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
9169 	      && (!is_global_var (decl)
9170 		  || !lookup_attribute ("omp declare target link",
9171 					DECL_ATTRIBUTES (decl))))
9172 	    {
9173 	      remove = true;
9174 	      /* For struct element mapping, if struct is never referenced
9175 		 in target block and none of the mapping has always modifier,
9176 		 remove all the struct element mappings, which immediately
9177 		 follow the GOMP_MAP_STRUCT map clause.  */
9178 	      if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
9179 		{
9180 		  HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
9181 		  while (cnt--)
9182 		    OMP_CLAUSE_CHAIN (c)
9183 		      = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
9184 		}
9185 	    }
9186 	  else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
9187 		   && code == OMP_TARGET_EXIT_DATA)
9188 	    remove = true;
9189 	  else if (DECL_SIZE (decl)
9190 		   && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
9191 		   && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
9192 		   && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9193 		   && (OMP_CLAUSE_MAP_KIND (c)
9194 		       != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9195 	    {
9196 	      /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9197 		 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9198 		 INTEGER_CST.  */
9199 	      gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
9200 
9201 	      tree decl2 = DECL_VALUE_EXPR (decl);
9202 	      gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9203 	      decl2 = TREE_OPERAND (decl2, 0);
9204 	      gcc_assert (DECL_P (decl2));
9205 	      tree mem = build_simple_mem_ref (decl2);
9206 	      OMP_CLAUSE_DECL (c) = mem;
9207 	      OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9208 	      if (ctx->outer_context)
9209 		{
9210 		  omp_notice_variable (ctx->outer_context, decl2, true);
9211 		  omp_notice_variable (ctx->outer_context,
9212 				       OMP_CLAUSE_SIZE (c), true);
9213 		}
9214 	      if (((ctx->region_type & ORT_TARGET) != 0
9215 		   || !ctx->target_firstprivatize_array_bases)
9216 		  && ((n->value & GOVD_SEEN) == 0
9217 		      || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
9218 		{
9219 		  tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9220 					      OMP_CLAUSE_MAP);
9221 		  OMP_CLAUSE_DECL (nc) = decl;
9222 		  OMP_CLAUSE_SIZE (nc) = size_zero_node;
9223 		  if (ctx->target_firstprivatize_array_bases)
9224 		    OMP_CLAUSE_SET_MAP_KIND (nc,
9225 					     GOMP_MAP_FIRSTPRIVATE_POINTER);
9226 		  else
9227 		    OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9228 		  OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9229 		  OMP_CLAUSE_CHAIN (c) = nc;
9230 		  c = nc;
9231 		}
9232 	    }
9233 	  else
9234 	    {
9235 	      if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9236 		OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9237 	      gcc_assert ((n->value & GOVD_SEEN) == 0
9238 			  || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9239 			      == 0));
9240 	    }
9241 	  break;
9242 
9243 	case OMP_CLAUSE_TO:
9244 	case OMP_CLAUSE_FROM:
9245 	case OMP_CLAUSE__CACHE_:
9246 	  decl = OMP_CLAUSE_DECL (c);
9247 	  if (!DECL_P (decl))
9248 	    break;
9249 	  if (DECL_SIZE (decl)
9250 	      && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9251 	    {
9252 	      tree decl2 = DECL_VALUE_EXPR (decl);
9253 	      gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9254 	      decl2 = TREE_OPERAND (decl2, 0);
9255 	      gcc_assert (DECL_P (decl2));
9256 	      tree mem = build_simple_mem_ref (decl2);
9257 	      OMP_CLAUSE_DECL (c) = mem;
9258 	      OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9259 	      if (ctx->outer_context)
9260 		{
9261 		  omp_notice_variable (ctx->outer_context, decl2, true);
9262 		  omp_notice_variable (ctx->outer_context,
9263 				       OMP_CLAUSE_SIZE (c), true);
9264 		}
9265 	    }
9266 	  else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9267 	    OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9268 	  break;
9269 
9270 	case OMP_CLAUSE_REDUCTION:
9271 	  decl = OMP_CLAUSE_DECL (c);
9272 	  /* OpenACC reductions need a present_or_copy data clause.
9273 	     Add one if necessary.  Error is the reduction is private.  */
9274 	  if (ctx->region_type == ORT_ACC_PARALLEL)
9275 	    {
9276 	      n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9277 	      if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9278 		error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9279 			  "reduction on %qE", DECL_NAME (decl));
9280 	      else if ((n->value & GOVD_MAP) == 0)
9281 		{
9282 		  tree next = OMP_CLAUSE_CHAIN (c);
9283 		  tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9284 		  OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9285 		  OMP_CLAUSE_DECL (nc) = decl;
9286 		  OMP_CLAUSE_CHAIN (c) = nc;
9287 		  lang_hooks.decls.omp_finish_clause (nc, pre_p);
9288 		  while (1)
9289 		    {
9290 		      OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9291 		      if (OMP_CLAUSE_CHAIN (nc) == NULL)
9292 			break;
9293 		      nc = OMP_CLAUSE_CHAIN (nc);
9294 		    }
9295 		  OMP_CLAUSE_CHAIN (nc) = next;
9296 		  n->value |= GOVD_MAP;
9297 		}
9298 	    }
9299 	  if (DECL_P (decl)
9300 	      && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9301 	    omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9302 	  break;
9303 	case OMP_CLAUSE_COPYIN:
9304 	case OMP_CLAUSE_COPYPRIVATE:
9305 	case OMP_CLAUSE_IF:
9306 	case OMP_CLAUSE_NUM_THREADS:
9307 	case OMP_CLAUSE_NUM_TEAMS:
9308 	case OMP_CLAUSE_THREAD_LIMIT:
9309 	case OMP_CLAUSE_DIST_SCHEDULE:
9310 	case OMP_CLAUSE_DEVICE:
9311 	case OMP_CLAUSE_SCHEDULE:
9312 	case OMP_CLAUSE_NOWAIT:
9313 	case OMP_CLAUSE_ORDERED:
9314 	case OMP_CLAUSE_DEFAULT:
9315 	case OMP_CLAUSE_UNTIED:
9316 	case OMP_CLAUSE_COLLAPSE:
9317 	case OMP_CLAUSE_FINAL:
9318 	case OMP_CLAUSE_MERGEABLE:
9319 	case OMP_CLAUSE_PROC_BIND:
9320 	case OMP_CLAUSE_SAFELEN:
9321 	case OMP_CLAUSE_SIMDLEN:
9322 	case OMP_CLAUSE_DEPEND:
9323 	case OMP_CLAUSE_PRIORITY:
9324 	case OMP_CLAUSE_GRAINSIZE:
9325 	case OMP_CLAUSE_NUM_TASKS:
9326 	case OMP_CLAUSE_NOGROUP:
9327 	case OMP_CLAUSE_THREADS:
9328 	case OMP_CLAUSE_SIMD:
9329 	case OMP_CLAUSE_HINT:
9330 	case OMP_CLAUSE_DEFAULTMAP:
9331 	case OMP_CLAUSE_USE_DEVICE_PTR:
9332 	case OMP_CLAUSE_IS_DEVICE_PTR:
9333 	case OMP_CLAUSE_ASYNC:
9334 	case OMP_CLAUSE_WAIT:
9335 	case OMP_CLAUSE_INDEPENDENT:
9336 	case OMP_CLAUSE_NUM_GANGS:
9337 	case OMP_CLAUSE_NUM_WORKERS:
9338 	case OMP_CLAUSE_VECTOR_LENGTH:
9339 	case OMP_CLAUSE_GANG:
9340 	case OMP_CLAUSE_WORKER:
9341 	case OMP_CLAUSE_VECTOR:
9342 	case OMP_CLAUSE_AUTO:
9343 	case OMP_CLAUSE_SEQ:
9344 	case OMP_CLAUSE_TILE:
9345 	  break;
9346 
9347 	default:
9348 	  gcc_unreachable ();
9349 	}
9350 
9351       if (remove)
9352 	*list_p = OMP_CLAUSE_CHAIN (c);
9353       else
9354 	list_p = &OMP_CLAUSE_CHAIN (c);
9355     }
9356 
9357   /* Add in any implicit data sharing.  */
9358   struct gimplify_adjust_omp_clauses_data data;
9359   data.list_p = list_p;
9360   data.pre_p = pre_p;
9361   splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
9362 
9363   gimplify_omp_ctxp = ctx->outer_context;
9364   delete_omp_context (ctx);
9365 }
9366 
9367 /* Gimplify OACC_CACHE.  */
9368 
9369 static void
9370 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
9371 {
9372   tree expr = *expr_p;
9373 
9374   gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
9375 			     OACC_CACHE);
9376   gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
9377 			       OACC_CACHE);
9378 
9379   /* TODO: Do something sensible with this information.  */
9380 
9381   *expr_p = NULL_TREE;
9382 }
9383 
9384 /* Helper function of gimplify_oacc_declare.  The helper's purpose is to,
9385    if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
9386    kind.  The entry kind will replace the one in CLAUSE, while the exit
9387    kind will be used in a new omp_clause and returned to the caller.  */
9388 
9389 static tree
9390 gimplify_oacc_declare_1 (tree clause)
9391 {
9392   HOST_WIDE_INT kind, new_op;
9393   bool ret = false;
9394   tree c = NULL;
9395 
9396   kind = OMP_CLAUSE_MAP_KIND (clause);
9397 
9398   switch (kind)
9399     {
9400       case GOMP_MAP_ALLOC:
9401       case GOMP_MAP_FORCE_ALLOC:
9402       case GOMP_MAP_FORCE_TO:
9403 	new_op = GOMP_MAP_DELETE;
9404 	ret = true;
9405 	break;
9406 
9407       case GOMP_MAP_FORCE_FROM:
9408 	OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9409 	new_op = GOMP_MAP_FORCE_FROM;
9410 	ret = true;
9411 	break;
9412 
9413       case GOMP_MAP_FORCE_TOFROM:
9414 	OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_TO);
9415 	new_op = GOMP_MAP_FORCE_FROM;
9416 	ret = true;
9417 	break;
9418 
9419       case GOMP_MAP_FROM:
9420 	OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9421 	new_op = GOMP_MAP_FROM;
9422 	ret = true;
9423 	break;
9424 
9425       case GOMP_MAP_TOFROM:
9426 	OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
9427 	new_op = GOMP_MAP_FROM;
9428 	ret = true;
9429 	break;
9430 
9431       case GOMP_MAP_DEVICE_RESIDENT:
9432       case GOMP_MAP_FORCE_DEVICEPTR:
9433       case GOMP_MAP_FORCE_PRESENT:
9434       case GOMP_MAP_LINK:
9435       case GOMP_MAP_POINTER:
9436       case GOMP_MAP_TO:
9437 	break;
9438 
9439       default:
9440 	gcc_unreachable ();
9441 	break;
9442     }
9443 
9444   if (ret)
9445     {
9446       c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
9447       OMP_CLAUSE_SET_MAP_KIND (c, new_op);
9448       OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
9449     }
9450 
9451   return c;
9452 }
9453 
9454 /* Gimplify OACC_DECLARE.  */
9455 
9456 static void
9457 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
9458 {
9459   tree expr = *expr_p;
9460   gomp_target *stmt;
9461   tree clauses, t, decl;
9462 
9463   clauses = OACC_DECLARE_CLAUSES (expr);
9464 
9465   gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
9466   gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
9467 
9468   for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
9469     {
9470       decl = OMP_CLAUSE_DECL (t);
9471 
9472       if (TREE_CODE (decl) == MEM_REF)
9473 	decl = TREE_OPERAND (decl, 0);
9474 
9475       if (VAR_P (decl) && !is_oacc_declared (decl))
9476 	{
9477 	  tree attr = get_identifier ("oacc declare target");
9478 	  DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
9479 					      DECL_ATTRIBUTES (decl));
9480 	}
9481 
9482       if (VAR_P (decl)
9483 	  && !is_global_var (decl)
9484 	  && DECL_CONTEXT (decl) == current_function_decl)
9485 	{
9486 	  tree c = gimplify_oacc_declare_1 (t);
9487 	  if (c)
9488 	    {
9489 	      if (oacc_declare_returns == NULL)
9490 		oacc_declare_returns = new hash_map<tree, tree>;
9491 
9492 	      oacc_declare_returns->put (decl, c);
9493 	    }
9494 	}
9495 
9496       if (gimplify_omp_ctxp)
9497 	omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
9498     }
9499 
9500   stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
9501 				  clauses);
9502 
9503   gimplify_seq_add_stmt (pre_p, stmt);
9504 
9505   *expr_p = NULL_TREE;
9506 }
9507 
9508 /* Gimplify the contents of an OMP_PARALLEL statement.  This involves
9509    gimplification of the body, as well as scanning the body for used
9510    variables.  We need to do this scan now, because variable-sized
9511    decls will be decomposed during gimplification.  */
9512 
9513 static void
9514 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
9515 {
9516   tree expr = *expr_p;
9517   gimple *g;
9518   gimple_seq body = NULL;
9519 
9520   gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
9521 			     OMP_PARALLEL_COMBINED (expr)
9522 			     ? ORT_COMBINED_PARALLEL
9523 			     : ORT_PARALLEL, OMP_PARALLEL);
9524 
9525   push_gimplify_context ();
9526 
9527   g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
9528   if (gimple_code (g) == GIMPLE_BIND)
9529     pop_gimplify_context (g);
9530   else
9531     pop_gimplify_context (NULL);
9532 
9533   gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
9534 			       OMP_PARALLEL);
9535 
9536   g = gimple_build_omp_parallel (body,
9537 				 OMP_PARALLEL_CLAUSES (expr),
9538 				 NULL_TREE, NULL_TREE);
9539   if (OMP_PARALLEL_COMBINED (expr))
9540     gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
9541   gimplify_seq_add_stmt (pre_p, g);
9542   *expr_p = NULL_TREE;
9543 }
9544 
9545 /* Gimplify the contents of an OMP_TASK statement.  This involves
9546    gimplification of the body, as well as scanning the body for used
9547    variables.  We need to do this scan now, because variable-sized
9548    decls will be decomposed during gimplification.  */
9549 
9550 static void
9551 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
9552 {
9553   tree expr = *expr_p;
9554   gimple *g;
9555   gimple_seq body = NULL;
9556 
9557   gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
9558 			     omp_find_clause (OMP_TASK_CLAUSES (expr),
9559 					      OMP_CLAUSE_UNTIED)
9560 			     ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
9561 
9562   push_gimplify_context ();
9563 
9564   g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
9565   if (gimple_code (g) == GIMPLE_BIND)
9566     pop_gimplify_context (g);
9567   else
9568     pop_gimplify_context (NULL);
9569 
9570   gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
9571 			       OMP_TASK);
9572 
9573   g = gimple_build_omp_task (body,
9574 			     OMP_TASK_CLAUSES (expr),
9575 			     NULL_TREE, NULL_TREE,
9576 			     NULL_TREE, NULL_TREE, NULL_TREE);
9577   gimplify_seq_add_stmt (pre_p, g);
9578   *expr_p = NULL_TREE;
9579 }
9580 
9581 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9582    with non-NULL OMP_FOR_INIT.  */
9583 
9584 static tree
9585 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
9586 {
9587   *walk_subtrees = 0;
9588   switch (TREE_CODE (*tp))
9589     {
9590     case OMP_FOR:
9591       *walk_subtrees = 1;
9592       /* FALLTHRU */
9593     case OMP_SIMD:
9594       if (OMP_FOR_INIT (*tp) != NULL_TREE)
9595 	return *tp;
9596       break;
9597     case BIND_EXPR:
9598     case STATEMENT_LIST:
9599     case OMP_PARALLEL:
9600       *walk_subtrees = 1;
9601       break;
9602     default:
9603       break;
9604     }
9605   return NULL_TREE;
9606 }
9607 
9608 /* Gimplify the gross structure of an OMP_FOR statement.  */
9609 
9610 static enum gimplify_status
9611 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
9612 {
9613   tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
9614   enum gimplify_status ret = GS_ALL_DONE;
9615   enum gimplify_status tret;
9616   gomp_for *gfor;
9617   gimple_seq for_body, for_pre_body;
9618   int i;
9619   bitmap has_decl_expr = NULL;
9620   enum omp_region_type ort = ORT_WORKSHARE;
9621 
9622   orig_for_stmt = for_stmt = *expr_p;
9623 
9624   switch (TREE_CODE (for_stmt))
9625     {
9626     case OMP_FOR:
9627     case OMP_DISTRIBUTE:
9628       break;
9629     case OACC_LOOP:
9630       ort = ORT_ACC;
9631       break;
9632     case OMP_TASKLOOP:
9633       if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
9634 	ort = ORT_UNTIED_TASK;
9635       else
9636 	ort = ORT_TASK;
9637       break;
9638     case OMP_SIMD:
9639       ort = ORT_SIMD;
9640       break;
9641     default:
9642       gcc_unreachable ();
9643     }
9644 
9645   /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9646      clause for the IV.  */
9647   if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9648     {
9649       t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
9650       gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9651       decl = TREE_OPERAND (t, 0);
9652       for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
9653 	if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9654 	    && OMP_CLAUSE_DECL (c) == decl)
9655 	  {
9656 	    OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9657 	    break;
9658 	  }
9659     }
9660 
9661   if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9662     {
9663       gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
9664       inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
9665 				  find_combined_omp_for, NULL, NULL);
9666       if (inner_for_stmt == NULL_TREE)
9667 	{
9668 	  gcc_assert (seen_error ());
9669 	  *expr_p = NULL_TREE;
9670 	  return GS_ERROR;
9671 	}
9672     }
9673 
9674   if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
9675     gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
9676 			       TREE_CODE (for_stmt));
9677 
9678   if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
9679     gimplify_omp_ctxp->distribute = true;
9680 
9681   /* Handle OMP_FOR_INIT.  */
9682   for_pre_body = NULL;
9683   if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
9684     {
9685       has_decl_expr = BITMAP_ALLOC (NULL);
9686       if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
9687 	  && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
9688 	     == VAR_DECL)
9689 	{
9690 	  t = OMP_FOR_PRE_BODY (for_stmt);
9691 	  bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9692 	}
9693       else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
9694 	{
9695 	  tree_stmt_iterator si;
9696 	  for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
9697 	       tsi_next (&si))
9698 	    {
9699 	      t = tsi_stmt (si);
9700 	      if (TREE_CODE (t) == DECL_EXPR
9701 		  && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
9702 		bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9703 	    }
9704 	}
9705     }
9706   if (OMP_FOR_PRE_BODY (for_stmt))
9707     {
9708       if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
9709 	gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9710       else
9711 	{
9712 	  struct gimplify_omp_ctx ctx;
9713 	  memset (&ctx, 0, sizeof (ctx));
9714 	  ctx.region_type = ORT_NONE;
9715 	  gimplify_omp_ctxp = &ctx;
9716 	  gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9717 	  gimplify_omp_ctxp = NULL;
9718 	}
9719     }
9720   OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
9721 
9722   if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9723     for_stmt = inner_for_stmt;
9724 
9725   /* For taskloop, need to gimplify the start, end and step before the
9726      taskloop, outside of the taskloop omp context.  */
9727   if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9728     {
9729       for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9730 	{
9731 	  t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9732 	  if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9733 	    {
9734 	      TREE_OPERAND (t, 1)
9735 		= get_initialized_tmp_var (TREE_OPERAND (t, 1),
9736 					   pre_p, NULL, false);
9737 	      tree c = build_omp_clause (input_location,
9738 					 OMP_CLAUSE_FIRSTPRIVATE);
9739 	      OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9740 	      OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9741 	      OMP_FOR_CLAUSES (orig_for_stmt) = c;
9742 	    }
9743 
9744 	  /* Handle OMP_FOR_COND.  */
9745 	  t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9746 	  if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9747 	    {
9748 	      TREE_OPERAND (t, 1)
9749 		= get_initialized_tmp_var (TREE_OPERAND (t, 1),
9750 					   gimple_seq_empty_p (for_pre_body)
9751 					   ? pre_p : &for_pre_body, NULL,
9752 					   false);
9753 	      tree c = build_omp_clause (input_location,
9754 					 OMP_CLAUSE_FIRSTPRIVATE);
9755 	      OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9756 	      OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9757 	      OMP_FOR_CLAUSES (orig_for_stmt) = c;
9758 	    }
9759 
9760 	  /* Handle OMP_FOR_INCR.  */
9761 	  t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9762 	  if (TREE_CODE (t) == MODIFY_EXPR)
9763 	    {
9764 	      decl = TREE_OPERAND (t, 0);
9765 	      t = TREE_OPERAND (t, 1);
9766 	      tree *tp = &TREE_OPERAND (t, 1);
9767 	      if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
9768 		tp = &TREE_OPERAND (t, 0);
9769 
9770 	      if (!is_gimple_constant (*tp))
9771 		{
9772 		  gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
9773 				    ? pre_p : &for_pre_body;
9774 		  *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
9775 		  tree c = build_omp_clause (input_location,
9776 					     OMP_CLAUSE_FIRSTPRIVATE);
9777 		  OMP_CLAUSE_DECL (c) = *tp;
9778 		  OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9779 		  OMP_FOR_CLAUSES (orig_for_stmt) = c;
9780 		}
9781 	    }
9782 	}
9783 
9784       gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
9785 				 OMP_TASKLOOP);
9786     }
9787 
9788   if (orig_for_stmt != for_stmt)
9789     gimplify_omp_ctxp->combined_loop = true;
9790 
9791   for_body = NULL;
9792   gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9793 	      == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
9794   gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9795 	      == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
9796 
9797   tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
9798   bool is_doacross = false;
9799   if (c && OMP_CLAUSE_ORDERED_EXPR (c))
9800     {
9801       is_doacross = true;
9802       gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
9803 						 (OMP_FOR_INIT (for_stmt))
9804 					       * 2);
9805     }
9806   int collapse = 1, tile = 0;
9807   c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
9808   if (c)
9809     collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9810   c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
9811   if (c)
9812     tile = list_length (OMP_CLAUSE_TILE_LIST (c));
9813   for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9814     {
9815       t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9816       gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9817       decl = TREE_OPERAND (t, 0);
9818       gcc_assert (DECL_P (decl));
9819       gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
9820 		  || POINTER_TYPE_P (TREE_TYPE (decl)));
9821       if (is_doacross)
9822 	{
9823 	  if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
9824 	    gimplify_omp_ctxp->loop_iter_var.quick_push
9825 	      (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
9826 	  else
9827 	    gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9828 	  gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9829 	}
9830 
9831       /* Make sure the iteration variable is private.  */
9832       tree c = NULL_TREE;
9833       tree c2 = NULL_TREE;
9834       if (orig_for_stmt != for_stmt)
9835 	/* Do this only on innermost construct for combined ones.  */;
9836       else if (ort == ORT_SIMD)
9837 	{
9838 	  splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9839 						 (splay_tree_key) decl);
9840 	  omp_is_private (gimplify_omp_ctxp, decl,
9841 			  1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9842 			       != 1));
9843 	  if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9844 	    omp_notice_variable (gimplify_omp_ctxp, decl, true);
9845 	  else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9846 	    {
9847 	      c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9848 	      OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9849 	      unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
9850 	      if (has_decl_expr
9851 		  && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9852 		{
9853 		  OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9854 		  flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9855 		}
9856 	      struct gimplify_omp_ctx *outer
9857 		= gimplify_omp_ctxp->outer_context;
9858 	      if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9859 		{
9860 		  if (outer->region_type == ORT_WORKSHARE
9861 		      && outer->combined_loop)
9862 		    {
9863 		      n = splay_tree_lookup (outer->variables,
9864 					     (splay_tree_key)decl);
9865 		      if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9866 			{
9867 			  OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9868 			  flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9869 			}
9870 		      else
9871 			{
9872 			  struct gimplify_omp_ctx *octx = outer->outer_context;
9873 			  if (octx
9874 			      && octx->region_type == ORT_COMBINED_PARALLEL
9875 			      && octx->outer_context
9876 			      && (octx->outer_context->region_type
9877 				  == ORT_WORKSHARE)
9878 			      && octx->outer_context->combined_loop)
9879 			    {
9880 			      octx = octx->outer_context;
9881 			      n = splay_tree_lookup (octx->variables,
9882 						     (splay_tree_key)decl);
9883 			      if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9884 				{
9885 				  OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9886 				  flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9887 				}
9888 			    }
9889 			}
9890 		    }
9891 		}
9892 
9893 	      OMP_CLAUSE_DECL (c) = decl;
9894 	      OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9895 	      OMP_FOR_CLAUSES (for_stmt) = c;
9896 	      omp_add_variable (gimplify_omp_ctxp, decl, flags);
9897 	      if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9898 		{
9899 		  if (outer->region_type == ORT_WORKSHARE
9900 		      && outer->combined_loop)
9901 		    {
9902 		      if (outer->outer_context
9903 			  && (outer->outer_context->region_type
9904 			      == ORT_COMBINED_PARALLEL))
9905 			outer = outer->outer_context;
9906 		      else if (omp_check_private (outer, decl, false))
9907 			outer = NULL;
9908 		    }
9909 		  else if (((outer->region_type & ORT_TASK) != 0)
9910 			   && outer->combined_loop
9911 			   && !omp_check_private (gimplify_omp_ctxp,
9912 						  decl, false))
9913 		    ;
9914 		  else if (outer->region_type != ORT_COMBINED_PARALLEL)
9915 		    {
9916 		      omp_notice_variable (outer, decl, true);
9917 		      outer = NULL;
9918 		    }
9919 		  if (outer)
9920 		    {
9921 		      n = splay_tree_lookup (outer->variables,
9922 					     (splay_tree_key)decl);
9923 		      if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9924 			{
9925 			  omp_add_variable (outer, decl,
9926 					    GOVD_LASTPRIVATE | GOVD_SEEN);
9927 			  if (outer->region_type == ORT_COMBINED_PARALLEL
9928 			      && outer->outer_context
9929 			      && (outer->outer_context->region_type
9930 				  == ORT_WORKSHARE)
9931 			      && outer->outer_context->combined_loop)
9932 			    {
9933 			      outer = outer->outer_context;
9934 			      n = splay_tree_lookup (outer->variables,
9935 						     (splay_tree_key)decl);
9936 			      if (omp_check_private (outer, decl, false))
9937 				outer = NULL;
9938 			      else if (n == NULL
9939 				       || ((n->value & GOVD_DATA_SHARE_CLASS)
9940 					   == 0))
9941 				omp_add_variable (outer, decl,
9942 						  GOVD_LASTPRIVATE
9943 						  | GOVD_SEEN);
9944 			      else
9945 				outer = NULL;
9946 			    }
9947 			  if (outer && outer->outer_context
9948 			      && (outer->outer_context->region_type
9949 				  == ORT_COMBINED_TEAMS))
9950 			    {
9951 			      outer = outer->outer_context;
9952 			      n = splay_tree_lookup (outer->variables,
9953 						     (splay_tree_key)decl);
9954 			      if (n == NULL
9955 				  || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9956 				omp_add_variable (outer, decl,
9957 						  GOVD_SHARED | GOVD_SEEN);
9958 			      else
9959 				outer = NULL;
9960 			    }
9961 			  if (outer && outer->outer_context)
9962 			    omp_notice_variable (outer->outer_context, decl,
9963 						 true);
9964 			}
9965 		    }
9966 		}
9967 	    }
9968 	  else
9969 	    {
9970 	      bool lastprivate
9971 		= (!has_decl_expr
9972 		   || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
9973 	      struct gimplify_omp_ctx *outer
9974 		= gimplify_omp_ctxp->outer_context;
9975 	      if (outer && lastprivate)
9976 		{
9977 		  if (outer->region_type == ORT_WORKSHARE
9978 		      && outer->combined_loop)
9979 		    {
9980 		      n = splay_tree_lookup (outer->variables,
9981 					     (splay_tree_key)decl);
9982 		      if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9983 			{
9984 			  lastprivate = false;
9985 			  outer = NULL;
9986 			}
9987 		      else if (outer->outer_context
9988 			       && (outer->outer_context->region_type
9989 				   == ORT_COMBINED_PARALLEL))
9990 			outer = outer->outer_context;
9991 		      else if (omp_check_private (outer, decl, false))
9992 			outer = NULL;
9993 		    }
9994 		  else if (((outer->region_type & ORT_TASK) != 0)
9995 			   && outer->combined_loop
9996 			   && !omp_check_private (gimplify_omp_ctxp,
9997 						  decl, false))
9998 		    ;
9999 		  else if (outer->region_type != ORT_COMBINED_PARALLEL)
10000 		    {
10001 		      omp_notice_variable (outer, decl, true);
10002 		      outer = NULL;
10003 		    }
10004 		  if (outer)
10005 		    {
10006 		      n = splay_tree_lookup (outer->variables,
10007 					     (splay_tree_key)decl);
10008 		      if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10009 			{
10010 			  omp_add_variable (outer, decl,
10011 					    GOVD_LASTPRIVATE | GOVD_SEEN);
10012 			  if (outer->region_type == ORT_COMBINED_PARALLEL
10013 			      && outer->outer_context
10014 			      && (outer->outer_context->region_type
10015 				  == ORT_WORKSHARE)
10016 			      && outer->outer_context->combined_loop)
10017 			    {
10018 			      outer = outer->outer_context;
10019 			      n = splay_tree_lookup (outer->variables,
10020 						     (splay_tree_key)decl);
10021 			      if (omp_check_private (outer, decl, false))
10022 				outer = NULL;
10023 			      else if (n == NULL
10024 				       || ((n->value & GOVD_DATA_SHARE_CLASS)
10025 					   == 0))
10026 				omp_add_variable (outer, decl,
10027 						  GOVD_LASTPRIVATE
10028 						  | GOVD_SEEN);
10029 			      else
10030 				outer = NULL;
10031 			    }
10032 			  if (outer && outer->outer_context
10033 			      && (outer->outer_context->region_type
10034 				  == ORT_COMBINED_TEAMS))
10035 			    {
10036 			      outer = outer->outer_context;
10037 			      n = splay_tree_lookup (outer->variables,
10038 						     (splay_tree_key)decl);
10039 			      if (n == NULL
10040 				  || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10041 				omp_add_variable (outer, decl,
10042 						  GOVD_SHARED | GOVD_SEEN);
10043 			      else
10044 				outer = NULL;
10045 			    }
10046 			  if (outer && outer->outer_context)
10047 			    omp_notice_variable (outer->outer_context, decl,
10048 						 true);
10049 			}
10050 		    }
10051 		}
10052 
10053 	      c = build_omp_clause (input_location,
10054 				    lastprivate ? OMP_CLAUSE_LASTPRIVATE
10055 						: OMP_CLAUSE_PRIVATE);
10056 	      OMP_CLAUSE_DECL (c) = decl;
10057 	      OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10058 	      OMP_FOR_CLAUSES (for_stmt) = c;
10059 	      omp_add_variable (gimplify_omp_ctxp, decl,
10060 				(lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
10061 				| GOVD_EXPLICIT | GOVD_SEEN);
10062 	      c = NULL_TREE;
10063 	    }
10064 	}
10065       else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
10066 	omp_notice_variable (gimplify_omp_ctxp, decl, true);
10067       else
10068 	omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
10069 
10070       /* If DECL is not a gimple register, create a temporary variable to act
10071 	 as an iteration counter.  This is valid, since DECL cannot be
10072 	 modified in the body of the loop.  Similarly for any iteration vars
10073 	 in simd with collapse > 1 where the iterator vars must be
10074 	 lastprivate.  */
10075       if (orig_for_stmt != for_stmt)
10076 	var = decl;
10077       else if (!is_gimple_reg (decl)
10078 	       || (ort == ORT_SIMD
10079 		   && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
10080 	{
10081 	  struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10082 	  /* Make sure omp_add_variable is not called on it prematurely.
10083 	     We call it ourselves a few lines later.  */
10084 	  gimplify_omp_ctxp = NULL;
10085 	  var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10086 	  gimplify_omp_ctxp = ctx;
10087 	  TREE_OPERAND (t, 0) = var;
10088 
10089 	  gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
10090 
10091 	  if (ort == ORT_SIMD
10092 	      && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10093 	    {
10094 	      c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10095 	      OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
10096 	      OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
10097 	      OMP_CLAUSE_DECL (c2) = var;
10098 	      OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
10099 	      OMP_FOR_CLAUSES (for_stmt) = c2;
10100 	      omp_add_variable (gimplify_omp_ctxp, var,
10101 				GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
10102 	      if (c == NULL_TREE)
10103 		{
10104 		  c = c2;
10105 		  c2 = NULL_TREE;
10106 		}
10107 	    }
10108 	  else
10109 	    omp_add_variable (gimplify_omp_ctxp, var,
10110 			      GOVD_PRIVATE | GOVD_SEEN);
10111 	}
10112       else
10113 	var = decl;
10114 
10115       tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10116 			    is_gimple_val, fb_rvalue, false);
10117       ret = MIN (ret, tret);
10118       if (ret == GS_ERROR)
10119 	return ret;
10120 
10121       /* Handle OMP_FOR_COND.  */
10122       t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10123       gcc_assert (COMPARISON_CLASS_P (t));
10124       gcc_assert (TREE_OPERAND (t, 0) == decl);
10125 
10126       tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10127 			    is_gimple_val, fb_rvalue, false);
10128       ret = MIN (ret, tret);
10129 
10130       /* Handle OMP_FOR_INCR.  */
10131       t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10132       switch (TREE_CODE (t))
10133 	{
10134 	case PREINCREMENT_EXPR:
10135 	case POSTINCREMENT_EXPR:
10136 	  {
10137 	    tree decl = TREE_OPERAND (t, 0);
10138 	    /* c_omp_for_incr_canonicalize_ptr() should have been
10139 	       called to massage things appropriately.  */
10140 	    gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10141 
10142 	    if (orig_for_stmt != for_stmt)
10143 	      break;
10144 	    t = build_int_cst (TREE_TYPE (decl), 1);
10145 	    if (c)
10146 	      OMP_CLAUSE_LINEAR_STEP (c) = t;
10147 	    t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10148 	    t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10149 	    TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10150 	    break;
10151 	  }
10152 
10153 	case PREDECREMENT_EXPR:
10154 	case POSTDECREMENT_EXPR:
10155 	  /* c_omp_for_incr_canonicalize_ptr() should have been
10156 	     called to massage things appropriately.  */
10157 	  gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10158 	  if (orig_for_stmt != for_stmt)
10159 	    break;
10160 	  t = build_int_cst (TREE_TYPE (decl), -1);
10161 	  if (c)
10162 	    OMP_CLAUSE_LINEAR_STEP (c) = t;
10163 	  t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10164 	  t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10165 	  TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10166 	  break;
10167 
10168 	case MODIFY_EXPR:
10169 	  gcc_assert (TREE_OPERAND (t, 0) == decl);
10170 	  TREE_OPERAND (t, 0) = var;
10171 
10172 	  t = TREE_OPERAND (t, 1);
10173 	  switch (TREE_CODE (t))
10174 	    {
10175 	    case PLUS_EXPR:
10176 	      if (TREE_OPERAND (t, 1) == decl)
10177 		{
10178 		  TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
10179 		  TREE_OPERAND (t, 0) = var;
10180 		  break;
10181 		}
10182 
10183 	      /* Fallthru.  */
10184 	    case MINUS_EXPR:
10185 	    case POINTER_PLUS_EXPR:
10186 	      gcc_assert (TREE_OPERAND (t, 0) == decl);
10187 	      TREE_OPERAND (t, 0) = var;
10188 	      break;
10189 	    default:
10190 	      gcc_unreachable ();
10191 	    }
10192 
10193 	  tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10194 				is_gimple_val, fb_rvalue, false);
10195 	  ret = MIN (ret, tret);
10196 	  if (c)
10197 	    {
10198 	      tree step = TREE_OPERAND (t, 1);
10199 	      tree stept = TREE_TYPE (decl);
10200 	      if (POINTER_TYPE_P (stept))
10201 		stept = sizetype;
10202 	      step = fold_convert (stept, step);
10203 	      if (TREE_CODE (t) == MINUS_EXPR)
10204 		step = fold_build1 (NEGATE_EXPR, stept, step);
10205 	      OMP_CLAUSE_LINEAR_STEP (c) = step;
10206 	      if (step != TREE_OPERAND (t, 1))
10207 		{
10208 		  tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
10209 					&for_pre_body, NULL,
10210 					is_gimple_val, fb_rvalue, false);
10211 		  ret = MIN (ret, tret);
10212 		}
10213 	    }
10214 	  break;
10215 
10216 	default:
10217 	  gcc_unreachable ();
10218 	}
10219 
10220       if (c2)
10221 	{
10222 	  gcc_assert (c);
10223 	  OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
10224 	}
10225 
10226       if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
10227 	{
10228 	  for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
10229 	    if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10230 		  && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
10231 		 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10232 		     && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
10233 		     && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
10234 		&& OMP_CLAUSE_DECL (c) == decl)
10235 	      {
10236 		if (is_doacross && (collapse == 1 || i >= collapse))
10237 		  t = var;
10238 		else
10239 		  {
10240 		    t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10241 		    gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10242 		    gcc_assert (TREE_OPERAND (t, 0) == var);
10243 		    t = TREE_OPERAND (t, 1);
10244 		    gcc_assert (TREE_CODE (t) == PLUS_EXPR
10245 				|| TREE_CODE (t) == MINUS_EXPR
10246 				|| TREE_CODE (t) == POINTER_PLUS_EXPR);
10247 		    gcc_assert (TREE_OPERAND (t, 0) == var);
10248 		    t = build2 (TREE_CODE (t), TREE_TYPE (decl),
10249 				is_doacross ? var : decl,
10250 				TREE_OPERAND (t, 1));
10251 		  }
10252 		gimple_seq *seq;
10253 		if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10254 		  seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
10255 		else
10256 		  seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
10257 		gimplify_assign (decl, t, seq);
10258 	    }
10259 	}
10260     }
10261 
10262   BITMAP_FREE (has_decl_expr);
10263 
10264   if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10265     {
10266       push_gimplify_context ();
10267       if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
10268 	{
10269 	  OMP_FOR_BODY (orig_for_stmt)
10270 	    = build3 (BIND_EXPR, void_type_node, NULL,
10271 		      OMP_FOR_BODY (orig_for_stmt), NULL);
10272 	  TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
10273 	}
10274     }
10275 
10276   gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
10277 					 &for_body);
10278 
10279   if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10280     {
10281       if (gimple_code (g) == GIMPLE_BIND)
10282 	pop_gimplify_context (g);
10283       else
10284 	pop_gimplify_context (NULL);
10285     }
10286 
10287   if (orig_for_stmt != for_stmt)
10288     for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10289       {
10290 	t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10291 	decl = TREE_OPERAND (t, 0);
10292 	struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10293 	if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10294 	  gimplify_omp_ctxp = ctx->outer_context;
10295 	var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10296 	gimplify_omp_ctxp = ctx;
10297 	omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
10298 	TREE_OPERAND (t, 0) = var;
10299 	t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10300 	TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
10301 	TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
10302       }
10303 
10304   gimplify_adjust_omp_clauses (pre_p, for_body,
10305 			       &OMP_FOR_CLAUSES (orig_for_stmt),
10306 			       TREE_CODE (orig_for_stmt));
10307 
10308   int kind;
10309   switch (TREE_CODE (orig_for_stmt))
10310     {
10311     case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
10312     case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
10313     case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
10314     case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
10315     case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
10316     default:
10317       gcc_unreachable ();
10318     }
10319   gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
10320 			       TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
10321 			       for_pre_body);
10322   if (orig_for_stmt != for_stmt)
10323     gimple_omp_for_set_combined_p (gfor, true);
10324   if (gimplify_omp_ctxp
10325       && (gimplify_omp_ctxp->combined_loop
10326 	  || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
10327 	      && gimplify_omp_ctxp->outer_context
10328 	      && gimplify_omp_ctxp->outer_context->combined_loop)))
10329     {
10330       gimple_omp_for_set_combined_into_p (gfor, true);
10331       if (gimplify_omp_ctxp->combined_loop)
10332 	gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
10333       else
10334 	gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
10335     }
10336 
10337   for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10338     {
10339       t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10340       gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
10341       gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
10342       t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10343       gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
10344       gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
10345       t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10346       gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
10347     }
10348 
10349   /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
10350      constructs with GIMPLE_OMP_TASK sandwiched in between them.
10351      The outer taskloop stands for computing the number of iterations,
10352      counts for collapsed loops and holding taskloop specific clauses.
10353      The task construct stands for the effect of data sharing on the
10354      explicit task it creates and the inner taskloop stands for expansion
10355      of the static loop inside of the explicit task construct.  */
10356   if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10357     {
10358       tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
10359       tree task_clauses = NULL_TREE;
10360       tree c = *gfor_clauses_ptr;
10361       tree *gtask_clauses_ptr = &task_clauses;
10362       tree outer_for_clauses = NULL_TREE;
10363       tree *gforo_clauses_ptr = &outer_for_clauses;
10364       for (; c; c = OMP_CLAUSE_CHAIN (c))
10365 	switch (OMP_CLAUSE_CODE (c))
10366 	  {
10367 	  /* These clauses are allowed on task, move them there.  */
10368 	  case OMP_CLAUSE_SHARED:
10369 	  case OMP_CLAUSE_FIRSTPRIVATE:
10370 	  case OMP_CLAUSE_DEFAULT:
10371 	  case OMP_CLAUSE_IF:
10372 	  case OMP_CLAUSE_UNTIED:
10373 	  case OMP_CLAUSE_FINAL:
10374 	  case OMP_CLAUSE_MERGEABLE:
10375 	  case OMP_CLAUSE_PRIORITY:
10376 	    *gtask_clauses_ptr = c;
10377 	    gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10378 	    break;
10379 	  case OMP_CLAUSE_PRIVATE:
10380 	    if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
10381 	      {
10382 		/* We want private on outer for and firstprivate
10383 		   on task.  */
10384 		*gtask_clauses_ptr
10385 		  = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10386 				      OMP_CLAUSE_FIRSTPRIVATE);
10387 		OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10388 		lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10389 		gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10390 		*gforo_clauses_ptr = c;
10391 		gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10392 	      }
10393 	    else
10394 	      {
10395 		*gtask_clauses_ptr = c;
10396 		gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10397 	      }
10398 	    break;
10399 	  /* These clauses go into outer taskloop clauses.  */
10400 	  case OMP_CLAUSE_GRAINSIZE:
10401 	  case OMP_CLAUSE_NUM_TASKS:
10402 	  case OMP_CLAUSE_NOGROUP:
10403 	    *gforo_clauses_ptr = c;
10404 	    gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10405 	    break;
10406 	  /* Taskloop clause we duplicate on both taskloops.  */
10407 	  case OMP_CLAUSE_COLLAPSE:
10408 	    *gfor_clauses_ptr = c;
10409 	    gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10410 	    *gforo_clauses_ptr = copy_node (c);
10411 	    gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10412 	    break;
10413 	  /* For lastprivate, keep the clause on inner taskloop, and add
10414 	     a shared clause on task.  If the same decl is also firstprivate,
10415 	     add also firstprivate clause on the inner taskloop.  */
10416 	  case OMP_CLAUSE_LASTPRIVATE:
10417 	    if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
10418 	      {
10419 		/* For taskloop C++ lastprivate IVs, we want:
10420 		   1) private on outer taskloop
10421 		   2) firstprivate and shared on task
10422 		   3) lastprivate on inner taskloop  */
10423 		*gtask_clauses_ptr
10424 		  = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10425 				      OMP_CLAUSE_FIRSTPRIVATE);
10426 		OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10427 		lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10428 		gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10429 		OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
10430 		*gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10431 						       OMP_CLAUSE_PRIVATE);
10432 		OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
10433 		OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
10434 		TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
10435 		gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10436 	      }
10437 	    *gfor_clauses_ptr = c;
10438 	    gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10439 	    *gtask_clauses_ptr
10440 	      = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
10441 	    OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10442 	    if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
10443 	      OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
10444 	    gtask_clauses_ptr
10445 	      = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10446 	    break;
10447 	  default:
10448 	    gcc_unreachable ();
10449 	  }
10450       *gfor_clauses_ptr = NULL_TREE;
10451       *gtask_clauses_ptr = NULL_TREE;
10452       *gforo_clauses_ptr = NULL_TREE;
10453       g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
10454       g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
10455 				 NULL_TREE, NULL_TREE, NULL_TREE);
10456       gimple_omp_task_set_taskloop_p (g, true);
10457       g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
10458       gomp_for *gforo
10459 	= gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
10460 				gimple_omp_for_collapse (gfor),
10461 				gimple_omp_for_pre_body (gfor));
10462       gimple_omp_for_set_pre_body (gfor, NULL);
10463       gimple_omp_for_set_combined_p (gforo, true);
10464       gimple_omp_for_set_combined_into_p (gfor, true);
10465       for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
10466 	{
10467 	  tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
10468 	  tree v = create_tmp_var (type);
10469 	  gimple_omp_for_set_index (gforo, i, v);
10470 	  t = unshare_expr (gimple_omp_for_initial (gfor, i));
10471 	  gimple_omp_for_set_initial (gforo, i, t);
10472 	  gimple_omp_for_set_cond (gforo, i,
10473 				   gimple_omp_for_cond (gfor, i));
10474 	  t = unshare_expr (gimple_omp_for_final (gfor, i));
10475 	  gimple_omp_for_set_final (gforo, i, t);
10476 	  t = unshare_expr (gimple_omp_for_incr (gfor, i));
10477 	  gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
10478 	  TREE_OPERAND (t, 0) = v;
10479 	  gimple_omp_for_set_incr (gforo, i, t);
10480 	  t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
10481 	  OMP_CLAUSE_DECL (t) = v;
10482 	  OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
10483 	  gimple_omp_for_set_clauses (gforo, t);
10484 	}
10485       gimplify_seq_add_stmt (pre_p, gforo);
10486     }
10487   else
10488     gimplify_seq_add_stmt (pre_p, gfor);
10489   if (ret != GS_ALL_DONE)
10490     return GS_ERROR;
10491   *expr_p = NULL_TREE;
10492   return GS_ALL_DONE;
10493 }
10494 
10495 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10496    of OMP_TARGET's body.  */
10497 
10498 static tree
10499 find_omp_teams (tree *tp, int *walk_subtrees, void *)
10500 {
10501   *walk_subtrees = 0;
10502   switch (TREE_CODE (*tp))
10503     {
10504     case OMP_TEAMS:
10505       return *tp;
10506     case BIND_EXPR:
10507     case STATEMENT_LIST:
10508       *walk_subtrees = 1;
10509       break;
10510     default:
10511       break;
10512     }
10513   return NULL_TREE;
10514 }
10515 
10516 /* Helper function of optimize_target_teams, determine if the expression
10517    can be computed safely before the target construct on the host.  */
10518 
10519 static tree
10520 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
10521 {
10522   splay_tree_node n;
10523 
10524   if (TYPE_P (*tp))
10525     {
10526       *walk_subtrees = 0;
10527       return NULL_TREE;
10528     }
10529   switch (TREE_CODE (*tp))
10530     {
10531     case VAR_DECL:
10532     case PARM_DECL:
10533     case RESULT_DECL:
10534       *walk_subtrees = 0;
10535       if (error_operand_p (*tp)
10536 	  || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
10537 	  || DECL_HAS_VALUE_EXPR_P (*tp)
10538 	  || DECL_THREAD_LOCAL_P (*tp)
10539 	  || TREE_SIDE_EFFECTS (*tp)
10540 	  || TREE_THIS_VOLATILE (*tp))
10541 	return *tp;
10542       if (is_global_var (*tp)
10543 	  && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
10544 	      || lookup_attribute ("omp declare target link",
10545 				   DECL_ATTRIBUTES (*tp))))
10546 	return *tp;
10547       if (VAR_P (*tp)
10548 	  && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
10549 	  && !is_global_var (*tp)
10550 	  && decl_function_context (*tp) == current_function_decl)
10551 	return *tp;
10552       n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10553 			     (splay_tree_key) *tp);
10554       if (n == NULL)
10555 	{
10556 	  if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
10557 	    return NULL_TREE;
10558 	  return *tp;
10559 	}
10560       else if (n->value & GOVD_LOCAL)
10561 	return *tp;
10562       else if (n->value & GOVD_FIRSTPRIVATE)
10563 	return NULL_TREE;
10564       else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10565 	       == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10566 	return NULL_TREE;
10567       return *tp;
10568     case INTEGER_CST:
10569       if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10570 	return *tp;
10571       return NULL_TREE;
10572     case TARGET_EXPR:
10573       if (TARGET_EXPR_INITIAL (*tp)
10574 	  || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
10575 	return *tp;
10576       return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
10577 				      walk_subtrees, NULL);
10578     /* Allow some reasonable subset of integral arithmetics.  */
10579     case PLUS_EXPR:
10580     case MINUS_EXPR:
10581     case MULT_EXPR:
10582     case TRUNC_DIV_EXPR:
10583     case CEIL_DIV_EXPR:
10584     case FLOOR_DIV_EXPR:
10585     case ROUND_DIV_EXPR:
10586     case TRUNC_MOD_EXPR:
10587     case CEIL_MOD_EXPR:
10588     case FLOOR_MOD_EXPR:
10589     case ROUND_MOD_EXPR:
10590     case RDIV_EXPR:
10591     case EXACT_DIV_EXPR:
10592     case MIN_EXPR:
10593     case MAX_EXPR:
10594     case LSHIFT_EXPR:
10595     case RSHIFT_EXPR:
10596     case BIT_IOR_EXPR:
10597     case BIT_XOR_EXPR:
10598     case BIT_AND_EXPR:
10599     case NEGATE_EXPR:
10600     case ABS_EXPR:
10601     case BIT_NOT_EXPR:
10602     case NON_LVALUE_EXPR:
10603     CASE_CONVERT:
10604       if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10605 	return *tp;
10606       return NULL_TREE;
10607     /* And disallow anything else, except for comparisons.  */
10608     default:
10609       if (COMPARISON_CLASS_P (*tp))
10610 	return NULL_TREE;
10611       return *tp;
10612     }
10613 }
10614 
10615 /* Try to determine if the num_teams and/or thread_limit expressions
10616    can have their values determined already before entering the
10617    target construct.
10618    INTEGER_CSTs trivially are,
10619    integral decls that are firstprivate (explicitly or implicitly)
10620    or explicitly map(always, to:) or map(always, tofrom:) on the target
10621    region too, and expressions involving simple arithmetics on those
10622    too, function calls are not ok, dereferencing something neither etc.
10623    Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10624    EXPR based on what we find:
10625    0 stands for clause not specified at all, use implementation default
10626    -1 stands for value that can't be determined easily before entering
10627       the target construct.
10628    If teams construct is not present at all, use 1 for num_teams
10629    and 0 for thread_limit (only one team is involved, and the thread
10630    limit is implementation defined.  */
10631 
10632 static void
10633 optimize_target_teams (tree target, gimple_seq *pre_p)
10634 {
10635   tree body = OMP_BODY (target);
10636   tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
10637   tree num_teams = integer_zero_node;
10638   tree thread_limit = integer_zero_node;
10639   location_t num_teams_loc = EXPR_LOCATION (target);
10640   location_t thread_limit_loc = EXPR_LOCATION (target);
10641   tree c, *p, expr;
10642   struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
10643 
10644   if (teams == NULL_TREE)
10645     num_teams = integer_one_node;
10646   else
10647     for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
10648       {
10649 	if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
10650 	  {
10651 	    p = &num_teams;
10652 	    num_teams_loc = OMP_CLAUSE_LOCATION (c);
10653 	  }
10654 	else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
10655 	  {
10656 	    p = &thread_limit;
10657 	    thread_limit_loc = OMP_CLAUSE_LOCATION (c);
10658 	  }
10659 	else
10660 	  continue;
10661 	expr = OMP_CLAUSE_OPERAND (c, 0);
10662 	if (TREE_CODE (expr) == INTEGER_CST)
10663 	  {
10664 	    *p = expr;
10665 	    continue;
10666 	  }
10667 	if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
10668 	  {
10669 	    *p = integer_minus_one_node;
10670 	    continue;
10671 	  }
10672 	*p = expr;
10673 	gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
10674 	if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
10675 	    == GS_ERROR)
10676 	  {
10677 	    gimplify_omp_ctxp = target_ctx;
10678 	    *p = integer_minus_one_node;
10679 	    continue;
10680 	  }
10681 	gimplify_omp_ctxp = target_ctx;
10682 	if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
10683 	  OMP_CLAUSE_OPERAND (c, 0) = *p;
10684       }
10685   c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10686   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
10687   OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10688   OMP_TARGET_CLAUSES (target) = c;
10689   c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10690   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
10691   OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10692   OMP_TARGET_CLAUSES (target) = c;
10693 }
10694 
10695 /* Gimplify the gross structure of several OMP constructs.  */
10696 
10697 static void
10698 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
10699 {
10700   tree expr = *expr_p;
10701   gimple *stmt;
10702   gimple_seq body = NULL;
10703   enum omp_region_type ort;
10704 
10705   switch (TREE_CODE (expr))
10706     {
10707     case OMP_SECTIONS:
10708     case OMP_SINGLE:
10709       ort = ORT_WORKSHARE;
10710       break;
10711     case OMP_TARGET:
10712       ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
10713       break;
10714     case OACC_KERNELS:
10715       ort = ORT_ACC_KERNELS;
10716       break;
10717     case OACC_PARALLEL:
10718       ort = ORT_ACC_PARALLEL;
10719       break;
10720     case OACC_DATA:
10721       ort = ORT_ACC_DATA;
10722       break;
10723     case OMP_TARGET_DATA:
10724       ort = ORT_TARGET_DATA;
10725       break;
10726     case OMP_TEAMS:
10727       ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
10728       break;
10729     case OACC_HOST_DATA:
10730       ort = ORT_ACC_HOST_DATA;
10731       break;
10732     default:
10733       gcc_unreachable ();
10734     }
10735   gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
10736 			     TREE_CODE (expr));
10737   if (TREE_CODE (expr) == OMP_TARGET)
10738     optimize_target_teams (expr, pre_p);
10739   if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
10740     {
10741       push_gimplify_context ();
10742       gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
10743       if (gimple_code (g) == GIMPLE_BIND)
10744 	pop_gimplify_context (g);
10745       else
10746 	pop_gimplify_context (NULL);
10747       if ((ort & ORT_TARGET_DATA) != 0)
10748 	{
10749 	  enum built_in_function end_ix;
10750 	  switch (TREE_CODE (expr))
10751 	    {
10752 	    case OACC_DATA:
10753 	    case OACC_HOST_DATA:
10754 	      end_ix = BUILT_IN_GOACC_DATA_END;
10755 	      break;
10756 	    case OMP_TARGET_DATA:
10757 	      end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
10758 	      break;
10759 	    default:
10760 	      gcc_unreachable ();
10761 	    }
10762 	  tree fn = builtin_decl_explicit (end_ix);
10763 	  g = gimple_build_call (fn, 0);
10764 	  gimple_seq cleanup = NULL;
10765 	  gimple_seq_add_stmt (&cleanup, g);
10766 	  g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10767 	  body = NULL;
10768 	  gimple_seq_add_stmt (&body, g);
10769 	}
10770     }
10771   else
10772     gimplify_and_add (OMP_BODY (expr), &body);
10773   gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
10774 			       TREE_CODE (expr));
10775 
10776   switch (TREE_CODE (expr))
10777     {
10778     case OACC_DATA:
10779       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
10780 				      OMP_CLAUSES (expr));
10781       break;
10782     case OACC_KERNELS:
10783       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
10784 				      OMP_CLAUSES (expr));
10785       break;
10786     case OACC_HOST_DATA:
10787       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
10788 				      OMP_CLAUSES (expr));
10789       break;
10790     case OACC_PARALLEL:
10791       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
10792 				      OMP_CLAUSES (expr));
10793       break;
10794     case OMP_SECTIONS:
10795       stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
10796       break;
10797     case OMP_SINGLE:
10798       stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
10799       break;
10800     case OMP_TARGET:
10801       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
10802 				      OMP_CLAUSES (expr));
10803       break;
10804     case OMP_TARGET_DATA:
10805       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
10806 				      OMP_CLAUSES (expr));
10807       break;
10808     case OMP_TEAMS:
10809       stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
10810       break;
10811     default:
10812       gcc_unreachable ();
10813     }
10814 
10815   gimplify_seq_add_stmt (pre_p, stmt);
10816   *expr_p = NULL_TREE;
10817 }
10818 
10819 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10820    target update constructs.  */
10821 
10822 static void
10823 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
10824 {
10825   tree expr = *expr_p;
10826   int kind;
10827   gomp_target *stmt;
10828   enum omp_region_type ort = ORT_WORKSHARE;
10829 
10830   switch (TREE_CODE (expr))
10831     {
10832     case OACC_ENTER_DATA:
10833     case OACC_EXIT_DATA:
10834       kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
10835       ort = ORT_ACC;
10836       break;
10837     case OACC_UPDATE:
10838       kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
10839       ort = ORT_ACC;
10840       break;
10841     case OMP_TARGET_UPDATE:
10842       kind = GF_OMP_TARGET_KIND_UPDATE;
10843       break;
10844     case OMP_TARGET_ENTER_DATA:
10845       kind = GF_OMP_TARGET_KIND_ENTER_DATA;
10846       break;
10847     case OMP_TARGET_EXIT_DATA:
10848       kind = GF_OMP_TARGET_KIND_EXIT_DATA;
10849       break;
10850     default:
10851       gcc_unreachable ();
10852     }
10853   gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
10854 			     ort, TREE_CODE (expr));
10855   gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
10856 			       TREE_CODE (expr));
10857   stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
10858 
10859   gimplify_seq_add_stmt (pre_p, stmt);
10860   *expr_p = NULL_TREE;
10861 }
10862 
10863 /* A subroutine of gimplify_omp_atomic.  The front end is supposed to have
10864    stabilized the lhs of the atomic operation as *ADDR.  Return true if
10865    EXPR is this stabilized form.  */
10866 
10867 static bool
10868 goa_lhs_expr_p (tree expr, tree addr)
10869 {
10870   /* Also include casts to other type variants.  The C front end is fond
10871      of adding these for e.g. volatile variables.  This is like
10872      STRIP_TYPE_NOPS but includes the main variant lookup.  */
10873   STRIP_USELESS_TYPE_CONVERSION (expr);
10874 
10875   if (TREE_CODE (expr) == INDIRECT_REF)
10876     {
10877       expr = TREE_OPERAND (expr, 0);
10878       while (expr != addr
10879 	     && (CONVERT_EXPR_P (expr)
10880 		 || TREE_CODE (expr) == NON_LVALUE_EXPR)
10881 	     && TREE_CODE (expr) == TREE_CODE (addr)
10882 	     && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
10883 	{
10884 	  expr = TREE_OPERAND (expr, 0);
10885 	  addr = TREE_OPERAND (addr, 0);
10886 	}
10887       if (expr == addr)
10888 	return true;
10889       return (TREE_CODE (addr) == ADDR_EXPR
10890 	      && TREE_CODE (expr) == ADDR_EXPR
10891 	      && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
10892     }
10893   if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
10894     return true;
10895   return false;
10896 }
10897 
10898 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR.  If an
10899    expression does not involve the lhs, evaluate it into a temporary.
10900    Return 1 if the lhs appeared as a subexpression, 0 if it did not,
10901    or -1 if an error was encountered.  */
10902 
10903 static int
10904 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
10905 		    tree lhs_var)
10906 {
10907   tree expr = *expr_p;
10908   int saw_lhs;
10909 
10910   if (goa_lhs_expr_p (expr, lhs_addr))
10911     {
10912       *expr_p = lhs_var;
10913       return 1;
10914     }
10915   if (is_gimple_val (expr))
10916     return 0;
10917 
10918   saw_lhs = 0;
10919   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
10920     {
10921     case tcc_binary:
10922     case tcc_comparison:
10923       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
10924 				     lhs_var);
10925       /* FALLTHRU */
10926     case tcc_unary:
10927       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
10928 				     lhs_var);
10929       break;
10930     case tcc_expression:
10931       switch (TREE_CODE (expr))
10932 	{
10933 	case TRUTH_ANDIF_EXPR:
10934 	case TRUTH_ORIF_EXPR:
10935 	case TRUTH_AND_EXPR:
10936 	case TRUTH_OR_EXPR:
10937 	case TRUTH_XOR_EXPR:
10938 	case BIT_INSERT_EXPR:
10939 	  saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
10940 					 lhs_addr, lhs_var);
10941 	  /* FALLTHRU */
10942 	case TRUTH_NOT_EXPR:
10943 	  saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
10944 					 lhs_addr, lhs_var);
10945 	  break;
10946 	case COMPOUND_EXPR:
10947 	  /* Break out any preevaluations from cp_build_modify_expr.  */
10948 	  for (; TREE_CODE (expr) == COMPOUND_EXPR;
10949 	       expr = TREE_OPERAND (expr, 1))
10950 	    gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
10951 	  *expr_p = expr;
10952 	  return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
10953 	default:
10954 	  break;
10955 	}
10956       break;
10957     case tcc_reference:
10958       if (TREE_CODE (expr) == BIT_FIELD_REF)
10959 	saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
10960 				       lhs_addr, lhs_var);
10961       break;
10962     default:
10963       break;
10964     }
10965 
10966   if (saw_lhs == 0)
10967     {
10968       enum gimplify_status gs;
10969       gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
10970       if (gs != GS_ALL_DONE)
10971 	saw_lhs = -1;
10972     }
10973 
10974   return saw_lhs;
10975 }
10976 
10977 /* Gimplify an OMP_ATOMIC statement.  */
10978 
10979 static enum gimplify_status
10980 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
10981 {
10982   tree addr = TREE_OPERAND (*expr_p, 0);
10983   tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
10984 	     ? NULL : TREE_OPERAND (*expr_p, 1);
10985   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
10986   tree tmp_load;
10987   gomp_atomic_load *loadstmt;
10988   gomp_atomic_store *storestmt;
10989 
10990   tmp_load = create_tmp_reg (type);
10991   if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
10992     return GS_ERROR;
10993 
10994   if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
10995       != GS_ALL_DONE)
10996     return GS_ERROR;
10997 
10998   loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
10999   gimplify_seq_add_stmt (pre_p, loadstmt);
11000   if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
11001       != GS_ALL_DONE)
11002     return GS_ERROR;
11003 
11004   if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
11005     rhs = tmp_load;
11006   storestmt = gimple_build_omp_atomic_store (rhs);
11007   gimplify_seq_add_stmt (pre_p, storestmt);
11008   if (OMP_ATOMIC_SEQ_CST (*expr_p))
11009     {
11010       gimple_omp_atomic_set_seq_cst (loadstmt);
11011       gimple_omp_atomic_set_seq_cst (storestmt);
11012     }
11013   switch (TREE_CODE (*expr_p))
11014     {
11015     case OMP_ATOMIC_READ:
11016     case OMP_ATOMIC_CAPTURE_OLD:
11017       *expr_p = tmp_load;
11018       gimple_omp_atomic_set_need_value (loadstmt);
11019       break;
11020     case OMP_ATOMIC_CAPTURE_NEW:
11021       *expr_p = rhs;
11022       gimple_omp_atomic_set_need_value (storestmt);
11023       break;
11024     default:
11025       *expr_p = NULL;
11026       break;
11027     }
11028 
11029   return GS_ALL_DONE;
11030 }
11031 
11032 /* Gimplify a TRANSACTION_EXPR.  This involves gimplification of the
11033    body, and adding some EH bits.  */
11034 
11035 static enum gimplify_status
11036 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
11037 {
11038   tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
11039   gimple *body_stmt;
11040   gtransaction *trans_stmt;
11041   gimple_seq body = NULL;
11042   int subcode = 0;
11043 
11044   /* Wrap the transaction body in a BIND_EXPR so we have a context
11045      where to put decls for OMP.  */
11046   if (TREE_CODE (tbody) != BIND_EXPR)
11047     {
11048       tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
11049       TREE_SIDE_EFFECTS (bind) = 1;
11050       SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
11051       TRANSACTION_EXPR_BODY (expr) = bind;
11052     }
11053 
11054   push_gimplify_context ();
11055   temp = voidify_wrapper_expr (*expr_p, NULL);
11056 
11057   body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
11058   pop_gimplify_context (body_stmt);
11059 
11060   trans_stmt = gimple_build_transaction (body);
11061   if (TRANSACTION_EXPR_OUTER (expr))
11062     subcode = GTMA_IS_OUTER;
11063   else if (TRANSACTION_EXPR_RELAXED (expr))
11064     subcode = GTMA_IS_RELAXED;
11065   gimple_transaction_set_subcode (trans_stmt, subcode);
11066 
11067   gimplify_seq_add_stmt (pre_p, trans_stmt);
11068 
11069   if (temp)
11070     {
11071       *expr_p = temp;
11072       return GS_OK;
11073     }
11074 
11075   *expr_p = NULL_TREE;
11076   return GS_ALL_DONE;
11077 }
11078 
11079 /* Gimplify an OMP_ORDERED construct.  EXPR is the tree version.  BODY
11080    is the OMP_BODY of the original EXPR (which has already been
11081    gimplified so it's not present in the EXPR).
11082 
11083    Return the gimplified GIMPLE_OMP_ORDERED tuple.  */
11084 
11085 static gimple *
11086 gimplify_omp_ordered (tree expr, gimple_seq body)
11087 {
11088   tree c, decls;
11089   int failures = 0;
11090   unsigned int i;
11091   tree source_c = NULL_TREE;
11092   tree sink_c = NULL_TREE;
11093 
11094   if (gimplify_omp_ctxp)
11095     {
11096       for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11097 	if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11098 	    && gimplify_omp_ctxp->loop_iter_var.is_empty ()
11099 	    && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
11100 		|| OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
11101 	  {
11102 	    error_at (OMP_CLAUSE_LOCATION (c),
11103 		      "%<ordered%> construct with %<depend%> clause must be "
11104 		      "closely nested inside a loop with %<ordered%> clause "
11105 		      "with a parameter");
11106 	    failures++;
11107 	  }
11108 	else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11109 		 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
11110 	  {
11111 	    bool fail = false;
11112 	    for (decls = OMP_CLAUSE_DECL (c), i = 0;
11113 		 decls && TREE_CODE (decls) == TREE_LIST;
11114 		 decls = TREE_CHAIN (decls), ++i)
11115 	      if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
11116 		continue;
11117 	      else if (TREE_VALUE (decls)
11118 		       != gimplify_omp_ctxp->loop_iter_var[2 * i])
11119 		{
11120 		  error_at (OMP_CLAUSE_LOCATION (c),
11121 			    "variable %qE is not an iteration "
11122 			    "of outermost loop %d, expected %qE",
11123 			    TREE_VALUE (decls), i + 1,
11124 			    gimplify_omp_ctxp->loop_iter_var[2 * i]);
11125 		  fail = true;
11126 		  failures++;
11127 		}
11128 	      else
11129 		TREE_VALUE (decls)
11130 		  = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
11131 	    if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
11132 	      {
11133 		error_at (OMP_CLAUSE_LOCATION (c),
11134 			  "number of variables in %<depend(sink)%> "
11135 			  "clause does not match number of "
11136 			  "iteration variables");
11137 		failures++;
11138 	      }
11139 	    sink_c = c;
11140 	  }
11141 	else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11142 		 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
11143 	  {
11144 	    if (source_c)
11145 	      {
11146 		error_at (OMP_CLAUSE_LOCATION (c),
11147 			  "more than one %<depend(source)%> clause on an "
11148 			  "%<ordered%> construct");
11149 		failures++;
11150 	      }
11151 	    else
11152 	      source_c = c;
11153 	  }
11154     }
11155   if (source_c && sink_c)
11156     {
11157       error_at (OMP_CLAUSE_LOCATION (source_c),
11158 		"%<depend(source)%> clause specified together with "
11159 		"%<depend(sink:)%> clauses on the same construct");
11160       failures++;
11161     }
11162 
11163   if (failures)
11164     return gimple_build_nop ();
11165   return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
11166 }
11167 
11168 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE.  If the
11169    expression produces a value to be used as an operand inside a GIMPLE
11170    statement, the value will be stored back in *EXPR_P.  This value will
11171    be a tree of class tcc_declaration, tcc_constant, tcc_reference or
11172    an SSA_NAME.  The corresponding sequence of GIMPLE statements is
11173    emitted in PRE_P and POST_P.
11174 
11175    Additionally, this process may overwrite parts of the input
11176    expression during gimplification.  Ideally, it should be
11177    possible to do non-destructive gimplification.
11178 
11179    EXPR_P points to the GENERIC expression to convert to GIMPLE.  If
11180       the expression needs to evaluate to a value to be used as
11181       an operand in a GIMPLE statement, this value will be stored in
11182       *EXPR_P on exit.  This happens when the caller specifies one
11183       of fb_lvalue or fb_rvalue fallback flags.
11184 
11185    PRE_P will contain the sequence of GIMPLE statements corresponding
11186        to the evaluation of EXPR and all the side-effects that must
11187        be executed before the main expression.  On exit, the last
11188        statement of PRE_P is the core statement being gimplified.  For
11189        instance, when gimplifying 'if (++a)' the last statement in
11190        PRE_P will be 'if (t.1)' where t.1 is the result of
11191        pre-incrementing 'a'.
11192 
11193    POST_P will contain the sequence of GIMPLE statements corresponding
11194        to the evaluation of all the side-effects that must be executed
11195        after the main expression.  If this is NULL, the post
11196        side-effects are stored at the end of PRE_P.
11197 
11198        The reason why the output is split in two is to handle post
11199        side-effects explicitly.  In some cases, an expression may have
11200        inner and outer post side-effects which need to be emitted in
11201        an order different from the one given by the recursive
11202        traversal.  For instance, for the expression (*p--)++ the post
11203        side-effects of '--' must actually occur *after* the post
11204        side-effects of '++'.  However, gimplification will first visit
11205        the inner expression, so if a separate POST sequence was not
11206        used, the resulting sequence would be:
11207 
11208        	    1	t.1 = *p
11209        	    2	p = p - 1
11210        	    3	t.2 = t.1 + 1
11211        	    4	*p = t.2
11212 
11213        However, the post-decrement operation in line #2 must not be
11214        evaluated until after the store to *p at line #4, so the
11215        correct sequence should be:
11216 
11217        	    1	t.1 = *p
11218        	    2	t.2 = t.1 + 1
11219        	    3	*p = t.2
11220        	    4	p = p - 1
11221 
11222        So, by specifying a separate post queue, it is possible
11223        to emit the post side-effects in the correct order.
11224        If POST_P is NULL, an internal queue will be used.  Before
11225        returning to the caller, the sequence POST_P is appended to
11226        the main output sequence PRE_P.
11227 
11228    GIMPLE_TEST_F points to a function that takes a tree T and
11229        returns nonzero if T is in the GIMPLE form requested by the
11230        caller.  The GIMPLE predicates are in gimple.c.
11231 
11232    FALLBACK tells the function what sort of a temporary we want if
11233        gimplification cannot produce an expression that complies with
11234        GIMPLE_TEST_F.
11235 
11236        fb_none means that no temporary should be generated
11237        fb_rvalue means that an rvalue is OK to generate
11238        fb_lvalue means that an lvalue is OK to generate
11239        fb_either means that either is OK, but an lvalue is preferable.
11240        fb_mayfail means that gimplification may fail (in which case
11241        GS_ERROR will be returned)
11242 
11243    The return value is either GS_ERROR or GS_ALL_DONE, since this
11244    function iterates until EXPR is completely gimplified or an error
11245    occurs.  */
11246 
11247 enum gimplify_status
11248 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
11249 	       bool (*gimple_test_f) (tree), fallback_t fallback)
11250 {
11251   tree tmp;
11252   gimple_seq internal_pre = NULL;
11253   gimple_seq internal_post = NULL;
11254   tree save_expr;
11255   bool is_statement;
11256   location_t saved_location;
11257   enum gimplify_status ret;
11258   gimple_stmt_iterator pre_last_gsi, post_last_gsi;
11259   tree label;
11260 
11261   save_expr = *expr_p;
11262   if (save_expr == NULL_TREE)
11263     return GS_ALL_DONE;
11264 
11265   /* If we are gimplifying a top-level statement, PRE_P must be valid.  */
11266   is_statement = gimple_test_f == is_gimple_stmt;
11267   if (is_statement)
11268     gcc_assert (pre_p);
11269 
11270   /* Consistency checks.  */
11271   if (gimple_test_f == is_gimple_reg)
11272     gcc_assert (fallback & (fb_rvalue | fb_lvalue));
11273   else if (gimple_test_f == is_gimple_val
11274            || gimple_test_f == is_gimple_call_addr
11275            || gimple_test_f == is_gimple_condexpr
11276            || gimple_test_f == is_gimple_mem_rhs
11277            || gimple_test_f == is_gimple_mem_rhs_or_call
11278            || gimple_test_f == is_gimple_reg_rhs
11279            || gimple_test_f == is_gimple_reg_rhs_or_call
11280            || gimple_test_f == is_gimple_asm_val
11281 	   || gimple_test_f == is_gimple_mem_ref_addr)
11282     gcc_assert (fallback & fb_rvalue);
11283   else if (gimple_test_f == is_gimple_min_lval
11284 	   || gimple_test_f == is_gimple_lvalue)
11285     gcc_assert (fallback & fb_lvalue);
11286   else if (gimple_test_f == is_gimple_addressable)
11287     gcc_assert (fallback & fb_either);
11288   else if (gimple_test_f == is_gimple_stmt)
11289     gcc_assert (fallback == fb_none);
11290   else
11291     {
11292       /* We should have recognized the GIMPLE_TEST_F predicate to
11293 	 know what kind of fallback to use in case a temporary is
11294 	 needed to hold the value or address of *EXPR_P.  */
11295       gcc_unreachable ();
11296     }
11297 
11298   /* We used to check the predicate here and return immediately if it
11299      succeeds.  This is wrong; the design is for gimplification to be
11300      idempotent, and for the predicates to only test for valid forms, not
11301      whether they are fully simplified.  */
11302   if (pre_p == NULL)
11303     pre_p = &internal_pre;
11304 
11305   if (post_p == NULL)
11306     post_p = &internal_post;
11307 
11308   /* Remember the last statements added to PRE_P and POST_P.  Every
11309      new statement added by the gimplification helpers needs to be
11310      annotated with location information.  To centralize the
11311      responsibility, we remember the last statement that had been
11312      added to both queues before gimplifying *EXPR_P.  If
11313      gimplification produces new statements in PRE_P and POST_P, those
11314      statements will be annotated with the same location information
11315      as *EXPR_P.  */
11316   pre_last_gsi = gsi_last (*pre_p);
11317   post_last_gsi = gsi_last (*post_p);
11318 
11319   saved_location = input_location;
11320   if (save_expr != error_mark_node
11321       && EXPR_HAS_LOCATION (*expr_p))
11322     input_location = EXPR_LOCATION (*expr_p);
11323 
11324   /* Loop over the specific gimplifiers until the toplevel node
11325      remains the same.  */
11326   do
11327     {
11328       /* Strip away as many useless type conversions as possible
11329 	 at the toplevel.  */
11330       STRIP_USELESS_TYPE_CONVERSION (*expr_p);
11331 
11332       /* Remember the expr.  */
11333       save_expr = *expr_p;
11334 
11335       /* Die, die, die, my darling.  */
11336       if (error_operand_p (save_expr))
11337 	{
11338 	  ret = GS_ERROR;
11339 	  break;
11340 	}
11341 
11342       /* Do any language-specific gimplification.  */
11343       ret = ((enum gimplify_status)
11344 	     lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
11345       if (ret == GS_OK)
11346 	{
11347 	  if (*expr_p == NULL_TREE)
11348 	    break;
11349 	  if (*expr_p != save_expr)
11350 	    continue;
11351 	}
11352       else if (ret != GS_UNHANDLED)
11353 	break;
11354 
11355       /* Make sure that all the cases set 'ret' appropriately.  */
11356       ret = GS_UNHANDLED;
11357       switch (TREE_CODE (*expr_p))
11358 	{
11359 	  /* First deal with the special cases.  */
11360 
11361 	case POSTINCREMENT_EXPR:
11362 	case POSTDECREMENT_EXPR:
11363 	case PREINCREMENT_EXPR:
11364 	case PREDECREMENT_EXPR:
11365 	  ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
11366 					fallback != fb_none,
11367 					TREE_TYPE (*expr_p));
11368 	  break;
11369 
11370 	case VIEW_CONVERT_EXPR:
11371 	  if (is_gimple_reg_type (TREE_TYPE (*expr_p))
11372 	      && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
11373 	    {
11374 	      ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11375 				   post_p, is_gimple_val, fb_rvalue);
11376 	      recalculate_side_effects (*expr_p);
11377 	      break;
11378 	    }
11379 	  /* Fallthru.  */
11380 
11381 	case ARRAY_REF:
11382 	case ARRAY_RANGE_REF:
11383 	case REALPART_EXPR:
11384 	case IMAGPART_EXPR:
11385 	case COMPONENT_REF:
11386 	  ret = gimplify_compound_lval (expr_p, pre_p, post_p,
11387 					fallback ? fallback : fb_rvalue);
11388 	  break;
11389 
11390 	case COND_EXPR:
11391 	  ret = gimplify_cond_expr (expr_p, pre_p, fallback);
11392 
11393 	  /* C99 code may assign to an array in a structure value of a
11394 	     conditional expression, and this has undefined behavior
11395 	     only on execution, so create a temporary if an lvalue is
11396 	     required.  */
11397 	  if (fallback == fb_lvalue)
11398 	    {
11399 	      *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11400 	      mark_addressable (*expr_p);
11401 	      ret = GS_OK;
11402 	    }
11403 	  break;
11404 
11405 	case CALL_EXPR:
11406 	  ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
11407 
11408 	  /* C99 code may assign to an array in a structure returned
11409 	     from a function, and this has undefined behavior only on
11410 	     execution, so create a temporary if an lvalue is
11411 	     required.  */
11412 	  if (fallback == fb_lvalue)
11413 	    {
11414 	      *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11415 	      mark_addressable (*expr_p);
11416 	      ret = GS_OK;
11417 	    }
11418 	  break;
11419 
11420 	case TREE_LIST:
11421 	  gcc_unreachable ();
11422 
11423 	case COMPOUND_EXPR:
11424 	  ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
11425 	  break;
11426 
11427 	case COMPOUND_LITERAL_EXPR:
11428 	  ret = gimplify_compound_literal_expr (expr_p, pre_p,
11429 						gimple_test_f, fallback);
11430 	  break;
11431 
11432 	case MODIFY_EXPR:
11433 	case INIT_EXPR:
11434 	  ret = gimplify_modify_expr (expr_p, pre_p, post_p,
11435 				      fallback != fb_none);
11436 	  break;
11437 
11438 	case TRUTH_ANDIF_EXPR:
11439 	case TRUTH_ORIF_EXPR:
11440 	  {
11441 	    /* Preserve the original type of the expression and the
11442 	       source location of the outer expression.  */
11443 	    tree org_type = TREE_TYPE (*expr_p);
11444 	    *expr_p = gimple_boolify (*expr_p);
11445 	    *expr_p = build3_loc (input_location, COND_EXPR,
11446 				  org_type, *expr_p,
11447 				  fold_convert_loc
11448 				    (input_location,
11449 				     org_type, boolean_true_node),
11450 				  fold_convert_loc
11451 				    (input_location,
11452 				     org_type, boolean_false_node));
11453 	    ret = GS_OK;
11454 	    break;
11455 	  }
11456 
11457 	case TRUTH_NOT_EXPR:
11458 	  {
11459 	    tree type = TREE_TYPE (*expr_p);
11460 	    /* The parsers are careful to generate TRUTH_NOT_EXPR
11461 	       only with operands that are always zero or one.
11462 	       We do not fold here but handle the only interesting case
11463 	       manually, as fold may re-introduce the TRUTH_NOT_EXPR.  */
11464 	    *expr_p = gimple_boolify (*expr_p);
11465 	    if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
11466 	      *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
11467 				    TREE_TYPE (*expr_p),
11468 				    TREE_OPERAND (*expr_p, 0));
11469 	    else
11470 	      *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
11471 				    TREE_TYPE (*expr_p),
11472 				    TREE_OPERAND (*expr_p, 0),
11473 				    build_int_cst (TREE_TYPE (*expr_p), 1));
11474 	    if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
11475 	      *expr_p = fold_convert_loc (input_location, type, *expr_p);
11476 	    ret = GS_OK;
11477 	    break;
11478 	  }
11479 
11480 	case ADDR_EXPR:
11481 	  ret = gimplify_addr_expr (expr_p, pre_p, post_p);
11482 	  break;
11483 
11484 	case ANNOTATE_EXPR:
11485 	  {
11486 	    tree cond = TREE_OPERAND (*expr_p, 0);
11487 	    tree kind = TREE_OPERAND (*expr_p, 1);
11488 	    tree data = TREE_OPERAND (*expr_p, 2);
11489 	    tree type = TREE_TYPE (cond);
11490 	    if (!INTEGRAL_TYPE_P (type))
11491 	      {
11492 		*expr_p = cond;
11493 		ret = GS_OK;
11494 		break;
11495 	      }
11496 	    tree tmp = create_tmp_var (type);
11497 	    gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
11498 	    gcall *call
11499 	      = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
11500 	    gimple_call_set_lhs (call, tmp);
11501 	    gimplify_seq_add_stmt (pre_p, call);
11502 	    *expr_p = tmp;
11503 	    ret = GS_ALL_DONE;
11504 	    break;
11505 	  }
11506 
11507 	case VA_ARG_EXPR:
11508 	  ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
11509 	  break;
11510 
11511 	CASE_CONVERT:
11512 	  if (IS_EMPTY_STMT (*expr_p))
11513 	    {
11514 	      ret = GS_ALL_DONE;
11515 	      break;
11516 	    }
11517 
11518 	  if (VOID_TYPE_P (TREE_TYPE (*expr_p))
11519 	      || fallback == fb_none)
11520 	    {
11521 	      /* Just strip a conversion to void (or in void context) and
11522 		 try again.  */
11523 	      *expr_p = TREE_OPERAND (*expr_p, 0);
11524 	      ret = GS_OK;
11525 	      break;
11526 	    }
11527 
11528 	  ret = gimplify_conversion (expr_p);
11529 	  if (ret == GS_ERROR)
11530 	    break;
11531 	  if (*expr_p != save_expr)
11532 	    break;
11533 	  /* FALLTHRU */
11534 
11535 	case FIX_TRUNC_EXPR:
11536 	  /* unary_expr: ... | '(' cast ')' val | ...  */
11537 	  ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11538 			       is_gimple_val, fb_rvalue);
11539 	  recalculate_side_effects (*expr_p);
11540 	  break;
11541 
11542 	case INDIRECT_REF:
11543 	  {
11544 	    bool volatilep = TREE_THIS_VOLATILE (*expr_p);
11545 	    bool notrap = TREE_THIS_NOTRAP (*expr_p);
11546 	    tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
11547 
11548 	    *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
11549 	    if (*expr_p != save_expr)
11550 	      {
11551 		ret = GS_OK;
11552 		break;
11553 	      }
11554 
11555 	    ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11556 				 is_gimple_reg, fb_rvalue);
11557 	    if (ret == GS_ERROR)
11558 	      break;
11559 
11560 	    recalculate_side_effects (*expr_p);
11561 	    *expr_p = fold_build2_loc (input_location, MEM_REF,
11562 				       TREE_TYPE (*expr_p),
11563 				       TREE_OPERAND (*expr_p, 0),
11564 				       build_int_cst (saved_ptr_type, 0));
11565 	    TREE_THIS_VOLATILE (*expr_p) = volatilep;
11566 	    TREE_THIS_NOTRAP (*expr_p) = notrap;
11567 	    ret = GS_OK;
11568 	    break;
11569 	  }
11570 
11571 	/* We arrive here through the various re-gimplifcation paths.  */
11572 	case MEM_REF:
11573 	  /* First try re-folding the whole thing.  */
11574 	  tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
11575 			     TREE_OPERAND (*expr_p, 0),
11576 			     TREE_OPERAND (*expr_p, 1));
11577 	  if (tmp)
11578 	    {
11579 	      REF_REVERSE_STORAGE_ORDER (tmp)
11580 	        = REF_REVERSE_STORAGE_ORDER (*expr_p);
11581 	      *expr_p = tmp;
11582 	      recalculate_side_effects (*expr_p);
11583 	      ret = GS_OK;
11584 	      break;
11585 	    }
11586 	  /* Avoid re-gimplifying the address operand if it is already
11587 	     in suitable form.  Re-gimplifying would mark the address
11588 	     operand addressable.  Always gimplify when not in SSA form
11589 	     as we still may have to gimplify decls with value-exprs.  */
11590 	  if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
11591 	      || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
11592 	    {
11593 	      ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11594 				   is_gimple_mem_ref_addr, fb_rvalue);
11595 	      if (ret == GS_ERROR)
11596 		break;
11597 	    }
11598 	  recalculate_side_effects (*expr_p);
11599 	  ret = GS_ALL_DONE;
11600 	  break;
11601 
11602 	/* Constants need not be gimplified.  */
11603 	case INTEGER_CST:
11604 	case REAL_CST:
11605 	case FIXED_CST:
11606 	case STRING_CST:
11607 	case COMPLEX_CST:
11608 	case VECTOR_CST:
11609 	  /* Drop the overflow flag on constants, we do not want
11610 	     that in the GIMPLE IL.  */
11611 	  if (TREE_OVERFLOW_P (*expr_p))
11612 	    *expr_p = drop_tree_overflow (*expr_p);
11613 	  ret = GS_ALL_DONE;
11614 	  break;
11615 
11616 	case CONST_DECL:
11617 	  /* If we require an lvalue, such as for ADDR_EXPR, retain the
11618 	     CONST_DECL node.  Otherwise the decl is replaceable by its
11619 	     value.  */
11620 	  /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either.  */
11621 	  if (fallback & fb_lvalue)
11622 	    ret = GS_ALL_DONE;
11623 	  else
11624 	    {
11625 	      *expr_p = DECL_INITIAL (*expr_p);
11626 	      ret = GS_OK;
11627 	    }
11628 	  break;
11629 
11630 	case DECL_EXPR:
11631 	  ret = gimplify_decl_expr (expr_p, pre_p);
11632 	  break;
11633 
11634 	case BIND_EXPR:
11635 	  ret = gimplify_bind_expr (expr_p, pre_p);
11636 	  break;
11637 
11638 	case LOOP_EXPR:
11639 	  ret = gimplify_loop_expr (expr_p, pre_p);
11640 	  break;
11641 
11642 	case SWITCH_EXPR:
11643 	  ret = gimplify_switch_expr (expr_p, pre_p);
11644 	  break;
11645 
11646 	case EXIT_EXPR:
11647 	  ret = gimplify_exit_expr (expr_p);
11648 	  break;
11649 
11650 	case GOTO_EXPR:
11651 	  /* If the target is not LABEL, then it is a computed jump
11652 	     and the target needs to be gimplified.  */
11653 	  if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
11654 	    {
11655 	      ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
11656 				   NULL, is_gimple_val, fb_rvalue);
11657 	      if (ret == GS_ERROR)
11658 		break;
11659 	    }
11660 	  gimplify_seq_add_stmt (pre_p,
11661 			  gimple_build_goto (GOTO_DESTINATION (*expr_p)));
11662 	  ret = GS_ALL_DONE;
11663 	  break;
11664 
11665 	case PREDICT_EXPR:
11666 	  gimplify_seq_add_stmt (pre_p,
11667 			gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
11668 					      PREDICT_EXPR_OUTCOME (*expr_p)));
11669 	  ret = GS_ALL_DONE;
11670 	  break;
11671 
11672 	case LABEL_EXPR:
11673 	  ret = gimplify_label_expr (expr_p, pre_p);
11674 	  label = LABEL_EXPR_LABEL (*expr_p);
11675 	  gcc_assert (decl_function_context (label) == current_function_decl);
11676 
11677 	  /* If the label is used in a goto statement, or address of the label
11678 	     is taken, we need to unpoison all variables that were seen so far.
11679 	     Doing so would prevent us from reporting a false positives.  */
11680 	  if (asan_poisoned_variables
11681 	      && asan_used_labels != NULL
11682 	      && asan_used_labels->contains (label))
11683 	    asan_poison_variables (asan_poisoned_variables, false, pre_p);
11684 	  break;
11685 
11686 	case CASE_LABEL_EXPR:
11687 	  ret = gimplify_case_label_expr (expr_p, pre_p);
11688 
11689 	  if (gimplify_ctxp->live_switch_vars)
11690 	    asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
11691 				   pre_p);
11692 	  break;
11693 
11694 	case RETURN_EXPR:
11695 	  ret = gimplify_return_expr (*expr_p, pre_p);
11696 	  break;
11697 
11698 	case CONSTRUCTOR:
11699 	  /* Don't reduce this in place; let gimplify_init_constructor work its
11700 	     magic.  Buf if we're just elaborating this for side effects, just
11701 	     gimplify any element that has side-effects.  */
11702 	  if (fallback == fb_none)
11703 	    {
11704 	      unsigned HOST_WIDE_INT ix;
11705 	      tree val;
11706 	      tree temp = NULL_TREE;
11707 	      FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
11708 		if (TREE_SIDE_EFFECTS (val))
11709 		  append_to_statement_list (val, &temp);
11710 
11711 	      *expr_p = temp;
11712 	      ret = temp ? GS_OK : GS_ALL_DONE;
11713 	    }
11714 	  /* C99 code may assign to an array in a constructed
11715 	     structure or union, and this has undefined behavior only
11716 	     on execution, so create a temporary if an lvalue is
11717 	     required.  */
11718 	  else if (fallback == fb_lvalue)
11719 	    {
11720 	      *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11721 	      mark_addressable (*expr_p);
11722 	      ret = GS_OK;
11723 	    }
11724 	  else
11725 	    ret = GS_ALL_DONE;
11726 	  break;
11727 
11728 	  /* The following are special cases that are not handled by the
11729 	     original GIMPLE grammar.  */
11730 
11731 	  /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11732 	     eliminated.  */
11733 	case SAVE_EXPR:
11734 	  ret = gimplify_save_expr (expr_p, pre_p, post_p);
11735 	  break;
11736 
11737 	case BIT_FIELD_REF:
11738 	  ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11739 			       post_p, is_gimple_lvalue, fb_either);
11740 	  recalculate_side_effects (*expr_p);
11741 	  break;
11742 
11743 	case TARGET_MEM_REF:
11744 	  {
11745 	    enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
11746 
11747 	    if (TMR_BASE (*expr_p))
11748 	      r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
11749 				  post_p, is_gimple_mem_ref_addr, fb_either);
11750 	    if (TMR_INDEX (*expr_p))
11751 	      r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
11752 				  post_p, is_gimple_val, fb_rvalue);
11753 	    if (TMR_INDEX2 (*expr_p))
11754 	      r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
11755 				  post_p, is_gimple_val, fb_rvalue);
11756 	    /* TMR_STEP and TMR_OFFSET are always integer constants.  */
11757 	    ret = MIN (r0, r1);
11758 	  }
11759 	  break;
11760 
11761 	case NON_LVALUE_EXPR:
11762 	  /* This should have been stripped above.  */
11763 	  gcc_unreachable ();
11764 
11765 	case ASM_EXPR:
11766 	  ret = gimplify_asm_expr (expr_p, pre_p, post_p);
11767 	  break;
11768 
11769 	case TRY_FINALLY_EXPR:
11770 	case TRY_CATCH_EXPR:
11771 	  {
11772 	    gimple_seq eval, cleanup;
11773 	    gtry *try_;
11774 
11775 	    /* Calls to destructors are generated automatically in FINALLY/CATCH
11776 	       block. They should have location as UNKNOWN_LOCATION. However,
11777 	       gimplify_call_expr will reset these call stmts to input_location
11778 	       if it finds stmt's location is unknown. To prevent resetting for
11779 	       destructors, we set the input_location to unknown.
11780 	       Note that this only affects the destructor calls in FINALLY/CATCH
11781 	       block, and will automatically reset to its original value by the
11782 	       end of gimplify_expr.  */
11783 	    input_location = UNKNOWN_LOCATION;
11784 	    eval = cleanup = NULL;
11785 	    gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
11786 	    gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
11787 	    /* Don't create bogus GIMPLE_TRY with empty cleanup.  */
11788 	    if (gimple_seq_empty_p (cleanup))
11789 	      {
11790 		gimple_seq_add_seq (pre_p, eval);
11791 		ret = GS_ALL_DONE;
11792 		break;
11793 	      }
11794 	    try_ = gimple_build_try (eval, cleanup,
11795 				     TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
11796 				     ? GIMPLE_TRY_FINALLY
11797 				     : GIMPLE_TRY_CATCH);
11798 	    if (EXPR_HAS_LOCATION (save_expr))
11799 	      gimple_set_location (try_, EXPR_LOCATION (save_expr));
11800 	    else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
11801 	      gimple_set_location (try_, saved_location);
11802 	    if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
11803 	      gimple_try_set_catch_is_cleanup (try_,
11804 					       TRY_CATCH_IS_CLEANUP (*expr_p));
11805 	    gimplify_seq_add_stmt (pre_p, try_);
11806 	    ret = GS_ALL_DONE;
11807 	    break;
11808 	  }
11809 
11810 	case CLEANUP_POINT_EXPR:
11811 	  ret = gimplify_cleanup_point_expr (expr_p, pre_p);
11812 	  break;
11813 
11814 	case TARGET_EXPR:
11815 	  ret = gimplify_target_expr (expr_p, pre_p, post_p);
11816 	  break;
11817 
11818 	case CATCH_EXPR:
11819 	  {
11820 	    gimple *c;
11821 	    gimple_seq handler = NULL;
11822 	    gimplify_and_add (CATCH_BODY (*expr_p), &handler);
11823 	    c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
11824 	    gimplify_seq_add_stmt (pre_p, c);
11825 	    ret = GS_ALL_DONE;
11826 	    break;
11827 	  }
11828 
11829 	case EH_FILTER_EXPR:
11830 	  {
11831 	    gimple *ehf;
11832 	    gimple_seq failure = NULL;
11833 
11834 	    gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
11835 	    ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
11836 	    gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
11837 	    gimplify_seq_add_stmt (pre_p, ehf);
11838 	    ret = GS_ALL_DONE;
11839 	    break;
11840 	  }
11841 
11842 	case OBJ_TYPE_REF:
11843 	  {
11844 	    enum gimplify_status r0, r1;
11845 	    r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
11846 				post_p, is_gimple_val, fb_rvalue);
11847 	    r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
11848 				post_p, is_gimple_val, fb_rvalue);
11849 	    TREE_SIDE_EFFECTS (*expr_p) = 0;
11850 	    ret = MIN (r0, r1);
11851 	  }
11852 	  break;
11853 
11854 	case LABEL_DECL:
11855 	  /* We get here when taking the address of a label.  We mark
11856 	     the label as "forced"; meaning it can never be removed and
11857 	     it is a potential target for any computed goto.  */
11858 	  FORCED_LABEL (*expr_p) = 1;
11859 	  ret = GS_ALL_DONE;
11860 	  break;
11861 
11862 	case STATEMENT_LIST:
11863 	  ret = gimplify_statement_list (expr_p, pre_p);
11864 	  break;
11865 
11866 	case WITH_SIZE_EXPR:
11867 	  {
11868 	    gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11869 			   post_p == &internal_post ? NULL : post_p,
11870 			   gimple_test_f, fallback);
11871 	    gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
11872 			   is_gimple_val, fb_rvalue);
11873 	    ret = GS_ALL_DONE;
11874 	  }
11875 	  break;
11876 
11877 	case VAR_DECL:
11878 	case PARM_DECL:
11879 	  ret = gimplify_var_or_parm_decl (expr_p);
11880 	  break;
11881 
11882 	case RESULT_DECL:
11883 	  /* When within an OMP context, notice uses of variables.  */
11884 	  if (gimplify_omp_ctxp)
11885 	    omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
11886 	  ret = GS_ALL_DONE;
11887 	  break;
11888 
11889 	case DEBUG_EXPR_DECL:
11890 	  gcc_unreachable ();
11891 
11892 	case DEBUG_BEGIN_STMT:
11893 	  gimplify_seq_add_stmt (pre_p,
11894 				 gimple_build_debug_begin_stmt
11895 				 (TREE_BLOCK (*expr_p),
11896 				  EXPR_LOCATION (*expr_p)));
11897 	  ret = GS_ALL_DONE;
11898 	  *expr_p = NULL;
11899 	  break;
11900 
11901 	case SSA_NAME:
11902 	  /* Allow callbacks into the gimplifier during optimization.  */
11903 	  ret = GS_ALL_DONE;
11904 	  break;
11905 
11906 	case OMP_PARALLEL:
11907 	  gimplify_omp_parallel (expr_p, pre_p);
11908 	  ret = GS_ALL_DONE;
11909 	  break;
11910 
11911 	case OMP_TASK:
11912 	  gimplify_omp_task (expr_p, pre_p);
11913 	  ret = GS_ALL_DONE;
11914 	  break;
11915 
11916 	case OMP_FOR:
11917 	case OMP_SIMD:
11918 	case OMP_DISTRIBUTE:
11919 	case OMP_TASKLOOP:
11920 	case OACC_LOOP:
11921 	  ret = gimplify_omp_for (expr_p, pre_p);
11922 	  break;
11923 
11924 	case OACC_CACHE:
11925 	  gimplify_oacc_cache (expr_p, pre_p);
11926 	  ret = GS_ALL_DONE;
11927 	  break;
11928 
11929 	case OACC_DECLARE:
11930 	  gimplify_oacc_declare (expr_p, pre_p);
11931 	  ret = GS_ALL_DONE;
11932 	  break;
11933 
11934 	case OACC_HOST_DATA:
11935 	case OACC_DATA:
11936 	case OACC_KERNELS:
11937 	case OACC_PARALLEL:
11938 	case OMP_SECTIONS:
11939 	case OMP_SINGLE:
11940 	case OMP_TARGET:
11941 	case OMP_TARGET_DATA:
11942 	case OMP_TEAMS:
11943 	  gimplify_omp_workshare (expr_p, pre_p);
11944 	  ret = GS_ALL_DONE;
11945 	  break;
11946 
11947 	case OACC_ENTER_DATA:
11948 	case OACC_EXIT_DATA:
11949 	case OACC_UPDATE:
11950 	case OMP_TARGET_UPDATE:
11951 	case OMP_TARGET_ENTER_DATA:
11952 	case OMP_TARGET_EXIT_DATA:
11953 	  gimplify_omp_target_update (expr_p, pre_p);
11954 	  ret = GS_ALL_DONE;
11955 	  break;
11956 
11957 	case OMP_SECTION:
11958 	case OMP_MASTER:
11959 	case OMP_TASKGROUP:
11960 	case OMP_ORDERED:
11961 	case OMP_CRITICAL:
11962 	  {
11963 	    gimple_seq body = NULL;
11964 	    gimple *g;
11965 
11966 	    gimplify_and_add (OMP_BODY (*expr_p), &body);
11967 	    switch (TREE_CODE (*expr_p))
11968 	      {
11969 	      case OMP_SECTION:
11970 	        g = gimple_build_omp_section (body);
11971 	        break;
11972 	      case OMP_MASTER:
11973 	        g = gimple_build_omp_master (body);
11974 		break;
11975 	      case OMP_TASKGROUP:
11976 		{
11977 		  gimple_seq cleanup = NULL;
11978 		  tree fn
11979 		    = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
11980 		  g = gimple_build_call (fn, 0);
11981 		  gimple_seq_add_stmt (&cleanup, g);
11982 		  g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
11983 		  body = NULL;
11984 		  gimple_seq_add_stmt (&body, g);
11985 		  g = gimple_build_omp_taskgroup (body);
11986 		}
11987 		break;
11988 	      case OMP_ORDERED:
11989 		g = gimplify_omp_ordered (*expr_p, body);
11990 		break;
11991 	      case OMP_CRITICAL:
11992 		gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
11993 					   pre_p, ORT_WORKSHARE, OMP_CRITICAL);
11994 		gimplify_adjust_omp_clauses (pre_p, body,
11995 					     &OMP_CRITICAL_CLAUSES (*expr_p),
11996 					     OMP_CRITICAL);
11997 		g = gimple_build_omp_critical (body,
11998 		    			       OMP_CRITICAL_NAME (*expr_p),
11999 		    			       OMP_CRITICAL_CLAUSES (*expr_p));
12000 		break;
12001 	      default:
12002 		gcc_unreachable ();
12003 	      }
12004 	    gimplify_seq_add_stmt (pre_p, g);
12005 	    ret = GS_ALL_DONE;
12006 	    break;
12007 	  }
12008 
12009 	case OMP_ATOMIC:
12010 	case OMP_ATOMIC_READ:
12011 	case OMP_ATOMIC_CAPTURE_OLD:
12012 	case OMP_ATOMIC_CAPTURE_NEW:
12013 	  ret = gimplify_omp_atomic (expr_p, pre_p);
12014 	  break;
12015 
12016 	case TRANSACTION_EXPR:
12017 	  ret = gimplify_transaction (expr_p, pre_p);
12018 	  break;
12019 
12020 	case TRUTH_AND_EXPR:
12021 	case TRUTH_OR_EXPR:
12022 	case TRUTH_XOR_EXPR:
12023 	  {
12024 	    tree orig_type = TREE_TYPE (*expr_p);
12025 	    tree new_type, xop0, xop1;
12026 	    *expr_p = gimple_boolify (*expr_p);
12027 	    new_type = TREE_TYPE (*expr_p);
12028 	    if (!useless_type_conversion_p (orig_type, new_type))
12029 	      {
12030 		*expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
12031 		ret = GS_OK;
12032 		break;
12033 	      }
12034 
12035 	  /* Boolified binary truth expressions are semantically equivalent
12036 	     to bitwise binary expressions.  Canonicalize them to the
12037 	     bitwise variant.  */
12038 	    switch (TREE_CODE (*expr_p))
12039 	      {
12040 	      case TRUTH_AND_EXPR:
12041 		TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
12042 		break;
12043 	      case TRUTH_OR_EXPR:
12044 		TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
12045 		break;
12046 	      case TRUTH_XOR_EXPR:
12047 		TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
12048 		break;
12049 	      default:
12050 		break;
12051 	      }
12052 	    /* Now make sure that operands have compatible type to
12053 	       expression's new_type.  */
12054 	    xop0 = TREE_OPERAND (*expr_p, 0);
12055 	    xop1 = TREE_OPERAND (*expr_p, 1);
12056 	    if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
12057 	      TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
12058 							    new_type,
12059 	      						    xop0);
12060 	    if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
12061 	      TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
12062 							    new_type,
12063 	      						    xop1);
12064 	    /* Continue classified as tcc_binary.  */
12065 	    goto expr_2;
12066 	  }
12067 
12068 	case VEC_COND_EXPR:
12069 	  {
12070 	    enum gimplify_status r0, r1, r2;
12071 
12072 	    r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12073 				post_p, is_gimple_condexpr, fb_rvalue);
12074 	    r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12075 				post_p, is_gimple_val, fb_rvalue);
12076 	    r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12077 				post_p, is_gimple_val, fb_rvalue);
12078 
12079 	    ret = MIN (MIN (r0, r1), r2);
12080 	    recalculate_side_effects (*expr_p);
12081 	  }
12082 	  break;
12083 
12084 	case FMA_EXPR:
12085 	case VEC_PERM_EXPR:
12086 	  /* Classified as tcc_expression.  */
12087 	  goto expr_3;
12088 
12089 	case BIT_INSERT_EXPR:
12090 	  /* Argument 3 is a constant.  */
12091 	  goto expr_2;
12092 
12093 	case POINTER_PLUS_EXPR:
12094 	  {
12095 	    enum gimplify_status r0, r1;
12096 	    r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12097 				post_p, is_gimple_val, fb_rvalue);
12098 	    r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12099 				post_p, is_gimple_val, fb_rvalue);
12100 	    recalculate_side_effects (*expr_p);
12101 	    ret = MIN (r0, r1);
12102 	    break;
12103 	  }
12104 
12105 	default:
12106 	  switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
12107 	    {
12108 	    case tcc_comparison:
12109 	      /* Handle comparison of objects of non scalar mode aggregates
12110 	     	 with a call to memcmp.  It would be nice to only have to do
12111 	     	 this for variable-sized objects, but then we'd have to allow
12112 	     	 the same nest of reference nodes we allow for MODIFY_EXPR and
12113 	     	 that's too complex.
12114 
12115 		 Compare scalar mode aggregates as scalar mode values.  Using
12116 		 memcmp for them would be very inefficient at best, and is
12117 		 plain wrong if bitfields are involved.  */
12118 		{
12119 		  tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
12120 
12121 		  /* Vector comparisons need no boolification.  */
12122 		  if (TREE_CODE (type) == VECTOR_TYPE)
12123 		    goto expr_2;
12124 		  else if (!AGGREGATE_TYPE_P (type))
12125 		    {
12126 		      tree org_type = TREE_TYPE (*expr_p);
12127 		      *expr_p = gimple_boolify (*expr_p);
12128 		      if (!useless_type_conversion_p (org_type,
12129 						      TREE_TYPE (*expr_p)))
12130 			{
12131 			  *expr_p = fold_convert_loc (input_location,
12132 						      org_type, *expr_p);
12133 			  ret = GS_OK;
12134 			}
12135 		      else
12136 			goto expr_2;
12137 		    }
12138 		  else if (TYPE_MODE (type) != BLKmode)
12139 		    ret = gimplify_scalar_mode_aggregate_compare (expr_p);
12140 		  else
12141 		    ret = gimplify_variable_sized_compare (expr_p);
12142 
12143 		  break;
12144 		}
12145 
12146 	    /* If *EXPR_P does not need to be special-cased, handle it
12147 	       according to its class.  */
12148 	    case tcc_unary:
12149 	      ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12150 				   post_p, is_gimple_val, fb_rvalue);
12151 	      break;
12152 
12153 	    case tcc_binary:
12154 	    expr_2:
12155 	      {
12156 		enum gimplify_status r0, r1;
12157 
12158 		r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12159 		                    post_p, is_gimple_val, fb_rvalue);
12160 		r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12161 				    post_p, is_gimple_val, fb_rvalue);
12162 
12163 		ret = MIN (r0, r1);
12164 		break;
12165 	      }
12166 
12167 	    expr_3:
12168 	      {
12169 		enum gimplify_status r0, r1, r2;
12170 
12171 		r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12172 		                    post_p, is_gimple_val, fb_rvalue);
12173 		r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12174 				    post_p, is_gimple_val, fb_rvalue);
12175 		r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12176 				    post_p, is_gimple_val, fb_rvalue);
12177 
12178 		ret = MIN (MIN (r0, r1), r2);
12179 		break;
12180 	      }
12181 
12182 	    case tcc_declaration:
12183 	    case tcc_constant:
12184 	      ret = GS_ALL_DONE;
12185 	      goto dont_recalculate;
12186 
12187 	    default:
12188 	      gcc_unreachable ();
12189 	    }
12190 
12191 	  recalculate_side_effects (*expr_p);
12192 
12193 	dont_recalculate:
12194 	  break;
12195 	}
12196 
12197       gcc_assert (*expr_p || ret != GS_OK);
12198     }
12199   while (ret == GS_OK);
12200 
12201   /* If we encountered an error_mark somewhere nested inside, either
12202      stub out the statement or propagate the error back out.  */
12203   if (ret == GS_ERROR)
12204     {
12205       if (is_statement)
12206 	*expr_p = NULL;
12207       goto out;
12208     }
12209 
12210   /* This was only valid as a return value from the langhook, which
12211      we handled.  Make sure it doesn't escape from any other context.  */
12212   gcc_assert (ret != GS_UNHANDLED);
12213 
12214   if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
12215     {
12216       /* We aren't looking for a value, and we don't have a valid
12217 	 statement.  If it doesn't have side-effects, throw it away.
12218 	 We can also get here with code such as "*&&L;", where L is
12219 	 a LABEL_DECL that is marked as FORCED_LABEL.  */
12220       if (TREE_CODE (*expr_p) == LABEL_DECL
12221 	  || !TREE_SIDE_EFFECTS (*expr_p))
12222 	*expr_p = NULL;
12223       else if (!TREE_THIS_VOLATILE (*expr_p))
12224 	{
12225 	  /* This is probably a _REF that contains something nested that
12226 	     has side effects.  Recurse through the operands to find it.  */
12227 	  enum tree_code code = TREE_CODE (*expr_p);
12228 
12229 	  switch (code)
12230 	    {
12231 	    case COMPONENT_REF:
12232 	    case REALPART_EXPR:
12233 	    case IMAGPART_EXPR:
12234 	    case VIEW_CONVERT_EXPR:
12235 	      gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12236 			     gimple_test_f, fallback);
12237 	      break;
12238 
12239 	    case ARRAY_REF:
12240 	    case ARRAY_RANGE_REF:
12241 	      gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12242 			     gimple_test_f, fallback);
12243 	      gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12244 			     gimple_test_f, fallback);
12245 	      break;
12246 
12247 	    default:
12248 	       /* Anything else with side-effects must be converted to
12249 		  a valid statement before we get here.  */
12250 	      gcc_unreachable ();
12251 	    }
12252 
12253 	  *expr_p = NULL;
12254 	}
12255       else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
12256 	       && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
12257 	{
12258 	  /* Historically, the compiler has treated a bare reference
12259 	     to a non-BLKmode volatile lvalue as forcing a load.  */
12260 	  tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
12261 
12262 	  /* Normally, we do not want to create a temporary for a
12263 	     TREE_ADDRESSABLE type because such a type should not be
12264 	     copied by bitwise-assignment.  However, we make an
12265 	     exception here, as all we are doing here is ensuring that
12266 	     we read the bytes that make up the type.  We use
12267 	     create_tmp_var_raw because create_tmp_var will abort when
12268 	     given a TREE_ADDRESSABLE type.  */
12269 	  tree tmp = create_tmp_var_raw (type, "vol");
12270 	  gimple_add_tmp_var (tmp);
12271 	  gimplify_assign (tmp, *expr_p, pre_p);
12272 	  *expr_p = NULL;
12273 	}
12274       else
12275 	/* We can't do anything useful with a volatile reference to
12276 	   an incomplete type, so just throw it away.  Likewise for
12277 	   a BLKmode type, since any implicit inner load should
12278 	   already have been turned into an explicit one by the
12279 	   gimplification process.  */
12280 	*expr_p = NULL;
12281     }
12282 
12283   /* If we are gimplifying at the statement level, we're done.  Tack
12284      everything together and return.  */
12285   if (fallback == fb_none || is_statement)
12286     {
12287       /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
12288          it out for GC to reclaim it.  */
12289       *expr_p = NULL_TREE;
12290 
12291       if (!gimple_seq_empty_p (internal_pre)
12292 	  || !gimple_seq_empty_p (internal_post))
12293 	{
12294 	  gimplify_seq_add_seq (&internal_pre, internal_post);
12295 	  gimplify_seq_add_seq (pre_p, internal_pre);
12296 	}
12297 
12298       /* The result of gimplifying *EXPR_P is going to be the last few
12299 	 statements in *PRE_P and *POST_P.  Add location information
12300 	 to all the statements that were added by the gimplification
12301 	 helpers.  */
12302       if (!gimple_seq_empty_p (*pre_p))
12303 	annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
12304 
12305       if (!gimple_seq_empty_p (*post_p))
12306 	annotate_all_with_location_after (*post_p, post_last_gsi,
12307 					  input_location);
12308 
12309       goto out;
12310     }
12311 
12312 #ifdef ENABLE_GIMPLE_CHECKING
12313   if (*expr_p)
12314     {
12315       enum tree_code code = TREE_CODE (*expr_p);
12316       /* These expressions should already be in gimple IR form.  */
12317       gcc_assert (code != MODIFY_EXPR
12318 		  && code != ASM_EXPR
12319 		  && code != BIND_EXPR
12320 		  && code != CATCH_EXPR
12321 		  && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
12322 		  && code != EH_FILTER_EXPR
12323 		  && code != GOTO_EXPR
12324 		  && code != LABEL_EXPR
12325 		  && code != LOOP_EXPR
12326 		  && code != SWITCH_EXPR
12327 		  && code != TRY_FINALLY_EXPR
12328 		  && code != OACC_PARALLEL
12329 		  && code != OACC_KERNELS
12330 		  && code != OACC_DATA
12331 		  && code != OACC_HOST_DATA
12332 		  && code != OACC_DECLARE
12333 		  && code != OACC_UPDATE
12334 		  && code != OACC_ENTER_DATA
12335 		  && code != OACC_EXIT_DATA
12336 		  && code != OACC_CACHE
12337 		  && code != OMP_CRITICAL
12338 		  && code != OMP_FOR
12339 		  && code != OACC_LOOP
12340 		  && code != OMP_MASTER
12341 		  && code != OMP_TASKGROUP
12342 		  && code != OMP_ORDERED
12343 		  && code != OMP_PARALLEL
12344 		  && code != OMP_SECTIONS
12345 		  && code != OMP_SECTION
12346 		  && code != OMP_SINGLE);
12347     }
12348 #endif
12349 
12350   /* Otherwise we're gimplifying a subexpression, so the resulting
12351      value is interesting.  If it's a valid operand that matches
12352      GIMPLE_TEST_F, we're done. Unless we are handling some
12353      post-effects internally; if that's the case, we need to copy into
12354      a temporary before adding the post-effects to POST_P.  */
12355   if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
12356     goto out;
12357 
12358   /* Otherwise, we need to create a new temporary for the gimplified
12359      expression.  */
12360 
12361   /* We can't return an lvalue if we have an internal postqueue.  The
12362      object the lvalue refers to would (probably) be modified by the
12363      postqueue; we need to copy the value out first, which means an
12364      rvalue.  */
12365   if ((fallback & fb_lvalue)
12366       && gimple_seq_empty_p (internal_post)
12367       && is_gimple_addressable (*expr_p))
12368     {
12369       /* An lvalue will do.  Take the address of the expression, store it
12370 	 in a temporary, and replace the expression with an INDIRECT_REF of
12371 	 that temporary.  */
12372       tmp = build_fold_addr_expr_loc (input_location, *expr_p);
12373       gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
12374       *expr_p = build_simple_mem_ref (tmp);
12375     }
12376   else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
12377     {
12378       /* An rvalue will do.  Assign the gimplified expression into a
12379 	 new temporary TMP and replace the original expression with
12380 	 TMP.  First, make sure that the expression has a type so that
12381 	 it can be assigned into a temporary.  */
12382       gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
12383       *expr_p = get_formal_tmp_var (*expr_p, pre_p);
12384     }
12385   else
12386     {
12387 #ifdef ENABLE_GIMPLE_CHECKING
12388       if (!(fallback & fb_mayfail))
12389 	{
12390 	  fprintf (stderr, "gimplification failed:\n");
12391 	  print_generic_expr (stderr, *expr_p);
12392 	  debug_tree (*expr_p);
12393 	  internal_error ("gimplification failed");
12394 	}
12395 #endif
12396       gcc_assert (fallback & fb_mayfail);
12397 
12398       /* If this is an asm statement, and the user asked for the
12399 	 impossible, don't die.  Fail and let gimplify_asm_expr
12400 	 issue an error.  */
12401       ret = GS_ERROR;
12402       goto out;
12403     }
12404 
12405   /* Make sure the temporary matches our predicate.  */
12406   gcc_assert ((*gimple_test_f) (*expr_p));
12407 
12408   if (!gimple_seq_empty_p (internal_post))
12409     {
12410       annotate_all_with_location (internal_post, input_location);
12411       gimplify_seq_add_seq (pre_p, internal_post);
12412     }
12413 
12414  out:
12415   input_location = saved_location;
12416   return ret;
12417 }
12418 
12419 /* Like gimplify_expr but make sure the gimplified result is not itself
12420    a SSA name (but a decl if it were).  Temporaries required by
12421    evaluating *EXPR_P may be still SSA names.  */
12422 
12423 static enum gimplify_status
12424 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12425 	       bool (*gimple_test_f) (tree), fallback_t fallback,
12426 	       bool allow_ssa)
12427 {
12428   bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
12429   enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
12430 					    gimple_test_f, fallback);
12431   if (! allow_ssa
12432       && TREE_CODE (*expr_p) == SSA_NAME)
12433     {
12434       tree name = *expr_p;
12435       if (was_ssa_name_p)
12436 	*expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
12437       else
12438 	{
12439 	  /* Avoid the extra copy if possible.  */
12440 	  *expr_p = create_tmp_reg (TREE_TYPE (name));
12441 	  gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
12442 	  release_ssa_name (name);
12443 	}
12444     }
12445   return ret;
12446 }
12447 
12448 /* Look through TYPE for variable-sized objects and gimplify each such
12449    size that we find.  Add to LIST_P any statements generated.  */
12450 
12451 void
12452 gimplify_type_sizes (tree type, gimple_seq *list_p)
12453 {
12454   tree field, t;
12455 
12456   if (type == NULL || type == error_mark_node)
12457     return;
12458 
12459   /* We first do the main variant, then copy into any other variants.  */
12460   type = TYPE_MAIN_VARIANT (type);
12461 
12462   /* Avoid infinite recursion.  */
12463   if (TYPE_SIZES_GIMPLIFIED (type))
12464     return;
12465 
12466   TYPE_SIZES_GIMPLIFIED (type) = 1;
12467 
12468   switch (TREE_CODE (type))
12469     {
12470     case INTEGER_TYPE:
12471     case ENUMERAL_TYPE:
12472     case BOOLEAN_TYPE:
12473     case REAL_TYPE:
12474     case FIXED_POINT_TYPE:
12475       gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
12476       gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
12477 
12478       for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12479 	{
12480 	  TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
12481 	  TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
12482 	}
12483       break;
12484 
12485     case ARRAY_TYPE:
12486       /* These types may not have declarations, so handle them here.  */
12487       gimplify_type_sizes (TREE_TYPE (type), list_p);
12488       gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
12489       /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12490 	 with assigned stack slots, for -O1+ -g they should be tracked
12491 	 by VTA.  */
12492       if (!(TYPE_NAME (type)
12493 	    && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12494 	    && DECL_IGNORED_P (TYPE_NAME (type)))
12495 	  && TYPE_DOMAIN (type)
12496 	  && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
12497 	{
12498 	  t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
12499 	  if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12500 	    DECL_IGNORED_P (t) = 0;
12501 	  t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12502 	  if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12503 	    DECL_IGNORED_P (t) = 0;
12504 	}
12505       break;
12506 
12507     case RECORD_TYPE:
12508     case UNION_TYPE:
12509     case QUAL_UNION_TYPE:
12510       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12511 	if (TREE_CODE (field) == FIELD_DECL)
12512 	  {
12513 	    gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
12514 	    gimplify_one_sizepos (&DECL_SIZE (field), list_p);
12515 	    gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
12516 	    gimplify_type_sizes (TREE_TYPE (field), list_p);
12517 	  }
12518       break;
12519 
12520     case POINTER_TYPE:
12521     case REFERENCE_TYPE:
12522 	/* We used to recurse on the pointed-to type here, which turned out to
12523 	   be incorrect because its definition might refer to variables not
12524 	   yet initialized at this point if a forward declaration is involved.
12525 
12526 	   It was actually useful for anonymous pointed-to types to ensure
12527 	   that the sizes evaluation dominates every possible later use of the
12528 	   values.  Restricting to such types here would be safe since there
12529 	   is no possible forward declaration around, but would introduce an
12530 	   undesirable middle-end semantic to anonymity.  We then defer to
12531 	   front-ends the responsibility of ensuring that the sizes are
12532 	   evaluated both early and late enough, e.g. by attaching artificial
12533 	   type declarations to the tree.  */
12534       break;
12535 
12536     default:
12537       break;
12538     }
12539 
12540   gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
12541   gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
12542 
12543   for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12544     {
12545       TYPE_SIZE (t) = TYPE_SIZE (type);
12546       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
12547       TYPE_SIZES_GIMPLIFIED (t) = 1;
12548     }
12549 }
12550 
12551 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12552    a size or position, has had all of its SAVE_EXPRs evaluated.
12553    We add any required statements to *STMT_P.  */
12554 
12555 void
12556 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
12557 {
12558   tree expr = *expr_p;
12559 
12560   /* We don't do anything if the value isn't there, is constant, or contains
12561      A PLACEHOLDER_EXPR.  We also don't want to do anything if it's already
12562      a VAR_DECL.  If it's a VAR_DECL from another function, the gimplifier
12563      will want to replace it with a new variable, but that will cause problems
12564      if this type is from outside the function.  It's OK to have that here.  */
12565   if (expr == NULL_TREE
12566       || is_gimple_constant (expr)
12567       || TREE_CODE (expr) == VAR_DECL
12568       || CONTAINS_PLACEHOLDER_P (expr))
12569     return;
12570 
12571   *expr_p = unshare_expr (expr);
12572 
12573   /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12574      if the def vanishes.  */
12575   gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
12576 
12577   /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
12578      FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
12579      as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs.  */
12580   if (is_gimple_constant (*expr_p))
12581     *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
12582 }
12583 
12584 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12585    containing the sequence of corresponding GIMPLE statements.  If DO_PARMS
12586    is true, also gimplify the parameters.  */
12587 
12588 gbind *
12589 gimplify_body (tree fndecl, bool do_parms)
12590 {
12591   location_t saved_location = input_location;
12592   gimple_seq parm_stmts, parm_cleanup = NULL, seq;
12593   gimple *outer_stmt;
12594   gbind *outer_bind;
12595   struct cgraph_node *cgn;
12596 
12597   timevar_push (TV_TREE_GIMPLIFY);
12598 
12599   init_tree_ssa (cfun);
12600 
12601   /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12602      gimplification.  */
12603   default_rtl_profile ();
12604 
12605   gcc_assert (gimplify_ctxp == NULL);
12606   push_gimplify_context (true);
12607 
12608   if (flag_openacc || flag_openmp)
12609     {
12610       gcc_assert (gimplify_omp_ctxp == NULL);
12611       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
12612 	gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
12613     }
12614 
12615   /* Unshare most shared trees in the body and in that of any nested functions.
12616      It would seem we don't have to do this for nested functions because
12617      they are supposed to be output and then the outer function gimplified
12618      first, but the g++ front end doesn't always do it that way.  */
12619   unshare_body (fndecl);
12620   unvisit_body (fndecl);
12621 
12622   cgn = cgraph_node::get (fndecl);
12623   if (cgn && cgn->origin)
12624     nonlocal_vlas = new hash_set<tree>;
12625 
12626   /* Make sure input_location isn't set to something weird.  */
12627   input_location = DECL_SOURCE_LOCATION (fndecl);
12628 
12629   /* Resolve callee-copies.  This has to be done before processing
12630      the body so that DECL_VALUE_EXPR gets processed correctly.  */
12631   parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
12632 
12633   /* Gimplify the function's body.  */
12634   seq = NULL;
12635   gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
12636   outer_stmt = gimple_seq_first_stmt (seq);
12637   if (!outer_stmt)
12638     {
12639       outer_stmt = gimple_build_nop ();
12640       gimplify_seq_add_stmt (&seq, outer_stmt);
12641     }
12642 
12643   /* The body must contain exactly one statement, a GIMPLE_BIND.  If this is
12644      not the case, wrap everything in a GIMPLE_BIND to make it so.  */
12645   if (gimple_code (outer_stmt) == GIMPLE_BIND
12646       && gimple_seq_first (seq) == gimple_seq_last (seq))
12647     outer_bind = as_a <gbind *> (outer_stmt);
12648   else
12649     outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
12650 
12651   DECL_SAVED_TREE (fndecl) = NULL_TREE;
12652 
12653   /* If we had callee-copies statements, insert them at the beginning
12654      of the function and clear DECL_VALUE_EXPR_P on the parameters.  */
12655   if (!gimple_seq_empty_p (parm_stmts))
12656     {
12657       tree parm;
12658 
12659       gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
12660       if (parm_cleanup)
12661 	{
12662 	  gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
12663 				      GIMPLE_TRY_FINALLY);
12664 	  parm_stmts = NULL;
12665 	  gimple_seq_add_stmt (&parm_stmts, g);
12666 	}
12667       gimple_bind_set_body (outer_bind, parm_stmts);
12668 
12669       for (parm = DECL_ARGUMENTS (current_function_decl);
12670 	   parm; parm = DECL_CHAIN (parm))
12671 	if (DECL_HAS_VALUE_EXPR_P (parm))
12672 	  {
12673 	    DECL_HAS_VALUE_EXPR_P (parm) = 0;
12674 	    DECL_IGNORED_P (parm) = 0;
12675 	  }
12676     }
12677 
12678   if (nonlocal_vlas)
12679     {
12680       if (nonlocal_vla_vars)
12681 	{
12682 	  /* tree-nested.c may later on call declare_vars (..., true);
12683 	     which relies on BLOCK_VARS chain to be the tail of the
12684 	     gimple_bind_vars chain.  Ensure we don't violate that
12685 	     assumption.  */
12686 	  if (gimple_bind_block (outer_bind)
12687 	      == DECL_INITIAL (current_function_decl))
12688 	    declare_vars (nonlocal_vla_vars, outer_bind, true);
12689 	  else
12690 	    BLOCK_VARS (DECL_INITIAL (current_function_decl))
12691 	      = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
12692 			 nonlocal_vla_vars);
12693 	  nonlocal_vla_vars = NULL_TREE;
12694 	}
12695       delete nonlocal_vlas;
12696       nonlocal_vlas = NULL;
12697     }
12698 
12699   if ((flag_openacc || flag_openmp || flag_openmp_simd)
12700       && gimplify_omp_ctxp)
12701     {
12702       delete_omp_context (gimplify_omp_ctxp);
12703       gimplify_omp_ctxp = NULL;
12704     }
12705 
12706   pop_gimplify_context (outer_bind);
12707   gcc_assert (gimplify_ctxp == NULL);
12708 
12709   if (flag_checking && !seen_error ())
12710     verify_gimple_in_seq (gimple_bind_body (outer_bind));
12711 
12712   timevar_pop (TV_TREE_GIMPLIFY);
12713   input_location = saved_location;
12714 
12715   return outer_bind;
12716 }
12717 
12718 typedef char *char_p; /* For DEF_VEC_P.  */
12719 
12720 /* Return whether we should exclude FNDECL from instrumentation.  */
12721 
12722 static bool
12723 flag_instrument_functions_exclude_p (tree fndecl)
12724 {
12725   vec<char_p> *v;
12726 
12727   v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
12728   if (v && v->length () > 0)
12729     {
12730       const char *name;
12731       int i;
12732       char *s;
12733 
12734       name = lang_hooks.decl_printable_name (fndecl, 0);
12735       FOR_EACH_VEC_ELT (*v, i, s)
12736 	if (strstr (name, s) != NULL)
12737 	  return true;
12738     }
12739 
12740   v = (vec<char_p> *) flag_instrument_functions_exclude_files;
12741   if (v && v->length () > 0)
12742     {
12743       const char *name;
12744       int i;
12745       char *s;
12746 
12747       name = DECL_SOURCE_FILE (fndecl);
12748       FOR_EACH_VEC_ELT (*v, i, s)
12749 	if (strstr (name, s) != NULL)
12750 	  return true;
12751     }
12752 
12753   return false;
12754 }
12755 
12756 /* Entry point to the gimplification pass.  FNDECL is the FUNCTION_DECL
12757    node for the function we want to gimplify.
12758 
12759    Return the sequence of GIMPLE statements corresponding to the body
12760    of FNDECL.  */
12761 
12762 void
12763 gimplify_function_tree (tree fndecl)
12764 {
12765   tree parm, ret;
12766   gimple_seq seq;
12767   gbind *bind;
12768 
12769   gcc_assert (!gimple_body (fndecl));
12770 
12771   if (DECL_STRUCT_FUNCTION (fndecl))
12772     push_cfun (DECL_STRUCT_FUNCTION (fndecl));
12773   else
12774     push_struct_function (fndecl);
12775 
12776   /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12777      if necessary.  */
12778   cfun->curr_properties |= PROP_gimple_lva;
12779 
12780   for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
12781     {
12782       /* Preliminarily mark non-addressed complex variables as eligible
12783          for promotion to gimple registers.  We'll transform their uses
12784          as we find them.  */
12785       if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
12786 	   || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
12787           && !TREE_THIS_VOLATILE (parm)
12788           && !needs_to_live_in_memory (parm))
12789         DECL_GIMPLE_REG_P (parm) = 1;
12790     }
12791 
12792   ret = DECL_RESULT (fndecl);
12793   if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
12794        || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
12795       && !needs_to_live_in_memory (ret))
12796     DECL_GIMPLE_REG_P (ret) = 1;
12797 
12798   if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
12799     asan_poisoned_variables = new hash_set<tree> ();
12800   bind = gimplify_body (fndecl, true);
12801   if (asan_poisoned_variables)
12802     {
12803       delete asan_poisoned_variables;
12804       asan_poisoned_variables = NULL;
12805     }
12806 
12807   /* The tree body of the function is no longer needed, replace it
12808      with the new GIMPLE body.  */
12809   seq = NULL;
12810   gimple_seq_add_stmt (&seq, bind);
12811   gimple_set_body (fndecl, seq);
12812 
12813   /* If we're instrumenting function entry/exit, then prepend the call to
12814      the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12815      catch the exit hook.  */
12816   /* ??? Add some way to ignore exceptions for this TFE.  */
12817   if (flag_instrument_function_entry_exit
12818       && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
12819       /* Do not instrument extern inline functions.  */
12820       && !(DECL_DECLARED_INLINE_P (fndecl)
12821 	   && DECL_EXTERNAL (fndecl)
12822 	   && DECL_DISREGARD_INLINE_LIMITS (fndecl))
12823       && !flag_instrument_functions_exclude_p (fndecl))
12824     {
12825       tree x;
12826       gbind *new_bind;
12827       gimple *tf;
12828       gimple_seq cleanup = NULL, body = NULL;
12829       tree tmp_var;
12830       gcall *call;
12831 
12832       x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12833       call = gimple_build_call (x, 1, integer_zero_node);
12834       tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12835       gimple_call_set_lhs (call, tmp_var);
12836       gimplify_seq_add_stmt (&cleanup, call);
12837       x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
12838       call = gimple_build_call (x, 2,
12839 				build_fold_addr_expr (current_function_decl),
12840 				tmp_var);
12841       gimplify_seq_add_stmt (&cleanup, call);
12842       tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
12843 
12844       x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12845       call = gimple_build_call (x, 1, integer_zero_node);
12846       tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12847       gimple_call_set_lhs (call, tmp_var);
12848       gimplify_seq_add_stmt (&body, call);
12849       x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
12850       call = gimple_build_call (x, 2,
12851 				build_fold_addr_expr (current_function_decl),
12852 				tmp_var);
12853       gimplify_seq_add_stmt (&body, call);
12854       gimplify_seq_add_stmt (&body, tf);
12855       new_bind = gimple_build_bind (NULL, body, NULL);
12856 
12857       /* Replace the current function body with the body
12858          wrapped in the try/finally TF.  */
12859       seq = NULL;
12860       gimple_seq_add_stmt (&seq, new_bind);
12861       gimple_set_body (fndecl, seq);
12862       bind = new_bind;
12863     }
12864 
12865   if (sanitize_flags_p (SANITIZE_THREAD))
12866     {
12867       gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
12868       gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
12869       gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
12870       /* Replace the current function body with the body
12871 	 wrapped in the try/finally TF.  */
12872       seq = NULL;
12873       gimple_seq_add_stmt (&seq, new_bind);
12874       gimple_set_body (fndecl, seq);
12875     }
12876 
12877   DECL_SAVED_TREE (fndecl) = NULL_TREE;
12878   cfun->curr_properties |= PROP_gimple_any;
12879 
12880   pop_cfun ();
12881 
12882   dump_function (TDI_gimple, fndecl);
12883 }
12884 
12885 /* Return a dummy expression of type TYPE in order to keep going after an
12886    error.  */
12887 
12888 static tree
12889 dummy_object (tree type)
12890 {
12891   tree t = build_int_cst (build_pointer_type (type), 0);
12892   return build2 (MEM_REF, type, t, t);
12893 }
12894 
12895 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
12896    builtin function, but a very special sort of operator.  */
12897 
12898 enum gimplify_status
12899 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
12900 		      gimple_seq *post_p ATTRIBUTE_UNUSED)
12901 {
12902   tree promoted_type, have_va_type;
12903   tree valist = TREE_OPERAND (*expr_p, 0);
12904   tree type = TREE_TYPE (*expr_p);
12905   tree t, tag, aptag;
12906   location_t loc = EXPR_LOCATION (*expr_p);
12907 
12908   /* Verify that valist is of the proper type.  */
12909   have_va_type = TREE_TYPE (valist);
12910   if (have_va_type == error_mark_node)
12911     return GS_ERROR;
12912   have_va_type = targetm.canonical_va_list_type (have_va_type);
12913   if (have_va_type == NULL_TREE
12914       && POINTER_TYPE_P (TREE_TYPE (valist)))
12915     /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg.  */
12916     have_va_type
12917       = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
12918   gcc_assert (have_va_type != NULL_TREE);
12919 
12920   /* Generate a diagnostic for requesting data of a type that cannot
12921      be passed through `...' due to type promotion at the call site.  */
12922   if ((promoted_type = lang_hooks.types.type_promotes_to (type))
12923 	   != type)
12924     {
12925       static bool gave_help;
12926       bool warned;
12927       /* Use the expansion point to handle cases such as passing bool (defined
12928 	 in a system header) through `...'.  */
12929       source_location xloc
12930 	= expansion_point_location_if_in_system_header (loc);
12931 
12932       /* Unfortunately, this is merely undefined, rather than a constraint
12933 	 violation, so we cannot make this an error.  If this call is never
12934 	 executed, the program is still strictly conforming.  */
12935       warned = warning_at (xloc, 0,
12936 			   "%qT is promoted to %qT when passed through %<...%>",
12937 			   type, promoted_type);
12938       if (!gave_help && warned)
12939 	{
12940 	  gave_help = true;
12941 	  inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
12942 		  promoted_type, type);
12943 	}
12944 
12945       /* We can, however, treat "undefined" any way we please.
12946 	 Call abort to encourage the user to fix the program.  */
12947       if (warned)
12948 	inform (xloc, "if this code is reached, the program will abort");
12949       /* Before the abort, allow the evaluation of the va_list
12950 	 expression to exit or longjmp.  */
12951       gimplify_and_add (valist, pre_p);
12952       t = build_call_expr_loc (loc,
12953 			       builtin_decl_implicit (BUILT_IN_TRAP), 0);
12954       gimplify_and_add (t, pre_p);
12955 
12956       /* This is dead code, but go ahead and finish so that the
12957 	 mode of the result comes out right.  */
12958       *expr_p = dummy_object (type);
12959       return GS_ALL_DONE;
12960     }
12961 
12962   tag = build_int_cst (build_pointer_type (type), 0);
12963   aptag = build_int_cst (TREE_TYPE (valist), 0);
12964 
12965   *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
12966 					  valist, tag, aptag);
12967 
12968   /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
12969      needs to be expanded.  */
12970   cfun->curr_properties &= ~PROP_gimple_lva;
12971 
12972   return GS_OK;
12973 }
12974 
12975 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
12976 
12977    DST/SRC are the destination and source respectively.  You can pass
12978    ungimplified trees in DST or SRC, in which case they will be
12979    converted to a gimple operand if necessary.
12980 
12981    This function returns the newly created GIMPLE_ASSIGN tuple.  */
12982 
12983 gimple *
12984 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
12985 {
12986   tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12987   gimplify_and_add (t, seq_p);
12988   ggc_free (t);
12989   return gimple_seq_last_stmt (*seq_p);
12990 }
12991 
12992 inline hashval_t
12993 gimplify_hasher::hash (const elt_t *p)
12994 {
12995   tree t = p->val;
12996   return iterative_hash_expr (t, 0);
12997 }
12998 
12999 inline bool
13000 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
13001 {
13002   tree t1 = p1->val;
13003   tree t2 = p2->val;
13004   enum tree_code code = TREE_CODE (t1);
13005 
13006   if (TREE_CODE (t2) != code
13007       || TREE_TYPE (t1) != TREE_TYPE (t2))
13008     return false;
13009 
13010   if (!operand_equal_p (t1, t2, 0))
13011     return false;
13012 
13013   /* Only allow them to compare equal if they also hash equal; otherwise
13014      results are nondeterminate, and we fail bootstrap comparison.  */
13015   gcc_checking_assert (hash (p1) == hash (p2));
13016 
13017   return true;
13018 }
13019