Update gcc-50 to SVN version 221423
[dragonfly.git] / contrib / gcc-5.0 / gcc / gimplify.c
1 /* Tree lowering pass.  This pass converts the GENERIC functions-as-trees
2    tree representation into the GIMPLE form.
3    Copyright (C) 2002-2015 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 "hash-set.h"
27 #include "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "options.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "fold-const.h"
38 #include "hashtab.h"
39 #include "tm.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "rtl.h"
43 #include "flags.h"
44 #include "statistics.h"
45 #include "real.h"
46 #include "fixed-value.h"
47 #include "insn-config.h"
48 #include "expmed.h"
49 #include "dojump.h"
50 #include "explow.h"
51 #include "calls.h"
52 #include "emit-rtl.h"
53 #include "varasm.h"
54 #include "stmt.h"
55 #include "expr.h"
56 #include "predict.h"
57 #include "basic-block.h"
58 #include "tree-ssa-alias.h"
59 #include "internal-fn.h"
60 #include "gimple-fold.h"
61 #include "tree-eh.h"
62 #include "gimple-expr.h"
63 #include "is-a.h"
64 #include "gimple.h"
65 #include "gimplify.h"
66 #include "gimple-iterator.h"
67 #include "stringpool.h"
68 #include "stor-layout.h"
69 #include "print-tree.h"
70 #include "tree-iterator.h"
71 #include "tree-inline.h"
72 #include "tree-pretty-print.h"
73 #include "langhooks.h"
74 #include "bitmap.h"
75 #include "gimple-ssa.h"
76 #include "hash-map.h"
77 #include "plugin-api.h"
78 #include "ipa-ref.h"
79 #include "cgraph.h"
80 #include "tree-cfg.h"
81 #include "tree-ssanames.h"
82 #include "tree-ssa.h"
83 #include "diagnostic-core.h"
84 #include "target.h"
85 #include "splay-tree.h"
86 #include "omp-low.h"
87 #include "gimple-low.h"
88 #include "cilk.h"
89 #include "gomp-constants.h"
90
91 #include "langhooks-def.h"      /* FIXME: for lhd_set_decl_assembler_name */
92 #include "tree-pass.h"          /* FIXME: only for PROP_gimple_any */
93 #include "builtins.h"
94
95 enum gimplify_omp_var_data
96 {
97   GOVD_SEEN = 1,
98   GOVD_EXPLICIT = 2,
99   GOVD_SHARED = 4,
100   GOVD_PRIVATE = 8,
101   GOVD_FIRSTPRIVATE = 16,
102   GOVD_LASTPRIVATE = 32,
103   GOVD_REDUCTION = 64,
104   GOVD_LOCAL = 128,
105   GOVD_MAP = 256,
106   GOVD_DEBUG_PRIVATE = 512,
107   GOVD_PRIVATE_OUTER_REF = 1024,
108   GOVD_LINEAR = 2048,
109   GOVD_ALIGNED = 4096,
110
111   /* Flag for GOVD_MAP: don't copy back.  */
112   GOVD_MAP_TO_ONLY = 8192,
113
114   GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
115                            | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
116                            | GOVD_LOCAL)
117 };
118
119
120 enum omp_region_type
121 {
122   ORT_WORKSHARE = 0,
123   ORT_SIMD = 1,
124   ORT_PARALLEL = 2,
125   ORT_COMBINED_PARALLEL = 3,
126   ORT_TASK = 4,
127   ORT_UNTIED_TASK = 5,
128   ORT_TEAMS = 8,
129   /* Data region.  */
130   ORT_TARGET_DATA = 16,
131   /* Data region with offloading.  */
132   ORT_TARGET = 32
133 };
134
135 /* Gimplify hashtable helper.  */
136
137 struct gimplify_hasher : typed_free_remove <elt_t>
138 {
139   typedef elt_t value_type;
140   typedef elt_t compare_type;
141   static inline hashval_t hash (const value_type *);
142   static inline bool equal (const value_type *, const compare_type *);
143 };
144
145 struct gimplify_ctx
146 {
147   struct gimplify_ctx *prev_context;
148
149   vec<gbind *> bind_expr_stack;
150   tree temps;
151   gimple_seq conditional_cleanups;
152   tree exit_label;
153   tree return_temp;
154
155   vec<tree> case_labels;
156   /* The formal temporary table.  Should this be persistent?  */
157   hash_table<gimplify_hasher> *temp_htab;
158
159   int conditions;
160   bool save_stack;
161   bool into_ssa;
162   bool allow_rhs_cond_expr;
163   bool in_cleanup_point_expr;
164 };
165
166 struct gimplify_omp_ctx
167 {
168   struct gimplify_omp_ctx *outer_context;
169   splay_tree variables;
170   hash_set<tree> *privatized_types;
171   location_t location;
172   enum omp_clause_default_kind default_kind;
173   enum omp_region_type region_type;
174   bool combined_loop;
175   bool distribute;
176 };
177
178 static struct gimplify_ctx *gimplify_ctxp;
179 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
180
181 /* Forward declaration.  */
182 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
183
184 /* Shorter alias name for the above function for use in gimplify.c
185    only.  */
186
187 static inline void
188 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
189 {
190   gimple_seq_add_stmt_without_update (seq_p, gs);
191 }
192
193 /* Append sequence SRC to the end of sequence *DST_P.  If *DST_P is
194    NULL, a new sequence is allocated.   This function is
195    similar to gimple_seq_add_seq, but does not scan the operands.
196    During gimplification, we need to manipulate statement sequences
197    before the def/use vectors have been constructed.  */
198
199 static void
200 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
201 {
202   gimple_stmt_iterator si;
203
204   if (src == NULL)
205     return;
206
207   si = gsi_last (*dst_p);
208   gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
209 }
210
211
212 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
213    and popping gimplify contexts.  */
214
215 static struct gimplify_ctx *ctx_pool = NULL;
216
217 /* Return a gimplify context struct from the pool.  */
218
219 static inline struct gimplify_ctx *
220 ctx_alloc (void)
221 {
222   struct gimplify_ctx * c = ctx_pool;
223
224   if (c)
225     ctx_pool = c->prev_context;
226   else
227     c = XNEW (struct gimplify_ctx);
228
229   memset (c, '\0', sizeof (*c));
230   return c;
231 }
232
233 /* Put gimplify context C back into the pool.  */
234
235 static inline void
236 ctx_free (struct gimplify_ctx *c)
237 {
238   c->prev_context = ctx_pool;
239   ctx_pool = c;
240 }
241
242 /* Free allocated ctx stack memory.  */
243
244 void
245 free_gimplify_stack (void)
246 {
247   struct gimplify_ctx *c;
248
249   while ((c = ctx_pool))
250     {
251       ctx_pool = c->prev_context;
252       free (c);
253     }
254 }
255
256
257 /* Set up a context for the gimplifier.  */
258
259 void
260 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
261 {
262   struct gimplify_ctx *c = ctx_alloc ();
263
264   c->prev_context = gimplify_ctxp;
265   gimplify_ctxp = c;
266   gimplify_ctxp->into_ssa = in_ssa;
267   gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
268 }
269
270 /* Tear down a context for the gimplifier.  If BODY is non-null, then
271    put the temporaries into the outer BIND_EXPR.  Otherwise, put them
272    in the local_decls.
273
274    BODY is not a sequence, but the first tuple in a sequence.  */
275
276 void
277 pop_gimplify_context (gimple body)
278 {
279   struct gimplify_ctx *c = gimplify_ctxp;
280
281   gcc_assert (c
282               && (!c->bind_expr_stack.exists ()
283                   || c->bind_expr_stack.is_empty ()));
284   c->bind_expr_stack.release ();
285   gimplify_ctxp = c->prev_context;
286
287   if (body)
288     declare_vars (c->temps, body, false);
289   else
290     record_vars (c->temps);
291
292   delete c->temp_htab;
293   c->temp_htab = NULL;
294   ctx_free (c);
295 }
296
297 /* Push a GIMPLE_BIND tuple onto the stack of bindings.  */
298
299 static void
300 gimple_push_bind_expr (gbind *bind_stmt)
301 {
302   gimplify_ctxp->bind_expr_stack.reserve (8);
303   gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
304 }
305
306 /* Pop the first element off the stack of bindings.  */
307
308 static void
309 gimple_pop_bind_expr (void)
310 {
311   gimplify_ctxp->bind_expr_stack.pop ();
312 }
313
314 /* Return the first element of the stack of bindings.  */
315
316 gbind *
317 gimple_current_bind_expr (void)
318 {
319   return gimplify_ctxp->bind_expr_stack.last ();
320 }
321
322 /* Return the stack of bindings created during gimplification.  */
323
324 vec<gbind *>
325 gimple_bind_expr_stack (void)
326 {
327   return gimplify_ctxp->bind_expr_stack;
328 }
329
330 /* Return true iff there is a COND_EXPR between us and the innermost
331    CLEANUP_POINT_EXPR.  This info is used by gimple_push_cleanup.  */
332
333 static bool
334 gimple_conditional_context (void)
335 {
336   return gimplify_ctxp->conditions > 0;
337 }
338
339 /* Note that we've entered a COND_EXPR.  */
340
341 static void
342 gimple_push_condition (void)
343 {
344 #ifdef ENABLE_GIMPLE_CHECKING
345   if (gimplify_ctxp->conditions == 0)
346     gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
347 #endif
348   ++(gimplify_ctxp->conditions);
349 }
350
351 /* Note that we've left a COND_EXPR.  If we're back at unconditional scope
352    now, add any conditional cleanups we've seen to the prequeue.  */
353
354 static void
355 gimple_pop_condition (gimple_seq *pre_p)
356 {
357   int conds = --(gimplify_ctxp->conditions);
358
359   gcc_assert (conds >= 0);
360   if (conds == 0)
361     {
362       gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
363       gimplify_ctxp->conditional_cleanups = NULL;
364     }
365 }
366
367 /* A stable comparison routine for use with splay trees and DECLs.  */
368
369 static int
370 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
371 {
372   tree a = (tree) xa;
373   tree b = (tree) xb;
374
375   return DECL_UID (a) - DECL_UID (b);
376 }
377
378 /* Create a new omp construct that deals with variable remapping.  */
379
380 static struct gimplify_omp_ctx *
381 new_omp_context (enum omp_region_type region_type)
382 {
383   struct gimplify_omp_ctx *c;
384
385   c = XCNEW (struct gimplify_omp_ctx);
386   c->outer_context = gimplify_omp_ctxp;
387   c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
388   c->privatized_types = new hash_set<tree>;
389   c->location = input_location;
390   c->region_type = region_type;
391   if ((region_type & ORT_TASK) == 0)
392     c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
393   else
394     c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
395
396   return c;
397 }
398
399 /* Destroy an omp construct that deals with variable remapping.  */
400
401 static void
402 delete_omp_context (struct gimplify_omp_ctx *c)
403 {
404   splay_tree_delete (c->variables);
405   delete c->privatized_types;
406   XDELETE (c);
407 }
408
409 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
410 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
411
412 /* Both gimplify the statement T and append it to *SEQ_P.  This function
413    behaves exactly as gimplify_stmt, but you don't have to pass T as a
414    reference.  */
415
416 void
417 gimplify_and_add (tree t, gimple_seq *seq_p)
418 {
419   gimplify_stmt (&t, seq_p);
420 }
421
422 /* Gimplify statement T into sequence *SEQ_P, and return the first
423    tuple in the sequence of generated tuples for this statement.
424    Return NULL if gimplifying T produced no tuples.  */
425
426 static gimple
427 gimplify_and_return_first (tree t, gimple_seq *seq_p)
428 {
429   gimple_stmt_iterator last = gsi_last (*seq_p);
430
431   gimplify_and_add (t, seq_p);
432
433   if (!gsi_end_p (last))
434     {
435       gsi_next (&last);
436       return gsi_stmt (last);
437     }
438   else
439     return gimple_seq_first_stmt (*seq_p);
440 }
441
442 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
443    LHS, or for a call argument.  */
444
445 static bool
446 is_gimple_mem_rhs (tree t)
447 {
448   /* If we're dealing with a renamable type, either source or dest must be
449      a renamed variable.  */
450   if (is_gimple_reg_type (TREE_TYPE (t)))
451     return is_gimple_val (t);
452   else
453     return is_gimple_val (t) || is_gimple_lvalue (t);
454 }
455
456 /* Return true if T is a CALL_EXPR or an expression that can be
457    assigned to a temporary.  Note that this predicate should only be
458    used during gimplification.  See the rationale for this in
459    gimplify_modify_expr.  */
460
461 static bool
462 is_gimple_reg_rhs_or_call (tree t)
463 {
464   return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
465           || TREE_CODE (t) == CALL_EXPR);
466 }
467
468 /* Return true if T is a valid memory RHS or a CALL_EXPR.  Note that
469    this predicate should only be used during gimplification.  See the
470    rationale for this in gimplify_modify_expr.  */
471
472 static bool
473 is_gimple_mem_rhs_or_call (tree t)
474 {
475   /* If we're dealing with a renamable type, either source or dest must be
476      a renamed variable.  */
477   if (is_gimple_reg_type (TREE_TYPE (t)))
478     return is_gimple_val (t);
479   else
480     return (is_gimple_val (t) || is_gimple_lvalue (t)
481             || TREE_CODE (t) == CALL_EXPR);
482 }
483
484 /* Create a temporary with a name derived from VAL.  Subroutine of
485    lookup_tmp_var; nobody else should call this function.  */
486
487 static inline tree
488 create_tmp_from_val (tree val)
489 {
490   /* Drop all qualifiers and address-space information from the value type.  */
491   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
492   tree var = create_tmp_var (type, get_name (val));
493   if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
494       || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
495     DECL_GIMPLE_REG_P (var) = 1;
496   return var;
497 }
498
499 /* Create a temporary to hold the value of VAL.  If IS_FORMAL, try to reuse
500    an existing expression temporary.  */
501
502 static tree
503 lookup_tmp_var (tree val, bool is_formal)
504 {
505   tree ret;
506
507   /* If not optimizing, never really reuse a temporary.  local-alloc
508      won't allocate any variable that is used in more than one basic
509      block, which means it will go into memory, causing much extra
510      work in reload and final and poorer code generation, outweighing
511      the extra memory allocation here.  */
512   if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
513     ret = create_tmp_from_val (val);
514   else
515     {
516       elt_t elt, *elt_p;
517       elt_t **slot;
518
519       elt.val = val;
520       if (!gimplify_ctxp->temp_htab)
521         gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
522       slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
523       if (*slot == NULL)
524         {
525           elt_p = XNEW (elt_t);
526           elt_p->val = val;
527           elt_p->temp = ret = create_tmp_from_val (val);
528           *slot = elt_p;
529         }
530       else
531         {
532           elt_p = *slot;
533           ret = elt_p->temp;
534         }
535     }
536
537   return ret;
538 }
539
540 /* Helper for get_formal_tmp_var and get_initialized_tmp_var.  */
541
542 static tree
543 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
544                       bool is_formal)
545 {
546   tree t, mod;
547
548   /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
549      can create an INIT_EXPR and convert it into a GIMPLE_CALL below.  */
550   gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
551                  fb_rvalue);
552
553   if (gimplify_ctxp->into_ssa
554       && is_gimple_reg_type (TREE_TYPE (val)))
555     t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
556   else
557     t = lookup_tmp_var (val, is_formal);
558
559   mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
560
561   SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
562
563   /* gimplify_modify_expr might want to reduce this further.  */
564   gimplify_and_add (mod, pre_p);
565   ggc_free (mod);
566
567   return t;
568 }
569
570 /* Return a formal temporary variable initialized with VAL.  PRE_P is as
571    in gimplify_expr.  Only use this function if:
572
573    1) The value of the unfactored expression represented by VAL will not
574       change between the initialization and use of the temporary, and
575    2) The temporary will not be otherwise modified.
576
577    For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
578    and #2 means it is inappropriate for && temps.
579
580    For other cases, use get_initialized_tmp_var instead.  */
581
582 tree
583 get_formal_tmp_var (tree val, gimple_seq *pre_p)
584 {
585   return internal_get_tmp_var (val, pre_p, NULL, true);
586 }
587
588 /* Return a temporary variable initialized with VAL.  PRE_P and POST_P
589    are as in gimplify_expr.  */
590
591 tree
592 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
593 {
594   return internal_get_tmp_var (val, pre_p, post_p, false);
595 }
596
597 /* Declare all the variables in VARS in SCOPE.  If DEBUG_INFO is true,
598    generate debug info for them; otherwise don't.  */
599
600 void
601 declare_vars (tree vars, gimple gs, bool debug_info)
602 {
603   tree last = vars;
604   if (last)
605     {
606       tree temps, block;
607
608       gbind *scope = as_a <gbind *> (gs);
609
610       temps = nreverse (last);
611
612       block = gimple_bind_block (scope);
613       gcc_assert (!block || TREE_CODE (block) == BLOCK);
614       if (!block || !debug_info)
615         {
616           DECL_CHAIN (last) = gimple_bind_vars (scope);
617           gimple_bind_set_vars (scope, temps);
618         }
619       else
620         {
621           /* We need to attach the nodes both to the BIND_EXPR and to its
622              associated BLOCK for debugging purposes.  The key point here
623              is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
624              is a subchain of the BIND_EXPR_VARS of the BIND_EXPR.  */
625           if (BLOCK_VARS (block))
626             BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
627           else
628             {
629               gimple_bind_set_vars (scope,
630                                     chainon (gimple_bind_vars (scope), temps));
631               BLOCK_VARS (block) = temps;
632             }
633         }
634     }
635 }
636
637 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
638    for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly.  Abort if
639    no such upper bound can be obtained.  */
640
641 static void
642 force_constant_size (tree var)
643 {
644   /* The only attempt we make is by querying the maximum size of objects
645      of the variable's type.  */
646
647   HOST_WIDE_INT max_size;
648
649   gcc_assert (TREE_CODE (var) == VAR_DECL);
650
651   max_size = max_int_size_in_bytes (TREE_TYPE (var));
652
653   gcc_assert (max_size >= 0);
654
655   DECL_SIZE_UNIT (var)
656     = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
657   DECL_SIZE (var)
658     = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
659 }
660
661 /* Push the temporary variable TMP into the current binding.  */
662
663 void
664 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
665 {
666   gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
667
668   /* Later processing assumes that the object size is constant, which might
669      not be true at this point.  Force the use of a constant upper bound in
670      this case.  */
671   if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
672     force_constant_size (tmp);
673
674   DECL_CONTEXT (tmp) = fn->decl;
675   DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
676
677   record_vars_into (tmp, fn->decl);
678 }
679
680 /* Push the temporary variable TMP into the current binding.  */
681
682 void
683 gimple_add_tmp_var (tree tmp)
684 {
685   gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
686
687   /* Later processing assumes that the object size is constant, which might
688      not be true at this point.  Force the use of a constant upper bound in
689      this case.  */
690   if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
691     force_constant_size (tmp);
692
693   DECL_CONTEXT (tmp) = current_function_decl;
694   DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
695
696   if (gimplify_ctxp)
697     {
698       DECL_CHAIN (tmp) = gimplify_ctxp->temps;
699       gimplify_ctxp->temps = tmp;
700
701       /* Mark temporaries local within the nearest enclosing parallel.  */
702       if (gimplify_omp_ctxp)
703         {
704           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
705           while (ctx
706                  && (ctx->region_type == ORT_WORKSHARE
707                      || ctx->region_type == ORT_SIMD))
708             ctx = ctx->outer_context;
709           if (ctx)
710             omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
711         }
712     }
713   else if (cfun)
714     record_vars (tmp);
715   else
716     {
717       gimple_seq body_seq;
718
719       /* This case is for nested functions.  We need to expose the locals
720          they create.  */
721       body_seq = gimple_body (current_function_decl);
722       declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
723     }
724 }
725
726
727 \f
728 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
729    nodes that are referenced more than once in GENERIC functions.  This is
730    necessary because gimplification (translation into GIMPLE) is performed
731    by modifying tree nodes in-place, so gimplication of a shared node in a
732    first context could generate an invalid GIMPLE form in a second context.
733
734    This is achieved with a simple mark/copy/unmark algorithm that walks the
735    GENERIC representation top-down, marks nodes with TREE_VISITED the first
736    time it encounters them, duplicates them if they already have TREE_VISITED
737    set, and finally removes the TREE_VISITED marks it has set.
738
739    The algorithm works only at the function level, i.e. it generates a GENERIC
740    representation of a function with no nodes shared within the function when
741    passed a GENERIC function (except for nodes that are allowed to be shared).
742
743    At the global level, it is also necessary to unshare tree nodes that are
744    referenced in more than one function, for the same aforementioned reason.
745    This requires some cooperation from the front-end.  There are 2 strategies:
746
747      1. Manual unsharing.  The front-end needs to call unshare_expr on every
748         expression that might end up being shared across functions.
749
750      2. Deep unsharing.  This is an extension of regular unsharing.  Instead
751         of calling unshare_expr on expressions that might be shared across
752         functions, the front-end pre-marks them with TREE_VISITED.  This will
753         ensure that they are unshared on the first reference within functions
754         when the regular unsharing algorithm runs.  The counterpart is that
755         this algorithm must look deeper than for manual unsharing, which is
756         specified by LANG_HOOKS_DEEP_UNSHARING.
757
758   If there are only few specific cases of node sharing across functions, it is
759   probably easier for a front-end to unshare the expressions manually.  On the
760   contrary, if the expressions generated at the global level are as widespread
761   as expressions generated within functions, deep unsharing is very likely the
762   way to go.  */
763
764 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
765    These nodes model computations that must be done once.  If we were to
766    unshare something like SAVE_EXPR(i++), the gimplification process would
767    create wrong code.  However, if DATA is non-null, it must hold a pointer
768    set that is used to unshare the subtrees of these nodes.  */
769
770 static tree
771 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
772 {
773   tree t = *tp;
774   enum tree_code code = TREE_CODE (t);
775
776   /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
777      copy their subtrees if we can make sure to do it only once.  */
778   if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
779     {
780       if (data && !((hash_set<tree> *)data)->add (t))
781         ;
782       else
783         *walk_subtrees = 0;
784     }
785
786   /* Stop at types, decls, constants like copy_tree_r.  */
787   else if (TREE_CODE_CLASS (code) == tcc_type
788            || TREE_CODE_CLASS (code) == tcc_declaration
789            || TREE_CODE_CLASS (code) == tcc_constant
790            /* We can't do anything sensible with a BLOCK used as an
791               expression, but we also can't just die when we see it
792               because of non-expression uses.  So we avert our eyes
793               and cross our fingers.  Silly Java.  */
794            || code == BLOCK)
795     *walk_subtrees = 0;
796
797   /* Cope with the statement expression extension.  */
798   else if (code == STATEMENT_LIST)
799     ;
800
801   /* Leave the bulk of the work to copy_tree_r itself.  */
802   else
803     copy_tree_r (tp, walk_subtrees, NULL);
804
805   return NULL_TREE;
806 }
807
808 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
809    If *TP has been visited already, then *TP is deeply copied by calling
810    mostly_copy_tree_r.  DATA is passed to mostly_copy_tree_r unmodified.  */
811
812 static tree
813 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
814 {
815   tree t = *tp;
816   enum tree_code code = TREE_CODE (t);
817
818   /* Skip types, decls, and constants.  But we do want to look at their
819      types and the bounds of types.  Mark them as visited so we properly
820      unmark their subtrees on the unmark pass.  If we've already seen them,
821      don't look down further.  */
822   if (TREE_CODE_CLASS (code) == tcc_type
823       || TREE_CODE_CLASS (code) == tcc_declaration
824       || TREE_CODE_CLASS (code) == tcc_constant)
825     {
826       if (TREE_VISITED (t))
827         *walk_subtrees = 0;
828       else
829         TREE_VISITED (t) = 1;
830     }
831
832   /* If this node has been visited already, unshare it and don't look
833      any deeper.  */
834   else if (TREE_VISITED (t))
835     {
836       walk_tree (tp, mostly_copy_tree_r, data, NULL);
837       *walk_subtrees = 0;
838     }
839
840   /* Otherwise, mark the node as visited and keep looking.  */
841   else
842     TREE_VISITED (t) = 1;
843
844   return NULL_TREE;
845 }
846
847 /* Unshare most of the shared trees rooted at *TP.  DATA is passed to the
848    copy_if_shared_r callback unmodified.  */
849
850 static inline void
851 copy_if_shared (tree *tp, void *data)
852 {
853   walk_tree (tp, copy_if_shared_r, data, NULL);
854 }
855
856 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
857    any nested functions.  */
858
859 static void
860 unshare_body (tree fndecl)
861 {
862   struct cgraph_node *cgn = cgraph_node::get (fndecl);
863   /* If the language requires deep unsharing, we need a pointer set to make
864      sure we don't repeatedly unshare subtrees of unshareable nodes.  */
865   hash_set<tree> *visited
866     = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
867
868   copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
869   copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
870   copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
871
872   delete visited;
873
874   if (cgn)
875     for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
876       unshare_body (cgn->decl);
877 }
878
879 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
880    Subtrees are walked until the first unvisited node is encountered.  */
881
882 static tree
883 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
884 {
885   tree t = *tp;
886
887   /* If this node has been visited, unmark it and keep looking.  */
888   if (TREE_VISITED (t))
889     TREE_VISITED (t) = 0;
890
891   /* Otherwise, don't look any deeper.  */
892   else
893     *walk_subtrees = 0;
894
895   return NULL_TREE;
896 }
897
898 /* Unmark the visited trees rooted at *TP.  */
899
900 static inline void
901 unmark_visited (tree *tp)
902 {
903   walk_tree (tp, unmark_visited_r, NULL, NULL);
904 }
905
906 /* Likewise, but mark all trees as not visited.  */
907
908 static void
909 unvisit_body (tree fndecl)
910 {
911   struct cgraph_node *cgn = cgraph_node::get (fndecl);
912
913   unmark_visited (&DECL_SAVED_TREE (fndecl));
914   unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
915   unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
916
917   if (cgn)
918     for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
919       unvisit_body (cgn->decl);
920 }
921
922 /* Unconditionally make an unshared copy of EXPR.  This is used when using
923    stored expressions which span multiple functions, such as BINFO_VTABLE,
924    as the normal unsharing process can't tell that they're shared.  */
925
926 tree
927 unshare_expr (tree expr)
928 {
929   walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
930   return expr;
931 }
932
933 /* Worker for unshare_expr_without_location.  */
934
935 static tree
936 prune_expr_location (tree *tp, int *walk_subtrees, void *)
937 {
938   if (EXPR_P (*tp))
939     SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
940   else
941     *walk_subtrees = 0;
942   return NULL_TREE;
943 }
944
945 /* Similar to unshare_expr but also prune all expression locations
946    from EXPR.  */
947
948 tree
949 unshare_expr_without_location (tree expr)
950 {
951   walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
952   if (EXPR_P (expr))
953     walk_tree (&expr, prune_expr_location, NULL, NULL);
954   return expr;
955 }
956 \f
957 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
958    contain statements and have a value.  Assign its value to a temporary
959    and give it void_type_node.  Return the temporary, or NULL_TREE if
960    WRAPPER was already void.  */
961
962 tree
963 voidify_wrapper_expr (tree wrapper, tree temp)
964 {
965   tree type = TREE_TYPE (wrapper);
966   if (type && !VOID_TYPE_P (type))
967     {
968       tree *p;
969
970       /* Set p to point to the body of the wrapper.  Loop until we find
971          something that isn't a wrapper.  */
972       for (p = &wrapper; p && *p; )
973         {
974           switch (TREE_CODE (*p))
975             {
976             case BIND_EXPR:
977               TREE_SIDE_EFFECTS (*p) = 1;
978               TREE_TYPE (*p) = void_type_node;
979               /* For a BIND_EXPR, the body is operand 1.  */
980               p = &BIND_EXPR_BODY (*p);
981               break;
982
983             case CLEANUP_POINT_EXPR:
984             case TRY_FINALLY_EXPR:
985             case TRY_CATCH_EXPR:
986               TREE_SIDE_EFFECTS (*p) = 1;
987               TREE_TYPE (*p) = void_type_node;
988               p = &TREE_OPERAND (*p, 0);
989               break;
990
991             case STATEMENT_LIST:
992               {
993                 tree_stmt_iterator i = tsi_last (*p);
994                 TREE_SIDE_EFFECTS (*p) = 1;
995                 TREE_TYPE (*p) = void_type_node;
996                 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
997               }
998               break;
999
1000             case COMPOUND_EXPR:
1001               /* Advance to the last statement.  Set all container types to
1002                  void.  */
1003               for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1004                 {
1005                   TREE_SIDE_EFFECTS (*p) = 1;
1006                   TREE_TYPE (*p) = void_type_node;
1007                 }
1008               break;
1009
1010             case TRANSACTION_EXPR:
1011               TREE_SIDE_EFFECTS (*p) = 1;
1012               TREE_TYPE (*p) = void_type_node;
1013               p = &TRANSACTION_EXPR_BODY (*p);
1014               break;
1015
1016             default:
1017               /* Assume that any tree upon which voidify_wrapper_expr is
1018                  directly called is a wrapper, and that its body is op0.  */
1019               if (p == &wrapper)
1020                 {
1021                   TREE_SIDE_EFFECTS (*p) = 1;
1022                   TREE_TYPE (*p) = void_type_node;
1023                   p = &TREE_OPERAND (*p, 0);
1024                   break;
1025                 }
1026               goto out;
1027             }
1028         }
1029
1030     out:
1031       if (p == NULL || IS_EMPTY_STMT (*p))
1032         temp = NULL_TREE;
1033       else if (temp)
1034         {
1035           /* The wrapper is on the RHS of an assignment that we're pushing
1036              down.  */
1037           gcc_assert (TREE_CODE (temp) == INIT_EXPR
1038                       || TREE_CODE (temp) == MODIFY_EXPR);
1039           TREE_OPERAND (temp, 1) = *p;
1040           *p = temp;
1041         }
1042       else
1043         {
1044           temp = create_tmp_var (type, "retval");
1045           *p = build2 (INIT_EXPR, type, temp, *p);
1046         }
1047
1048       return temp;
1049     }
1050
1051   return NULL_TREE;
1052 }
1053
1054 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1055    a temporary through which they communicate.  */
1056
1057 static void
1058 build_stack_save_restore (gcall **save, gcall **restore)
1059 {
1060   tree tmp_var;
1061
1062   *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1063   tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1064   gimple_call_set_lhs (*save, tmp_var);
1065
1066   *restore
1067     = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1068                          1, tmp_var);
1069 }
1070
1071 /* Gimplify a BIND_EXPR.  Just voidify and recurse.  */
1072
1073 static enum gimplify_status
1074 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1075 {
1076   tree bind_expr = *expr_p;
1077   bool old_save_stack = gimplify_ctxp->save_stack;
1078   tree t;
1079   gbind *bind_stmt;
1080   gimple_seq body, cleanup;
1081   gcall *stack_save;
1082   location_t start_locus = 0, end_locus = 0;
1083
1084   tree temp = voidify_wrapper_expr (bind_expr, NULL);
1085
1086   /* Mark variables seen in this bind expr.  */
1087   for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1088     {
1089       if (TREE_CODE (t) == VAR_DECL)
1090         {
1091           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1092
1093           /* Mark variable as local.  */
1094           if (ctx && !DECL_EXTERNAL (t)
1095               && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1096                   || splay_tree_lookup (ctx->variables,
1097                                         (splay_tree_key) t) == NULL))
1098             {
1099               if (ctx->region_type == ORT_SIMD
1100                   && TREE_ADDRESSABLE (t)
1101                   && !TREE_STATIC (t))
1102                 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1103               else
1104                 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1105             }
1106
1107           DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1108
1109           if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1110             cfun->has_local_explicit_reg_vars = true;
1111         }
1112
1113       /* Preliminarily mark non-addressed complex variables as eligible
1114          for promotion to gimple registers.  We'll transform their uses
1115          as we find them.  */
1116       if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1117            || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1118           && !TREE_THIS_VOLATILE (t)
1119           && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1120           && !needs_to_live_in_memory (t))
1121         DECL_GIMPLE_REG_P (t) = 1;
1122     }
1123
1124   bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1125                                    BIND_EXPR_BLOCK (bind_expr));
1126   gimple_push_bind_expr (bind_stmt);
1127
1128   gimplify_ctxp->save_stack = false;
1129
1130   /* Gimplify the body into the GIMPLE_BIND tuple's body.  */
1131   body = NULL;
1132   gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1133   gimple_bind_set_body (bind_stmt, body);
1134
1135   /* Source location wise, the cleanup code (stack_restore and clobbers)
1136      belongs to the end of the block, so propagate what we have.  The
1137      stack_save operation belongs to the beginning of block, which we can
1138      infer from the bind_expr directly if the block has no explicit
1139      assignment.  */
1140   if (BIND_EXPR_BLOCK (bind_expr))
1141     {
1142       end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1143       start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1144     }
1145   if (start_locus == 0)
1146     start_locus = EXPR_LOCATION (bind_expr);
1147
1148   cleanup = NULL;
1149   stack_save = NULL;
1150   if (gimplify_ctxp->save_stack)
1151     {
1152       gcall *stack_restore;
1153
1154       /* Save stack on entry and restore it on exit.  Add a try_finally
1155          block to achieve this.  */
1156       build_stack_save_restore (&stack_save, &stack_restore);
1157
1158       gimple_set_location (stack_save, start_locus);
1159       gimple_set_location (stack_restore, end_locus);
1160
1161       gimplify_seq_add_stmt (&cleanup, stack_restore);
1162     }
1163
1164   /* Add clobbers for all variables that go out of scope.  */
1165   for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1166     {
1167       if (TREE_CODE (t) == VAR_DECL
1168           && !is_global_var (t)
1169           && DECL_CONTEXT (t) == current_function_decl
1170           && !DECL_HARD_REGISTER (t)
1171           && !TREE_THIS_VOLATILE (t)
1172           && !DECL_HAS_VALUE_EXPR_P (t)
1173           /* Only care for variables that have to be in memory.  Others
1174              will be rewritten into SSA names, hence moved to the top-level.  */
1175           && !is_gimple_reg (t)
1176           && flag_stack_reuse != SR_NONE)
1177         {
1178           tree clobber = build_constructor (TREE_TYPE (t), NULL);
1179           gimple clobber_stmt;
1180           TREE_THIS_VOLATILE (clobber) = 1;
1181           clobber_stmt = gimple_build_assign (t, clobber);
1182           gimple_set_location (clobber_stmt, end_locus);
1183           gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1184         }
1185     }
1186
1187   if (cleanup)
1188     {
1189       gtry *gs;
1190       gimple_seq new_body;
1191
1192       new_body = NULL;
1193       gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1194                              GIMPLE_TRY_FINALLY);
1195
1196       if (stack_save)
1197         gimplify_seq_add_stmt (&new_body, stack_save);
1198       gimplify_seq_add_stmt (&new_body, gs);
1199       gimple_bind_set_body (bind_stmt, new_body);
1200     }
1201
1202   gimplify_ctxp->save_stack = old_save_stack;
1203   gimple_pop_bind_expr ();
1204
1205   gimplify_seq_add_stmt (pre_p, bind_stmt);
1206
1207   if (temp)
1208     {
1209       *expr_p = temp;
1210       return GS_OK;
1211     }
1212
1213   *expr_p = NULL_TREE;
1214   return GS_ALL_DONE;
1215 }
1216
1217 /* Gimplify a RETURN_EXPR.  If the expression to be returned is not a
1218    GIMPLE value, it is assigned to a new temporary and the statement is
1219    re-written to return the temporary.
1220
1221    PRE_P points to the sequence where side effects that must happen before
1222    STMT should be stored.  */
1223
1224 static enum gimplify_status
1225 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1226 {
1227   greturn *ret;
1228   tree ret_expr = TREE_OPERAND (stmt, 0);
1229   tree result_decl, result;
1230
1231   if (ret_expr == error_mark_node)
1232     return GS_ERROR;
1233
1234   /* Implicit _Cilk_sync must be inserted right before any return statement 
1235      if there is a _Cilk_spawn in the function.  If the user has provided a 
1236      _Cilk_sync, the optimizer should remove this duplicate one.  */
1237   if (fn_contains_cilk_spawn_p (cfun))
1238     {
1239       tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1240       gimplify_and_add (impl_sync, pre_p);
1241     }
1242
1243   if (!ret_expr
1244       || TREE_CODE (ret_expr) == RESULT_DECL
1245       || ret_expr == error_mark_node)
1246     {
1247       greturn *ret = gimple_build_return (ret_expr);
1248       gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1249       gimplify_seq_add_stmt (pre_p, ret);
1250       return GS_ALL_DONE;
1251     }
1252
1253   if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1254     result_decl = NULL_TREE;
1255   else
1256     {
1257       result_decl = TREE_OPERAND (ret_expr, 0);
1258
1259       /* See through a return by reference.  */
1260       if (TREE_CODE (result_decl) == INDIRECT_REF)
1261         result_decl = TREE_OPERAND (result_decl, 0);
1262
1263       gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1264                    || TREE_CODE (ret_expr) == INIT_EXPR)
1265                   && TREE_CODE (result_decl) == RESULT_DECL);
1266     }
1267
1268   /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1269      Recall that aggregate_value_p is FALSE for any aggregate type that is
1270      returned in registers.  If we're returning values in registers, then
1271      we don't want to extend the lifetime of the RESULT_DECL, particularly
1272      across another call.  In addition, for those aggregates for which
1273      hard_function_value generates a PARALLEL, we'll die during normal
1274      expansion of structure assignments; there's special code in expand_return
1275      to handle this case that does not exist in expand_expr.  */
1276   if (!result_decl)
1277     result = NULL_TREE;
1278   else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1279     {
1280       if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1281         {
1282           if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1283             gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1284           /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1285              should be effectively allocated by the caller, i.e. all calls to
1286              this function must be subject to the Return Slot Optimization.  */
1287           gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1288           gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1289         }
1290       result = result_decl;
1291     }
1292   else if (gimplify_ctxp->return_temp)
1293     result = gimplify_ctxp->return_temp;
1294   else
1295     {
1296       result = create_tmp_reg (TREE_TYPE (result_decl));
1297
1298       /* ??? With complex control flow (usually involving abnormal edges),
1299          we can wind up warning about an uninitialized value for this.  Due
1300          to how this variable is constructed and initialized, this is never
1301          true.  Give up and never warn.  */
1302       TREE_NO_WARNING (result) = 1;
1303
1304       gimplify_ctxp->return_temp = result;
1305     }
1306
1307   /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1308      Then gimplify the whole thing.  */
1309   if (result != result_decl)
1310     TREE_OPERAND (ret_expr, 0) = result;
1311
1312   gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1313
1314   ret = gimple_build_return (result);
1315   gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1316   gimplify_seq_add_stmt (pre_p, ret);
1317
1318   return GS_ALL_DONE;
1319 }
1320
1321 /* Gimplify a variable-length array DECL.  */
1322
1323 static void
1324 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1325 {
1326   /* This is a variable-sized decl.  Simplify its size and mark it
1327      for deferred expansion.  */
1328   tree t, addr, ptr_type;
1329
1330   gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1331   gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1332
1333   /* Don't mess with a DECL_VALUE_EXPR set by the front-end.  */
1334   if (DECL_HAS_VALUE_EXPR_P (decl))
1335     return;
1336
1337   /* All occurrences of this decl in final gimplified code will be
1338      replaced by indirection.  Setting DECL_VALUE_EXPR does two
1339      things: First, it lets the rest of the gimplifier know what
1340      replacement to use.  Second, it lets the debug info know
1341      where to find the value.  */
1342   ptr_type = build_pointer_type (TREE_TYPE (decl));
1343   addr = create_tmp_var (ptr_type, get_name (decl));
1344   DECL_IGNORED_P (addr) = 0;
1345   t = build_fold_indirect_ref (addr);
1346   TREE_THIS_NOTRAP (t) = 1;
1347   SET_DECL_VALUE_EXPR (decl, t);
1348   DECL_HAS_VALUE_EXPR_P (decl) = 1;
1349
1350   t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1351   t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1352                        size_int (DECL_ALIGN (decl)));
1353   /* The call has been built for a variable-sized object.  */
1354   CALL_ALLOCA_FOR_VAR_P (t) = 1;
1355   t = fold_convert (ptr_type, t);
1356   t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1357
1358   gimplify_and_add (t, seq_p);
1359
1360   /* Indicate that we need to restore the stack level when the
1361      enclosing BIND_EXPR is exited.  */
1362   gimplify_ctxp->save_stack = true;
1363 }
1364
1365 /* A helper function to be called via walk_tree.  Mark all labels under *TP
1366    as being forced.  To be called for DECL_INITIAL of static variables.  */
1367
1368 static tree
1369 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1370 {
1371   if (TYPE_P (*tp))
1372     *walk_subtrees = 0;
1373   if (TREE_CODE (*tp) == LABEL_DECL)
1374     FORCED_LABEL (*tp) = 1;
1375
1376   return NULL_TREE;
1377 }
1378
1379 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1380    and initialization explicit.  */
1381
1382 static enum gimplify_status
1383 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1384 {
1385   tree stmt = *stmt_p;
1386   tree decl = DECL_EXPR_DECL (stmt);
1387
1388   *stmt_p = NULL_TREE;
1389
1390   if (TREE_TYPE (decl) == error_mark_node)
1391     return GS_ERROR;
1392
1393   if ((TREE_CODE (decl) == TYPE_DECL
1394        || TREE_CODE (decl) == VAR_DECL)
1395       && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1396     gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1397
1398   /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1399      in case its size expressions contain problematic nodes like CALL_EXPR.  */
1400   if (TREE_CODE (decl) == TYPE_DECL
1401       && DECL_ORIGINAL_TYPE (decl)
1402       && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1403     gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1404
1405   if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1406     {
1407       tree init = DECL_INITIAL (decl);
1408
1409       if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1410           || (!TREE_STATIC (decl)
1411               && flag_stack_check == GENERIC_STACK_CHECK
1412               && compare_tree_int (DECL_SIZE_UNIT (decl),
1413                                    STACK_CHECK_MAX_VAR_SIZE) > 0))
1414         gimplify_vla_decl (decl, seq_p);
1415
1416       /* Some front ends do not explicitly declare all anonymous
1417          artificial variables.  We compensate here by declaring the
1418          variables, though it would be better if the front ends would
1419          explicitly declare them.  */
1420       if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1421           && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1422         gimple_add_tmp_var (decl);
1423
1424       if (init && init != error_mark_node)
1425         {
1426           if (!TREE_STATIC (decl))
1427             {
1428               DECL_INITIAL (decl) = NULL_TREE;
1429               init = build2 (INIT_EXPR, void_type_node, decl, init);
1430               gimplify_and_add (init, seq_p);
1431               ggc_free (init);
1432             }
1433           else
1434             /* We must still examine initializers for static variables
1435                as they may contain a label address.  */
1436             walk_tree (&init, force_labels_r, NULL, NULL);
1437         }
1438     }
1439
1440   return GS_ALL_DONE;
1441 }
1442
1443 /* Gimplify a LOOP_EXPR.  Normally this just involves gimplifying the body
1444    and replacing the LOOP_EXPR with goto, but if the loop contains an
1445    EXIT_EXPR, we need to append a label for it to jump to.  */
1446
1447 static enum gimplify_status
1448 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1449 {
1450   tree saved_label = gimplify_ctxp->exit_label;
1451   tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1452
1453   gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1454
1455   gimplify_ctxp->exit_label = NULL_TREE;
1456
1457   gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1458
1459   gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1460
1461   if (gimplify_ctxp->exit_label)
1462     gimplify_seq_add_stmt (pre_p,
1463                            gimple_build_label (gimplify_ctxp->exit_label));
1464
1465   gimplify_ctxp->exit_label = saved_label;
1466
1467   *expr_p = NULL;
1468   return GS_ALL_DONE;
1469 }
1470
1471 /* Gimplify a statement list onto a sequence.  These may be created either
1472    by an enlightened front-end, or by shortcut_cond_expr.  */
1473
1474 static enum gimplify_status
1475 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1476 {
1477   tree temp = voidify_wrapper_expr (*expr_p, NULL);
1478
1479   tree_stmt_iterator i = tsi_start (*expr_p);
1480
1481   while (!tsi_end_p (i))
1482     {
1483       gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1484       tsi_delink (&i);
1485     }
1486
1487   if (temp)
1488     {
1489       *expr_p = temp;
1490       return GS_OK;
1491     }
1492
1493   return GS_ALL_DONE;
1494 }
1495
1496 \f
1497 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1498    branch to.  */
1499
1500 static enum gimplify_status
1501 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1502 {
1503   tree switch_expr = *expr_p;
1504   gimple_seq switch_body_seq = NULL;
1505   enum gimplify_status ret;
1506   tree index_type = TREE_TYPE (switch_expr);
1507   if (index_type == NULL_TREE)
1508     index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1509
1510   ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1511                        fb_rvalue);
1512   if (ret == GS_ERROR || ret == GS_UNHANDLED)
1513     return ret;
1514
1515   if (SWITCH_BODY (switch_expr))
1516     {
1517       vec<tree> labels;
1518       vec<tree> saved_labels;
1519       tree default_case = NULL_TREE;
1520       gswitch *switch_stmt;
1521
1522       /* If someone can be bothered to fill in the labels, they can
1523          be bothered to null out the body too.  */
1524       gcc_assert (!SWITCH_LABELS (switch_expr));
1525
1526       /* Save old labels, get new ones from body, then restore the old
1527          labels.  Save all the things from the switch body to append after.  */
1528       saved_labels = gimplify_ctxp->case_labels;
1529       gimplify_ctxp->case_labels.create (8);
1530
1531       gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1532       labels = gimplify_ctxp->case_labels;
1533       gimplify_ctxp->case_labels = saved_labels;
1534
1535       preprocess_case_label_vec_for_gimple (labels, index_type,
1536                                             &default_case);
1537
1538       if (!default_case)
1539         {
1540           glabel *new_default;
1541
1542           default_case
1543             = build_case_label (NULL_TREE, NULL_TREE,
1544                                 create_artificial_label (UNKNOWN_LOCATION));
1545           new_default = gimple_build_label (CASE_LABEL (default_case));
1546           gimplify_seq_add_stmt (&switch_body_seq, new_default);
1547         }
1548
1549       switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
1550                                            default_case, labels);
1551       gimplify_seq_add_stmt (pre_p, switch_stmt);
1552       gimplify_seq_add_seq (pre_p, switch_body_seq);
1553       labels.release ();
1554     }
1555   else
1556     gcc_assert (SWITCH_LABELS (switch_expr));
1557
1558   return GS_ALL_DONE;
1559 }
1560
1561 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P.  */
1562
1563 static enum gimplify_status
1564 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1565 {
1566   struct gimplify_ctx *ctxp;
1567   glabel *label_stmt;
1568
1569   /* Invalid programs can play Duff's Device type games with, for example,
1570      #pragma omp parallel.  At least in the C front end, we don't
1571      detect such invalid branches until after gimplification, in the
1572      diagnose_omp_blocks pass.  */
1573   for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1574     if (ctxp->case_labels.exists ())
1575       break;
1576
1577   label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
1578   ctxp->case_labels.safe_push (*expr_p);
1579   gimplify_seq_add_stmt (pre_p, label_stmt);
1580
1581   return GS_ALL_DONE;
1582 }
1583
1584 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1585    if necessary.  */
1586
1587 tree
1588 build_and_jump (tree *label_p)
1589 {
1590   if (label_p == NULL)
1591     /* If there's nowhere to jump, just fall through.  */
1592     return NULL_TREE;
1593
1594   if (*label_p == NULL_TREE)
1595     {
1596       tree label = create_artificial_label (UNKNOWN_LOCATION);
1597       *label_p = label;
1598     }
1599
1600   return build1 (GOTO_EXPR, void_type_node, *label_p);
1601 }
1602
1603 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1604    This also involves building a label to jump to and communicating it to
1605    gimplify_loop_expr through gimplify_ctxp->exit_label.  */
1606
1607 static enum gimplify_status
1608 gimplify_exit_expr (tree *expr_p)
1609 {
1610   tree cond = TREE_OPERAND (*expr_p, 0);
1611   tree expr;
1612
1613   expr = build_and_jump (&gimplify_ctxp->exit_label);
1614   expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1615   *expr_p = expr;
1616
1617   return GS_OK;
1618 }
1619
1620 /* *EXPR_P is a COMPONENT_REF being used as an rvalue.  If its type is
1621    different from its canonical type, wrap the whole thing inside a
1622    NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1623    type.
1624
1625    The canonical type of a COMPONENT_REF is the type of the field being
1626    referenced--unless the field is a bit-field which can be read directly
1627    in a smaller mode, in which case the canonical type is the
1628    sign-appropriate type corresponding to that mode.  */
1629
1630 static void
1631 canonicalize_component_ref (tree *expr_p)
1632 {
1633   tree expr = *expr_p;
1634   tree type;
1635
1636   gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1637
1638   if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1639     type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1640   else
1641     type = TREE_TYPE (TREE_OPERAND (expr, 1));
1642
1643   /* One could argue that all the stuff below is not necessary for
1644      the non-bitfield case and declare it a FE error if type
1645      adjustment would be needed.  */
1646   if (TREE_TYPE (expr) != type)
1647     {
1648 #ifdef ENABLE_TYPES_CHECKING
1649       tree old_type = TREE_TYPE (expr);
1650 #endif
1651       int type_quals;
1652
1653       /* We need to preserve qualifiers and propagate them from
1654          operand 0.  */
1655       type_quals = TYPE_QUALS (type)
1656         | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1657       if (TYPE_QUALS (type) != type_quals)
1658         type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1659
1660       /* Set the type of the COMPONENT_REF to the underlying type.  */
1661       TREE_TYPE (expr) = type;
1662
1663 #ifdef ENABLE_TYPES_CHECKING
1664       /* It is now a FE error, if the conversion from the canonical
1665          type to the original expression type is not useless.  */
1666       gcc_assert (useless_type_conversion_p (old_type, type));
1667 #endif
1668     }
1669 }
1670
1671 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1672    to foo, embed that change in the ADDR_EXPR by converting
1673       T array[U];
1674       (T *)&array
1675    ==>
1676       &array[L]
1677    where L is the lower bound.  For simplicity, only do this for constant
1678    lower bound.
1679    The constraint is that the type of &array[L] is trivially convertible
1680    to T *.  */
1681
1682 static void
1683 canonicalize_addr_expr (tree *expr_p)
1684 {
1685   tree expr = *expr_p;
1686   tree addr_expr = TREE_OPERAND (expr, 0);
1687   tree datype, ddatype, pddatype;
1688
1689   /* We simplify only conversions from an ADDR_EXPR to a pointer type.  */
1690   if (!POINTER_TYPE_P (TREE_TYPE (expr))
1691       || TREE_CODE (addr_expr) != ADDR_EXPR)
1692     return;
1693
1694   /* The addr_expr type should be a pointer to an array.  */
1695   datype = TREE_TYPE (TREE_TYPE (addr_expr));
1696   if (TREE_CODE (datype) != ARRAY_TYPE)
1697     return;
1698
1699   /* The pointer to element type shall be trivially convertible to
1700      the expression pointer type.  */
1701   ddatype = TREE_TYPE (datype);
1702   pddatype = build_pointer_type (ddatype);
1703   if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1704                                   pddatype))
1705     return;
1706
1707   /* The lower bound and element sizes must be constant.  */
1708   if (!TYPE_SIZE_UNIT (ddatype)
1709       || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1710       || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1711       || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1712     return;
1713
1714   /* All checks succeeded.  Build a new node to merge the cast.  */
1715   *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1716                     TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1717                     NULL_TREE, NULL_TREE);
1718   *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1719
1720   /* We can have stripped a required restrict qualifier above.  */
1721   if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1722     *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1723 }
1724
1725 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR.  Remove it and/or other conversions
1726    underneath as appropriate.  */
1727
1728 static enum gimplify_status
1729 gimplify_conversion (tree *expr_p)
1730 {
1731   location_t loc = EXPR_LOCATION (*expr_p);
1732   gcc_assert (CONVERT_EXPR_P (*expr_p));
1733
1734   /* Then strip away all but the outermost conversion.  */
1735   STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1736
1737   /* And remove the outermost conversion if it's useless.  */
1738   if (tree_ssa_useless_type_conversion (*expr_p))
1739     *expr_p = TREE_OPERAND (*expr_p, 0);
1740
1741   /* If we still have a conversion at the toplevel,
1742      then canonicalize some constructs.  */
1743   if (CONVERT_EXPR_P (*expr_p))
1744     {
1745       tree sub = TREE_OPERAND (*expr_p, 0);
1746
1747       /* If a NOP conversion is changing the type of a COMPONENT_REF
1748          expression, then canonicalize its type now in order to expose more
1749          redundant conversions.  */
1750       if (TREE_CODE (sub) == COMPONENT_REF)
1751         canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1752
1753       /* If a NOP conversion is changing a pointer to array of foo
1754          to a pointer to foo, embed that change in the ADDR_EXPR.  */
1755       else if (TREE_CODE (sub) == ADDR_EXPR)
1756         canonicalize_addr_expr (expr_p);
1757     }
1758
1759   /* If we have a conversion to a non-register type force the
1760      use of a VIEW_CONVERT_EXPR instead.  */
1761   if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1762     *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1763                                TREE_OPERAND (*expr_p, 0));
1764
1765   /* Canonicalize CONVERT_EXPR to NOP_EXPR.  */
1766   if (TREE_CODE (*expr_p) == CONVERT_EXPR)
1767     TREE_SET_CODE (*expr_p, NOP_EXPR);
1768
1769   return GS_OK;
1770 }
1771
1772 /* Nonlocal VLAs seen in the current function.  */
1773 static hash_set<tree> *nonlocal_vlas;
1774
1775 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes.  */
1776 static tree nonlocal_vla_vars;
1777
1778 /* Gimplify a VAR_DECL or PARM_DECL.  Return GS_OK if we expanded a
1779    DECL_VALUE_EXPR, and it's worth re-examining things.  */
1780
1781 static enum gimplify_status
1782 gimplify_var_or_parm_decl (tree *expr_p)
1783 {
1784   tree decl = *expr_p;
1785
1786   /* ??? If this is a local variable, and it has not been seen in any
1787      outer BIND_EXPR, then it's probably the result of a duplicate
1788      declaration, for which we've already issued an error.  It would
1789      be really nice if the front end wouldn't leak these at all.
1790      Currently the only known culprit is C++ destructors, as seen
1791      in g++.old-deja/g++.jason/binding.C.  */
1792   if (TREE_CODE (decl) == VAR_DECL
1793       && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1794       && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1795       && decl_function_context (decl) == current_function_decl)
1796     {
1797       gcc_assert (seen_error ());
1798       return GS_ERROR;
1799     }
1800
1801   /* When within an OMP context, notice uses of variables.  */
1802   if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1803     return GS_ALL_DONE;
1804
1805   /* If the decl is an alias for another expression, substitute it now.  */
1806   if (DECL_HAS_VALUE_EXPR_P (decl))
1807     {
1808       tree value_expr = DECL_VALUE_EXPR (decl);
1809
1810       /* For referenced nonlocal VLAs add a decl for debugging purposes
1811          to the current function.  */
1812       if (TREE_CODE (decl) == VAR_DECL
1813           && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1814           && nonlocal_vlas != NULL
1815           && TREE_CODE (value_expr) == INDIRECT_REF
1816           && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1817           && decl_function_context (decl) != current_function_decl)
1818         {
1819           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1820           while (ctx
1821                  && (ctx->region_type == ORT_WORKSHARE
1822                      || ctx->region_type == ORT_SIMD))
1823             ctx = ctx->outer_context;
1824           if (!ctx && !nonlocal_vlas->add (decl))
1825             {
1826               tree copy = copy_node (decl);
1827
1828               lang_hooks.dup_lang_specific_decl (copy);
1829               SET_DECL_RTL (copy, 0);
1830               TREE_USED (copy) = 1;
1831               DECL_CHAIN (copy) = nonlocal_vla_vars;
1832               nonlocal_vla_vars = copy;
1833               SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1834               DECL_HAS_VALUE_EXPR_P (copy) = 1;
1835             }
1836         }
1837
1838       *expr_p = unshare_expr (value_expr);
1839       return GS_OK;
1840     }
1841
1842   return GS_ALL_DONE;
1843 }
1844
1845 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T.  */
1846
1847 static void
1848 recalculate_side_effects (tree t)
1849 {
1850   enum tree_code code = TREE_CODE (t);
1851   int len = TREE_OPERAND_LENGTH (t);
1852   int i;
1853
1854   switch (TREE_CODE_CLASS (code))
1855     {
1856     case tcc_expression:
1857       switch (code)
1858         {
1859         case INIT_EXPR:
1860         case MODIFY_EXPR:
1861         case VA_ARG_EXPR:
1862         case PREDECREMENT_EXPR:
1863         case PREINCREMENT_EXPR:
1864         case POSTDECREMENT_EXPR:
1865         case POSTINCREMENT_EXPR:
1866           /* All of these have side-effects, no matter what their
1867              operands are.  */
1868           return;
1869
1870         default:
1871           break;
1872         }
1873       /* Fall through.  */
1874
1875     case tcc_comparison:  /* a comparison expression */
1876     case tcc_unary:       /* a unary arithmetic expression */
1877     case tcc_binary:      /* a binary arithmetic expression */
1878     case tcc_reference:   /* a reference */
1879     case tcc_vl_exp:        /* a function call */
1880       TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1881       for (i = 0; i < len; ++i)
1882         {
1883           tree op = TREE_OPERAND (t, i);
1884           if (op && TREE_SIDE_EFFECTS (op))
1885             TREE_SIDE_EFFECTS (t) = 1;
1886         }
1887       break;
1888
1889     case tcc_constant:
1890       /* No side-effects.  */
1891       return;
1892
1893     default:
1894       gcc_unreachable ();
1895    }
1896 }
1897
1898 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1899    node *EXPR_P.
1900
1901       compound_lval
1902               : min_lval '[' val ']'
1903               | min_lval '.' ID
1904               | compound_lval '[' val ']'
1905               | compound_lval '.' ID
1906
1907    This is not part of the original SIMPLE definition, which separates
1908    array and member references, but it seems reasonable to handle them
1909    together.  Also, this way we don't run into problems with union
1910    aliasing; gcc requires that for accesses through a union to alias, the
1911    union reference must be explicit, which was not always the case when we
1912    were splitting up array and member refs.
1913
1914    PRE_P points to the sequence where side effects that must happen before
1915      *EXPR_P should be stored.
1916
1917    POST_P points to the sequence where side effects that must happen after
1918      *EXPR_P should be stored.  */
1919
1920 static enum gimplify_status
1921 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1922                         fallback_t fallback)
1923 {
1924   tree *p;
1925   enum gimplify_status ret = GS_ALL_DONE, tret;
1926   int i;
1927   location_t loc = EXPR_LOCATION (*expr_p);
1928   tree expr = *expr_p;
1929
1930   /* Create a stack of the subexpressions so later we can walk them in
1931      order from inner to outer.  */
1932   auto_vec<tree, 10> expr_stack;
1933
1934   /* We can handle anything that get_inner_reference can deal with.  */
1935   for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1936     {
1937     restart:
1938       /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs.  */
1939       if (TREE_CODE (*p) == INDIRECT_REF)
1940         *p = fold_indirect_ref_loc (loc, *p);
1941
1942       if (handled_component_p (*p))
1943         ;
1944       /* Expand DECL_VALUE_EXPR now.  In some cases that may expose
1945          additional COMPONENT_REFs.  */
1946       else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1947                && gimplify_var_or_parm_decl (p) == GS_OK)
1948         goto restart;
1949       else
1950         break;
1951
1952       expr_stack.safe_push (*p);
1953     }
1954
1955   gcc_assert (expr_stack.length ());
1956
1957   /* Now EXPR_STACK is a stack of pointers to all the refs we've
1958      walked through and P points to the innermost expression.
1959
1960      Java requires that we elaborated nodes in source order.  That
1961      means we must gimplify the inner expression followed by each of
1962      the indices, in order.  But we can't gimplify the inner
1963      expression until we deal with any variable bounds, sizes, or
1964      positions in order to deal with PLACEHOLDER_EXPRs.
1965
1966      So we do this in three steps.  First we deal with the annotations
1967      for any variables in the components, then we gimplify the base,
1968      then we gimplify any indices, from left to right.  */
1969   for (i = expr_stack.length () - 1; i >= 0; i--)
1970     {
1971       tree t = expr_stack[i];
1972
1973       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1974         {
1975           /* Gimplify the low bound and element type size and put them into
1976              the ARRAY_REF.  If these values are set, they have already been
1977              gimplified.  */
1978           if (TREE_OPERAND (t, 2) == NULL_TREE)
1979             {
1980               tree low = unshare_expr (array_ref_low_bound (t));
1981               if (!is_gimple_min_invariant (low))
1982                 {
1983                   TREE_OPERAND (t, 2) = low;
1984                   tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1985                                         post_p, is_gimple_reg,
1986                                         fb_rvalue);
1987                   ret = MIN (ret, tret);
1988                 }
1989             }
1990           else
1991             {
1992               tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1993                                     is_gimple_reg, fb_rvalue);
1994               ret = MIN (ret, tret);
1995             }
1996
1997           if (TREE_OPERAND (t, 3) == NULL_TREE)
1998             {
1999               tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2000               tree elmt_size = unshare_expr (array_ref_element_size (t));
2001               tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2002
2003               /* Divide the element size by the alignment of the element
2004                  type (above).  */
2005               elmt_size
2006                 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2007
2008               if (!is_gimple_min_invariant (elmt_size))
2009                 {
2010                   TREE_OPERAND (t, 3) = elmt_size;
2011                   tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2012                                         post_p, is_gimple_reg,
2013                                         fb_rvalue);
2014                   ret = MIN (ret, tret);
2015                 }
2016             }
2017           else
2018             {
2019               tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2020                                     is_gimple_reg, fb_rvalue);
2021               ret = MIN (ret, tret);
2022             }
2023         }
2024       else if (TREE_CODE (t) == COMPONENT_REF)
2025         {
2026           /* Set the field offset into T and gimplify it.  */
2027           if (TREE_OPERAND (t, 2) == NULL_TREE)
2028             {
2029               tree offset = unshare_expr (component_ref_field_offset (t));
2030               tree field = TREE_OPERAND (t, 1);
2031               tree factor
2032                 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2033
2034               /* Divide the offset by its alignment.  */
2035               offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2036
2037               if (!is_gimple_min_invariant (offset))
2038                 {
2039                   TREE_OPERAND (t, 2) = offset;
2040                   tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2041                                         post_p, is_gimple_reg,
2042                                         fb_rvalue);
2043                   ret = MIN (ret, tret);
2044                 }
2045             }
2046           else
2047             {
2048               tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2049                                     is_gimple_reg, fb_rvalue);
2050               ret = MIN (ret, tret);
2051             }
2052         }
2053     }
2054
2055   /* Step 2 is to gimplify the base expression.  Make sure lvalue is set
2056      so as to match the min_lval predicate.  Failure to do so may result
2057      in the creation of large aggregate temporaries.  */
2058   tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2059                         fallback | fb_lvalue);
2060   ret = MIN (ret, tret);
2061
2062   /* And finally, the indices and operands of ARRAY_REF.  During this
2063      loop we also remove any useless conversions.  */
2064   for (; expr_stack.length () > 0; )
2065     {
2066       tree t = expr_stack.pop ();
2067
2068       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2069         {
2070           /* Gimplify the dimension.  */
2071           if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2072             {
2073               tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2074                                     is_gimple_val, fb_rvalue);
2075               ret = MIN (ret, tret);
2076             }
2077         }
2078
2079       STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2080
2081       /* The innermost expression P may have originally had
2082          TREE_SIDE_EFFECTS set which would have caused all the outer
2083          expressions in *EXPR_P leading to P to also have had
2084          TREE_SIDE_EFFECTS set.  */
2085       recalculate_side_effects (t);
2086     }
2087
2088   /* If the outermost expression is a COMPONENT_REF, canonicalize its type.  */
2089   if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2090     {
2091       canonicalize_component_ref (expr_p);
2092     }
2093
2094   expr_stack.release ();
2095
2096   gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2097
2098   return ret;
2099 }
2100
2101 /*  Gimplify the self modifying expression pointed to by EXPR_P
2102     (++, --, +=, -=).
2103
2104     PRE_P points to the list where side effects that must happen before
2105         *EXPR_P should be stored.
2106
2107     POST_P points to the list where side effects that must happen after
2108         *EXPR_P should be stored.
2109
2110     WANT_VALUE is nonzero iff we want to use the value of this expression
2111         in another expression.
2112
2113     ARITH_TYPE is the type the computation should be performed in.  */
2114
2115 enum gimplify_status
2116 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2117                         bool want_value, tree arith_type)
2118 {
2119   enum tree_code code;
2120   tree lhs, lvalue, rhs, t1;
2121   gimple_seq post = NULL, *orig_post_p = post_p;
2122   bool postfix;
2123   enum tree_code arith_code;
2124   enum gimplify_status ret;
2125   location_t loc = EXPR_LOCATION (*expr_p);
2126
2127   code = TREE_CODE (*expr_p);
2128
2129   gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2130               || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2131
2132   /* Prefix or postfix?  */
2133   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2134     /* Faster to treat as prefix if result is not used.  */
2135     postfix = want_value;
2136   else
2137     postfix = false;
2138
2139   /* For postfix, make sure the inner expression's post side effects
2140      are executed after side effects from this expression.  */
2141   if (postfix)
2142     post_p = &post;
2143
2144   /* Add or subtract?  */
2145   if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2146     arith_code = PLUS_EXPR;
2147   else
2148     arith_code = MINUS_EXPR;
2149
2150   /* Gimplify the LHS into a GIMPLE lvalue.  */
2151   lvalue = TREE_OPERAND (*expr_p, 0);
2152   ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2153   if (ret == GS_ERROR)
2154     return ret;
2155
2156   /* Extract the operands to the arithmetic operation.  */
2157   lhs = lvalue;
2158   rhs = TREE_OPERAND (*expr_p, 1);
2159
2160   /* For postfix operator, we evaluate the LHS to an rvalue and then use
2161      that as the result value and in the postqueue operation.  */
2162   if (postfix)
2163     {
2164       ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2165       if (ret == GS_ERROR)
2166         return ret;
2167
2168       lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2169     }
2170
2171   /* For POINTERs increment, use POINTER_PLUS_EXPR.  */
2172   if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2173     {
2174       rhs = convert_to_ptrofftype_loc (loc, rhs);
2175       if (arith_code == MINUS_EXPR)
2176         rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2177       t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2178     }
2179   else
2180     t1 = fold_convert (TREE_TYPE (*expr_p),
2181                        fold_build2 (arith_code, arith_type,
2182                                     fold_convert (arith_type, lhs),
2183                                     fold_convert (arith_type, rhs)));
2184
2185   if (postfix)
2186     {
2187       gimplify_assign (lvalue, t1, pre_p);
2188       gimplify_seq_add_seq (orig_post_p, post);
2189       *expr_p = lhs;
2190       return GS_ALL_DONE;
2191     }
2192   else
2193     {
2194       *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2195       return GS_OK;
2196     }
2197 }
2198
2199 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR.  */
2200
2201 static void
2202 maybe_with_size_expr (tree *expr_p)
2203 {
2204   tree expr = *expr_p;
2205   tree type = TREE_TYPE (expr);
2206   tree size;
2207
2208   /* If we've already wrapped this or the type is error_mark_node, we can't do
2209      anything.  */
2210   if (TREE_CODE (expr) == WITH_SIZE_EXPR
2211       || type == error_mark_node)
2212     return;
2213
2214   /* If the size isn't known or is a constant, we have nothing to do.  */
2215   size = TYPE_SIZE_UNIT (type);
2216   if (!size || TREE_CODE (size) == INTEGER_CST)
2217     return;
2218
2219   /* Otherwise, make a WITH_SIZE_EXPR.  */
2220   size = unshare_expr (size);
2221   size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2222   *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2223 }
2224
2225 /* Helper for gimplify_call_expr.  Gimplify a single argument *ARG_P
2226    Store any side-effects in PRE_P.  CALL_LOCATION is the location of
2227    the CALL_EXPR.  */
2228
2229 enum gimplify_status
2230 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2231 {
2232   bool (*test) (tree);
2233   fallback_t fb;
2234
2235   /* In general, we allow lvalues for function arguments to avoid
2236      extra overhead of copying large aggregates out of even larger
2237      aggregates into temporaries only to copy the temporaries to
2238      the argument list.  Make optimizers happy by pulling out to
2239      temporaries those types that fit in registers.  */
2240   if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2241     test = is_gimple_val, fb = fb_rvalue;
2242   else
2243     {
2244       test = is_gimple_lvalue, fb = fb_either;
2245       /* Also strip a TARGET_EXPR that would force an extra copy.  */
2246       if (TREE_CODE (*arg_p) == TARGET_EXPR)
2247         {
2248           tree init = TARGET_EXPR_INITIAL (*arg_p);
2249           if (init
2250               && !VOID_TYPE_P (TREE_TYPE (init)))
2251             *arg_p = init;
2252         }
2253     }
2254
2255   /* If this is a variable sized type, we must remember the size.  */
2256   maybe_with_size_expr (arg_p);
2257
2258   /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c.  */
2259   /* Make sure arguments have the same location as the function call
2260      itself.  */
2261   protected_set_expr_location (*arg_p, call_location);
2262
2263   /* There is a sequence point before a function call.  Side effects in
2264      the argument list must occur before the actual call. So, when
2265      gimplifying arguments, force gimplify_expr to use an internal
2266      post queue which is then appended to the end of PRE_P.  */
2267   return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2268 }
2269
2270 /* Don't fold inside offloading regions: it can break code by adding decl
2271    references that weren't in the source.  We'll do it during omplower pass
2272    instead.  */
2273
2274 static bool
2275 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2276 {
2277   struct gimplify_omp_ctx *ctx;
2278   for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2279     if (ctx->region_type == ORT_TARGET)
2280       return false;
2281   return fold_stmt (gsi);
2282 }
2283
2284 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2285    WANT_VALUE is true if the result of the call is desired.  */
2286
2287 static enum gimplify_status
2288 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2289 {
2290   tree fndecl, parms, p, fnptrtype;
2291   enum gimplify_status ret;
2292   int i, nargs;
2293   gcall *call;
2294   bool builtin_va_start_p = false;
2295   location_t loc = EXPR_LOCATION (*expr_p);
2296
2297   gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2298
2299   /* For reliable diagnostics during inlining, it is necessary that
2300      every call_expr be annotated with file and line.  */
2301   if (! EXPR_HAS_LOCATION (*expr_p))
2302     SET_EXPR_LOCATION (*expr_p, input_location);
2303
2304   /* Gimplify internal functions created in the FEs.  */
2305   if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2306     {
2307       if (want_value)
2308         return GS_ALL_DONE;
2309
2310       nargs = call_expr_nargs (*expr_p);
2311       enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2312       auto_vec<tree> vargs (nargs);
2313
2314       for (i = 0; i < nargs; i++)
2315         {
2316           gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2317                         EXPR_LOCATION (*expr_p));
2318           vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2319         }
2320       gimple call = gimple_build_call_internal_vec (ifn, vargs);
2321       gimplify_seq_add_stmt (pre_p, call);
2322       return GS_ALL_DONE;
2323     }
2324
2325   /* This may be a call to a builtin function.
2326
2327      Builtin function calls may be transformed into different
2328      (and more efficient) builtin function calls under certain
2329      circumstances.  Unfortunately, gimplification can muck things
2330      up enough that the builtin expanders are not aware that certain
2331      transformations are still valid.
2332
2333      So we attempt transformation/gimplification of the call before
2334      we gimplify the CALL_EXPR.  At this time we do not manage to
2335      transform all calls in the same manner as the expanders do, but
2336      we do transform most of them.  */
2337   fndecl = get_callee_fndecl (*expr_p);
2338   if (fndecl
2339       && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2340     switch (DECL_FUNCTION_CODE (fndecl))
2341       {
2342       case BUILT_IN_VA_START:
2343         {
2344           builtin_va_start_p = TRUE;
2345           if (call_expr_nargs (*expr_p) < 2)
2346             {
2347               error ("too few arguments to function %<va_start%>");
2348               *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2349               return GS_OK;
2350             }
2351
2352           if (fold_builtin_next_arg (*expr_p, true))
2353             {
2354               *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2355               return GS_OK;
2356             }
2357           break;
2358         }
2359       case BUILT_IN_LINE:
2360         {
2361           *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2362                                    LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2363           return GS_OK;
2364         }
2365       case BUILT_IN_FILE:
2366         {
2367           const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2368           *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2369           return GS_OK;
2370         }
2371       case BUILT_IN_FUNCTION:
2372         {
2373           const char *function;
2374           function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2375           *expr_p = build_string_literal (strlen (function) + 1, function);
2376           return GS_OK;
2377         }
2378       default:
2379         ;
2380       }
2381   if (fndecl && DECL_BUILT_IN (fndecl))
2382     {
2383       tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2384       if (new_tree && new_tree != *expr_p)
2385         {
2386           /* There was a transformation of this call which computes the
2387              same value, but in a more efficient way.  Return and try
2388              again.  */
2389           *expr_p = new_tree;
2390           return GS_OK;
2391         }
2392     }
2393
2394   /* Remember the original function pointer type.  */
2395   fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2396
2397   /* There is a sequence point before the call, so any side effects in
2398      the calling expression must occur before the actual call.  Force
2399      gimplify_expr to use an internal post queue.  */
2400   ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2401                        is_gimple_call_addr, fb_rvalue);
2402
2403   nargs = call_expr_nargs (*expr_p);
2404
2405   /* Get argument types for verification.  */
2406   fndecl = get_callee_fndecl (*expr_p);
2407   parms = NULL_TREE;
2408   if (fndecl)
2409     parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2410   else
2411     parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2412
2413   if (fndecl && DECL_ARGUMENTS (fndecl))
2414     p = DECL_ARGUMENTS (fndecl);
2415   else if (parms)
2416     p = parms;
2417   else
2418     p = NULL_TREE;
2419   for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2420     ;
2421
2422   /* If the last argument is __builtin_va_arg_pack () and it is not
2423      passed as a named argument, decrease the number of CALL_EXPR
2424      arguments and set instead the CALL_EXPR_VA_ARG_PACK flag.  */
2425   if (!p
2426       && i < nargs
2427       && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2428     {
2429       tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2430       tree last_arg_fndecl = get_callee_fndecl (last_arg);
2431
2432       if (last_arg_fndecl
2433           && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2434           && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2435           && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2436         {
2437           tree call = *expr_p;
2438
2439           --nargs;
2440           *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2441                                           CALL_EXPR_FN (call),
2442                                           nargs, CALL_EXPR_ARGP (call));
2443
2444           /* Copy all CALL_EXPR flags, location and block, except
2445              CALL_EXPR_VA_ARG_PACK flag.  */
2446           CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2447           CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2448           CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2449             = CALL_EXPR_RETURN_SLOT_OPT (call);
2450           CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2451           SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2452
2453           /* Set CALL_EXPR_VA_ARG_PACK.  */
2454           CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2455         }
2456     }
2457
2458   /* Gimplify the function arguments.  */
2459   if (nargs > 0)
2460     {
2461       for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2462            PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2463            PUSH_ARGS_REVERSED ? i-- : i++)
2464         {
2465           enum gimplify_status t;
2466
2467           /* Avoid gimplifying the second argument to va_start, which needs to
2468              be the plain PARM_DECL.  */
2469           if ((i != 1) || !builtin_va_start_p)
2470             {
2471               t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2472                                 EXPR_LOCATION (*expr_p));
2473
2474               if (t == GS_ERROR)
2475                 ret = GS_ERROR;
2476             }
2477         }
2478     }
2479
2480   /* Gimplify the static chain.  */
2481   if (CALL_EXPR_STATIC_CHAIN (*expr_p))
2482     {
2483       if (fndecl && !DECL_STATIC_CHAIN (fndecl))
2484         CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
2485       else
2486         {
2487           enum gimplify_status t;
2488           t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
2489                             EXPR_LOCATION (*expr_p));
2490           if (t == GS_ERROR)
2491             ret = GS_ERROR;
2492         }
2493     }
2494
2495   /* Verify the function result.  */
2496   if (want_value && fndecl
2497       && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2498     {
2499       error_at (loc, "using result of function returning %<void%>");
2500       ret = GS_ERROR;
2501     }
2502
2503   /* Try this again in case gimplification exposed something.  */
2504   if (ret != GS_ERROR)
2505     {
2506       tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2507
2508       if (new_tree && new_tree != *expr_p)
2509         {
2510           /* There was a transformation of this call which computes the
2511              same value, but in a more efficient way.  Return and try
2512              again.  */
2513           *expr_p = new_tree;
2514           return GS_OK;
2515         }
2516     }
2517   else
2518     {
2519       *expr_p = error_mark_node;
2520       return GS_ERROR;
2521     }
2522
2523   /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2524      decl.  This allows us to eliminate redundant or useless
2525      calls to "const" functions.  */
2526   if (TREE_CODE (*expr_p) == CALL_EXPR)
2527     {
2528       int flags = call_expr_flags (*expr_p);
2529       if (flags & (ECF_CONST | ECF_PURE)
2530           /* An infinite loop is considered a side effect.  */
2531           && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2532         TREE_SIDE_EFFECTS (*expr_p) = 0;
2533     }
2534
2535   /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2536      and clear *EXPR_P.  Otherwise, leave *EXPR_P in its gimplified
2537      form and delegate the creation of a GIMPLE_CALL to
2538      gimplify_modify_expr.  This is always possible because when
2539      WANT_VALUE is true, the caller wants the result of this call into
2540      a temporary, which means that we will emit an INIT_EXPR in
2541      internal_get_tmp_var which will then be handled by
2542      gimplify_modify_expr.  */
2543   if (!want_value)
2544     {
2545       /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2546          have to do is replicate it as a GIMPLE_CALL tuple.  */
2547       gimple_stmt_iterator gsi;
2548       call = gimple_build_call_from_tree (*expr_p);
2549       gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2550       notice_special_calls (call);
2551       gimplify_seq_add_stmt (pre_p, call);
2552       gsi = gsi_last (*pre_p);
2553       maybe_fold_stmt (&gsi);
2554       *expr_p = NULL_TREE;
2555     }
2556   else
2557     /* Remember the original function type.  */
2558     CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2559                                      CALL_EXPR_FN (*expr_p));
2560
2561   return ret;
2562 }
2563
2564 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2565    rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2566
2567    TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2568    condition is true or false, respectively.  If null, we should generate
2569    our own to skip over the evaluation of this specific expression.
2570
2571    LOCUS is the source location of the COND_EXPR.
2572
2573    This function is the tree equivalent of do_jump.
2574
2575    shortcut_cond_r should only be called by shortcut_cond_expr.  */
2576
2577 static tree
2578 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2579                  location_t locus)
2580 {
2581   tree local_label = NULL_TREE;
2582   tree t, expr = NULL;
2583
2584   /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2585      retain the shortcut semantics.  Just insert the gotos here;
2586      shortcut_cond_expr will append the real blocks later.  */
2587   if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2588     {
2589       location_t new_locus;
2590
2591       /* Turn if (a && b) into
2592
2593          if (a); else goto no;
2594          if (b) goto yes; else goto no;
2595          (no:) */
2596
2597       if (false_label_p == NULL)
2598         false_label_p = &local_label;
2599
2600       /* Keep the original source location on the first 'if'.  */
2601       t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2602       append_to_statement_list (t, &expr);
2603
2604       /* Set the source location of the && on the second 'if'.  */
2605       new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2606       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2607                            new_locus);
2608       append_to_statement_list (t, &expr);
2609     }
2610   else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2611     {
2612       location_t new_locus;
2613
2614       /* Turn if (a || b) into
2615
2616          if (a) goto yes;
2617          if (b) goto yes; else goto no;
2618          (yes:) */
2619
2620       if (true_label_p == NULL)
2621         true_label_p = &local_label;
2622
2623       /* Keep the original source location on the first 'if'.  */
2624       t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2625       append_to_statement_list (t, &expr);
2626
2627       /* Set the source location of the || on the second 'if'.  */
2628       new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2629       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2630                            new_locus);
2631       append_to_statement_list (t, &expr);
2632     }
2633   else if (TREE_CODE (pred) == COND_EXPR
2634            && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2635            && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2636     {
2637       location_t new_locus;
2638
2639       /* As long as we're messing with gotos, turn if (a ? b : c) into
2640          if (a)
2641            if (b) goto yes; else goto no;
2642          else
2643            if (c) goto yes; else goto no;
2644
2645          Don't do this if one of the arms has void type, which can happen
2646          in C++ when the arm is throw.  */
2647
2648       /* Keep the original source location on the first 'if'.  Set the source
2649          location of the ? on the second 'if'.  */
2650       new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2651       expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2652                      shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2653                                       false_label_p, locus),
2654                      shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2655                                       false_label_p, new_locus));
2656     }
2657   else
2658     {
2659       expr = build3 (COND_EXPR, void_type_node, pred,
2660                      build_and_jump (true_label_p),
2661                      build_and_jump (false_label_p));
2662       SET_EXPR_LOCATION (expr, locus);
2663     }
2664
2665   if (local_label)
2666     {
2667       t = build1 (LABEL_EXPR, void_type_node, local_label);
2668       append_to_statement_list (t, &expr);
2669     }
2670
2671   return expr;
2672 }
2673
2674 /* Given a conditional expression EXPR with short-circuit boolean
2675    predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2676    predicate apart into the equivalent sequence of conditionals.  */
2677
2678 static tree
2679 shortcut_cond_expr (tree expr)
2680 {
2681   tree pred = TREE_OPERAND (expr, 0);
2682   tree then_ = TREE_OPERAND (expr, 1);
2683   tree else_ = TREE_OPERAND (expr, 2);
2684   tree true_label, false_label, end_label, t;
2685   tree *true_label_p;
2686   tree *false_label_p;
2687   bool emit_end, emit_false, jump_over_else;
2688   bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2689   bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2690
2691   /* First do simple transformations.  */
2692   if (!else_se)
2693     {
2694       /* If there is no 'else', turn
2695            if (a && b) then c
2696          into
2697            if (a) if (b) then c.  */
2698       while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2699         {
2700           /* Keep the original source location on the first 'if'.  */
2701           location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2702           TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2703           /* Set the source location of the && on the second 'if'.  */
2704           if (EXPR_HAS_LOCATION (pred))
2705             SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2706           then_ = shortcut_cond_expr (expr);
2707           then_se = then_ && TREE_SIDE_EFFECTS (then_);
2708           pred = TREE_OPERAND (pred, 0);
2709           expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2710           SET_EXPR_LOCATION (expr, locus);
2711         }
2712     }
2713
2714   if (!then_se)
2715     {
2716       /* If there is no 'then', turn
2717            if (a || b); else d
2718          into
2719            if (a); else if (b); else d.  */
2720       while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2721         {
2722           /* Keep the original source location on the first 'if'.  */
2723           location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2724           TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2725           /* Set the source location of the || on the second 'if'.  */
2726           if (EXPR_HAS_LOCATION (pred))
2727             SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2728           else_ = shortcut_cond_expr (expr);
2729           else_se = else_ && TREE_SIDE_EFFECTS (else_);
2730           pred = TREE_OPERAND (pred, 0);
2731           expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2732           SET_EXPR_LOCATION (expr, locus);
2733         }
2734     }
2735
2736   /* If we're done, great.  */
2737   if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2738       && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2739     return expr;
2740
2741   /* Otherwise we need to mess with gotos.  Change
2742        if (a) c; else d;
2743      to
2744        if (a); else goto no;
2745        c; goto end;
2746        no: d; end:
2747      and recursively gimplify the condition.  */
2748
2749   true_label = false_label = end_label = NULL_TREE;
2750
2751   /* If our arms just jump somewhere, hijack those labels so we don't
2752      generate jumps to jumps.  */
2753
2754   if (then_
2755       && TREE_CODE (then_) == GOTO_EXPR
2756       && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2757     {
2758       true_label = GOTO_DESTINATION (then_);
2759       then_ = NULL;
2760       then_se = false;
2761     }
2762
2763   if (else_
2764       && TREE_CODE (else_) == GOTO_EXPR
2765       && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2766     {
2767       false_label = GOTO_DESTINATION (else_);
2768       else_ = NULL;
2769       else_se = false;
2770     }
2771
2772   /* If we aren't hijacking a label for the 'then' branch, it falls through.  */
2773   if (true_label)
2774     true_label_p = &true_label;
2775   else
2776     true_label_p = NULL;
2777
2778   /* The 'else' branch also needs a label if it contains interesting code.  */
2779   if (false_label || else_se)
2780     false_label_p = &false_label;
2781   else
2782     false_label_p = NULL;
2783
2784   /* If there was nothing else in our arms, just forward the label(s).  */
2785   if (!then_se && !else_se)
2786     return shortcut_cond_r (pred, true_label_p, false_label_p,
2787                             EXPR_LOC_OR_LOC (expr, input_location));
2788
2789   /* If our last subexpression already has a terminal label, reuse it.  */
2790   if (else_se)
2791     t = expr_last (else_);
2792   else if (then_se)
2793     t = expr_last (then_);
2794   else
2795     t = NULL;
2796   if (t && TREE_CODE (t) == LABEL_EXPR)
2797     end_label = LABEL_EXPR_LABEL (t);
2798
2799   /* If we don't care about jumping to the 'else' branch, jump to the end
2800      if the condition is false.  */
2801   if (!false_label_p)
2802     false_label_p = &end_label;
2803
2804   /* We only want to emit these labels if we aren't hijacking them.  */
2805   emit_end = (end_label == NULL_TREE);
2806   emit_false = (false_label == NULL_TREE);
2807
2808   /* We only emit the jump over the else clause if we have to--if the
2809      then clause may fall through.  Otherwise we can wind up with a
2810      useless jump and a useless label at the end of gimplified code,
2811      which will cause us to think that this conditional as a whole
2812      falls through even if it doesn't.  If we then inline a function
2813      which ends with such a condition, that can cause us to issue an
2814      inappropriate warning about control reaching the end of a
2815      non-void function.  */
2816   jump_over_else = block_may_fallthru (then_);
2817
2818   pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2819                           EXPR_LOC_OR_LOC (expr, input_location));
2820
2821   expr = NULL;
2822   append_to_statement_list (pred, &expr);
2823
2824   append_to_statement_list (then_, &expr);
2825   if (else_se)
2826     {
2827       if (jump_over_else)
2828         {
2829           tree last = expr_last (expr);
2830           t = build_and_jump (&end_label);
2831           if (EXPR_HAS_LOCATION (last))
2832             SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2833           append_to_statement_list (t, &expr);
2834         }
2835       if (emit_false)
2836         {
2837           t = build1 (LABEL_EXPR, void_type_node, false_label);
2838           append_to_statement_list (t, &expr);
2839         }
2840       append_to_statement_list (else_, &expr);
2841     }
2842   if (emit_end && end_label)
2843     {
2844       t = build1 (LABEL_EXPR, void_type_node, end_label);
2845       append_to_statement_list (t, &expr);
2846     }
2847
2848   return expr;
2849 }
2850
2851 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE.  */
2852
2853 tree
2854 gimple_boolify (tree expr)
2855 {
2856   tree type = TREE_TYPE (expr);
2857   location_t loc = EXPR_LOCATION (expr);
2858
2859   if (TREE_CODE (expr) == NE_EXPR
2860       && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2861       && integer_zerop (TREE_OPERAND (expr, 1)))
2862     {
2863       tree call = TREE_OPERAND (expr, 0);
2864       tree fn = get_callee_fndecl (call);
2865
2866       /* For __builtin_expect ((long) (x), y) recurse into x as well
2867          if x is truth_value_p.  */
2868       if (fn
2869           && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2870           && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2871           && call_expr_nargs (call) == 2)
2872         {
2873           tree arg = CALL_EXPR_ARG (call, 0);
2874           if (arg)
2875             {
2876               if (TREE_CODE (arg) == NOP_EXPR
2877                   && TREE_TYPE (arg) == TREE_TYPE (call))
2878                 arg = TREE_OPERAND (arg, 0);
2879               if (truth_value_p (TREE_CODE (arg)))
2880                 {
2881                   arg = gimple_boolify (arg);
2882                   CALL_EXPR_ARG (call, 0)
2883                     = fold_convert_loc (loc, TREE_TYPE (call), arg);
2884                 }
2885             }
2886         }
2887     }
2888
2889   switch (TREE_CODE (expr))
2890     {
2891     case TRUTH_AND_EXPR:
2892     case TRUTH_OR_EXPR:
2893     case TRUTH_XOR_EXPR:
2894     case TRUTH_ANDIF_EXPR:
2895     case TRUTH_ORIF_EXPR:
2896       /* Also boolify the arguments of truth exprs.  */
2897       TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2898       /* FALLTHRU */
2899
2900     case TRUTH_NOT_EXPR:
2901       TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2902
2903       /* These expressions always produce boolean results.  */
2904       if (TREE_CODE (type) != BOOLEAN_TYPE)
2905         TREE_TYPE (expr) = boolean_type_node;
2906       return expr;
2907
2908     case ANNOTATE_EXPR:
2909       switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2910         {
2911         case annot_expr_ivdep_kind:
2912         case annot_expr_no_vector_kind:
2913         case annot_expr_vector_kind:
2914           TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2915           if (TREE_CODE (type) != BOOLEAN_TYPE)
2916             TREE_TYPE (expr) = boolean_type_node;
2917           return expr;
2918         default:
2919           gcc_unreachable ();
2920         }
2921
2922     default:
2923       if (COMPARISON_CLASS_P (expr))
2924         {
2925           /* There expressions always prduce boolean results.  */
2926           if (TREE_CODE (type) != BOOLEAN_TYPE)
2927             TREE_TYPE (expr) = boolean_type_node;
2928           return expr;
2929         }
2930       /* Other expressions that get here must have boolean values, but
2931          might need to be converted to the appropriate mode.  */
2932       if (TREE_CODE (type) == BOOLEAN_TYPE)
2933         return expr;
2934       return fold_convert_loc (loc, boolean_type_node, expr);
2935     }
2936 }
2937
2938 /* Given a conditional expression *EXPR_P without side effects, gimplify
2939    its operands.  New statements are inserted to PRE_P.  */
2940
2941 static enum gimplify_status
2942 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2943 {
2944   tree expr = *expr_p, cond;
2945   enum gimplify_status ret, tret;
2946   enum tree_code code;
2947
2948   cond = gimple_boolify (COND_EXPR_COND (expr));
2949
2950   /* We need to handle && and || specially, as their gimplification
2951      creates pure cond_expr, thus leading to an infinite cycle otherwise.  */
2952   code = TREE_CODE (cond);
2953   if (code == TRUTH_ANDIF_EXPR)
2954     TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2955   else if (code == TRUTH_ORIF_EXPR)
2956     TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2957   ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2958   COND_EXPR_COND (*expr_p) = cond;
2959
2960   tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2961                                    is_gimple_val, fb_rvalue);
2962   ret = MIN (ret, tret);
2963   tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2964                                    is_gimple_val, fb_rvalue);
2965
2966   return MIN (ret, tret);
2967 }
2968
2969 /* Return true if evaluating EXPR could trap.
2970    EXPR is GENERIC, while tree_could_trap_p can be called
2971    only on GIMPLE.  */
2972
2973 static bool
2974 generic_expr_could_trap_p (tree expr)
2975 {
2976   unsigned i, n;
2977
2978   if (!expr || is_gimple_val (expr))
2979     return false;
2980
2981   if (!EXPR_P (expr) || tree_could_trap_p (expr))
2982     return true;
2983
2984   n = TREE_OPERAND_LENGTH (expr);
2985   for (i = 0; i < n; i++)
2986     if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2987       return true;
2988
2989   return false;
2990 }
2991
2992 /*  Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2993     into
2994
2995     if (p)                      if (p)
2996       t1 = a;                     a;
2997     else                or      else
2998       t1 = b;                     b;
2999     t1;
3000
3001     The second form is used when *EXPR_P is of type void.
3002
3003     PRE_P points to the list where side effects that must happen before
3004       *EXPR_P should be stored.  */
3005
3006 static enum gimplify_status
3007 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3008 {
3009   tree expr = *expr_p;
3010   tree type = TREE_TYPE (expr);
3011   location_t loc = EXPR_LOCATION (expr);
3012   tree tmp, arm1, arm2;
3013   enum gimplify_status ret;
3014   tree label_true, label_false, label_cont;
3015   bool have_then_clause_p, have_else_clause_p;
3016   gcond *cond_stmt;
3017   enum tree_code pred_code;
3018   gimple_seq seq = NULL;
3019
3020   /* If this COND_EXPR has a value, copy the values into a temporary within
3021      the arms.  */
3022   if (!VOID_TYPE_P (type))
3023     {
3024       tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3025       tree result;
3026
3027       /* If either an rvalue is ok or we do not require an lvalue, create the
3028          temporary.  But we cannot do that if the type is addressable.  */
3029       if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3030           && !TREE_ADDRESSABLE (type))
3031         {
3032           if (gimplify_ctxp->allow_rhs_cond_expr
3033               /* If either branch has side effects or could trap, it can't be
3034                  evaluated unconditionally.  */
3035               && !TREE_SIDE_EFFECTS (then_)
3036               && !generic_expr_could_trap_p (then_)
3037               && !TREE_SIDE_EFFECTS (else_)
3038               && !generic_expr_could_trap_p (else_))
3039             return gimplify_pure_cond_expr (expr_p, pre_p);
3040
3041           tmp = create_tmp_var (type, "iftmp");
3042           result = tmp;
3043         }
3044
3045       /* Otherwise, only create and copy references to the values.  */
3046       else
3047         {
3048           type = build_pointer_type (type);
3049
3050           if (!VOID_TYPE_P (TREE_TYPE (then_)))
3051             then_ = build_fold_addr_expr_loc (loc, then_);
3052
3053           if (!VOID_TYPE_P (TREE_TYPE (else_)))
3054             else_ = build_fold_addr_expr_loc (loc, else_);
3055  
3056           expr
3057             = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3058
3059           tmp = create_tmp_var (type, "iftmp");
3060           result = build_simple_mem_ref_loc (loc, tmp);
3061         }
3062
3063       /* Build the new then clause, `tmp = then_;'.  But don't build the
3064          assignment if the value is void; in C++ it can be if it's a throw.  */
3065       if (!VOID_TYPE_P (TREE_TYPE (then_)))
3066         TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3067
3068       /* Similarly, build the new else clause, `tmp = else_;'.  */
3069       if (!VOID_TYPE_P (TREE_TYPE (else_)))
3070         TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3071
3072       TREE_TYPE (expr) = void_type_node;
3073       recalculate_side_effects (expr);
3074
3075       /* Move the COND_EXPR to the prequeue.  */
3076       gimplify_stmt (&expr, pre_p);
3077
3078       *expr_p = result;
3079       return GS_ALL_DONE;
3080     }
3081
3082   /* Remove any COMPOUND_EXPR so the following cases will be caught.  */
3083   STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3084   if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3085     gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3086
3087   /* Make sure the condition has BOOLEAN_TYPE.  */
3088   TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3089
3090   /* Break apart && and || conditions.  */
3091   if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3092       || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3093     {
3094       expr = shortcut_cond_expr (expr);
3095
3096       if (expr != *expr_p)
3097         {
3098           *expr_p = expr;
3099
3100           /* We can't rely on gimplify_expr to re-gimplify the expanded
3101              form properly, as cleanups might cause the target labels to be
3102              wrapped in a TRY_FINALLY_EXPR.  To prevent that, we need to
3103              set up a conditional context.  */
3104           gimple_push_condition ();
3105           gimplify_stmt (expr_p, &seq);
3106           gimple_pop_condition (pre_p);
3107           gimple_seq_add_seq (pre_p, seq);
3108
3109           return GS_ALL_DONE;
3110         }
3111     }
3112
3113   /* Now do the normal gimplification.  */
3114
3115   /* Gimplify condition.  */
3116   ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3117                        fb_rvalue);
3118   if (ret == GS_ERROR)
3119     return GS_ERROR;
3120   gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3121
3122   gimple_push_condition ();
3123
3124   have_then_clause_p = have_else_clause_p = false;
3125   if (TREE_OPERAND (expr, 1) != NULL
3126       && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3127       && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3128       && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3129           == current_function_decl)
3130       /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3131          have different locations, otherwise we end up with incorrect
3132          location information on the branches.  */
3133       && (optimize
3134           || !EXPR_HAS_LOCATION (expr)
3135           || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3136           || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3137     {
3138       label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3139       have_then_clause_p = true;
3140     }
3141   else
3142     label_true = create_artificial_label (UNKNOWN_LOCATION);
3143   if (TREE_OPERAND (expr, 2) != NULL
3144       && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3145       && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3146       && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3147           == current_function_decl)
3148       /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3149          have different locations, otherwise we end up with incorrect
3150          location information on the branches.  */
3151       && (optimize
3152           || !EXPR_HAS_LOCATION (expr)
3153           || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3154           || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3155     {
3156       label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3157       have_else_clause_p = true;
3158     }
3159   else
3160     label_false = create_artificial_label (UNKNOWN_LOCATION);
3161
3162   gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3163                                  &arm2);
3164
3165   cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3166                                    label_false);
3167
3168   gimplify_seq_add_stmt (&seq, cond_stmt);
3169   label_cont = NULL_TREE;
3170   if (!have_then_clause_p)
3171     {
3172       /* For if (...) {} else { code; } put label_true after
3173          the else block.  */
3174       if (TREE_OPERAND (expr, 1) == NULL_TREE
3175           && !have_else_clause_p
3176           && TREE_OPERAND (expr, 2) != NULL_TREE)
3177         label_cont = label_true;
3178       else
3179         {
3180           gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3181           have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3182           /* For if (...) { code; } else {} or
3183              if (...) { code; } else goto label; or
3184              if (...) { code; return; } else { ... }
3185              label_cont isn't needed.  */
3186           if (!have_else_clause_p
3187               && TREE_OPERAND (expr, 2) != NULL_TREE
3188               && gimple_seq_may_fallthru (seq))
3189             {
3190               gimple g;
3191               label_cont = create_artificial_label (UNKNOWN_LOCATION);
3192
3193               g = gimple_build_goto (label_cont);
3194
3195               /* GIMPLE_COND's are very low level; they have embedded
3196                  gotos.  This particular embedded goto should not be marked
3197                  with the location of the original COND_EXPR, as it would
3198                  correspond to the COND_EXPR's condition, not the ELSE or the
3199                  THEN arms.  To avoid marking it with the wrong location, flag
3200                  it as "no location".  */
3201               gimple_set_do_not_emit_location (g);
3202
3203               gimplify_seq_add_stmt (&seq, g);
3204             }
3205         }
3206     }
3207   if (!have_else_clause_p)
3208     {
3209       gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3210       have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3211     }
3212   if (label_cont)
3213     gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3214
3215   gimple_pop_condition (pre_p);
3216   gimple_seq_add_seq (pre_p, seq);
3217
3218   if (ret == GS_ERROR)
3219     ; /* Do nothing.  */
3220   else if (have_then_clause_p || have_else_clause_p)
3221     ret = GS_ALL_DONE;
3222   else
3223     {
3224       /* Both arms are empty; replace the COND_EXPR with its predicate.  */
3225       expr = TREE_OPERAND (expr, 0);
3226       gimplify_stmt (&expr, pre_p);
3227     }
3228
3229   *expr_p = NULL;
3230   return ret;
3231 }
3232
3233 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3234    to be marked addressable.
3235
3236    We cannot rely on such an expression being directly markable if a temporary
3237    has been created by the gimplification.  In this case, we create another
3238    temporary and initialize it with a copy, which will become a store after we
3239    mark it addressable.  This can happen if the front-end passed us something
3240    that it could not mark addressable yet, like a Fortran pass-by-reference
3241    parameter (int) floatvar.  */
3242
3243 static void
3244 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3245 {
3246   while (handled_component_p (*expr_p))
3247     expr_p = &TREE_OPERAND (*expr_p, 0);
3248   if (is_gimple_reg (*expr_p))
3249     {
3250       tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3251       DECL_GIMPLE_REG_P (var) = 0;
3252       *expr_p = var;
3253     }
3254 }
3255
3256 /* A subroutine of gimplify_modify_expr.  Replace a MODIFY_EXPR with
3257    a call to __builtin_memcpy.  */
3258
3259 static enum gimplify_status
3260 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3261                                 gimple_seq *seq_p)
3262 {
3263   tree t, to, to_ptr, from, from_ptr;
3264   gcall *gs;
3265   location_t loc = EXPR_LOCATION (*expr_p);
3266
3267   to = TREE_OPERAND (*expr_p, 0);
3268   from = TREE_OPERAND (*expr_p, 1);
3269
3270   /* Mark the RHS addressable.  Beware that it may not be possible to do so
3271      directly if a temporary has been created by the gimplification.  */
3272   prepare_gimple_addressable (&from, seq_p);
3273
3274   mark_addressable (from);
3275   from_ptr = build_fold_addr_expr_loc (loc, from);
3276   gimplify_arg (&from_ptr, seq_p, loc);
3277
3278   mark_addressable (to);
3279   to_ptr = build_fold_addr_expr_loc (loc, to);
3280   gimplify_arg (&to_ptr, seq_p, loc);
3281
3282   t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3283
3284   gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3285
3286   if (want_value)
3287     {
3288       /* tmp = memcpy() */
3289       t = create_tmp_var (TREE_TYPE (to_ptr));
3290       gimple_call_set_lhs (gs, t);
3291       gimplify_seq_add_stmt (seq_p, gs);
3292
3293       *expr_p = build_simple_mem_ref (t);
3294       return GS_ALL_DONE;
3295     }
3296
3297   gimplify_seq_add_stmt (seq_p, gs);
3298   *expr_p = NULL;
3299   return GS_ALL_DONE;
3300 }
3301
3302 /* A subroutine of gimplify_modify_expr.  Replace a MODIFY_EXPR with
3303    a call to __builtin_memset.  In this case we know that the RHS is
3304    a CONSTRUCTOR with an empty element list.  */
3305
3306 static enum gimplify_status
3307 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3308                                 gimple_seq *seq_p)
3309 {
3310   tree t, from, to, to_ptr;
3311   gcall *gs;
3312   location_t loc = EXPR_LOCATION (*expr_p);
3313
3314   /* Assert our assumptions, to abort instead of producing wrong code
3315      silently if they are not met.  Beware that the RHS CONSTRUCTOR might
3316      not be immediately exposed.  */
3317   from = TREE_OPERAND (*expr_p, 1);
3318   if (TREE_CODE (from) == WITH_SIZE_EXPR)
3319     from = TREE_OPERAND (from, 0);
3320
3321   gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3322               && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3323
3324   /* Now proceed.  */
3325   to = TREE_OPERAND (*expr_p, 0);
3326
3327   to_ptr = build_fold_addr_expr_loc (loc, to);
3328   gimplify_arg (&to_ptr, seq_p, loc);
3329   t = builtin_decl_implicit (BUILT_IN_MEMSET);
3330
3331   gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3332
3333   if (want_value)
3334     {
3335       /* tmp = memset() */
3336       t = create_tmp_var (TREE_TYPE (to_ptr));
3337       gimple_call_set_lhs (gs, t);
3338       gimplify_seq_add_stmt (seq_p, gs);
3339
3340       *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3341       return GS_ALL_DONE;
3342     }
3343
3344   gimplify_seq_add_stmt (seq_p, gs);
3345   *expr_p = NULL;
3346   return GS_ALL_DONE;
3347 }
3348
3349 /* A subroutine of gimplify_init_ctor_preeval.  Called via walk_tree,
3350    determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3351    assignment.  Return non-null if we detect a potential overlap.  */
3352
3353 struct gimplify_init_ctor_preeval_data
3354 {
3355   /* The base decl of the lhs object.  May be NULL, in which case we
3356      have to assume the lhs is indirect.  */
3357   tree lhs_base_decl;
3358
3359   /* The alias set of the lhs object.  */
3360   alias_set_type lhs_alias_set;
3361 };
3362
3363 static tree
3364 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3365 {
3366   struct gimplify_init_ctor_preeval_data *data
3367     = (struct gimplify_init_ctor_preeval_data *) xdata;
3368   tree t = *tp;
3369
3370   /* If we find the base object, obviously we have overlap.  */
3371   if (data->lhs_base_decl == t)
3372     return t;
3373
3374   /* If the constructor component is indirect, determine if we have a
3375      potential overlap with the lhs.  The only bits of information we
3376      have to go on at this point are addressability and alias sets.  */
3377   if ((INDIRECT_REF_P (t)
3378        || TREE_CODE (t) == MEM_REF)
3379       && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3380       && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3381     return t;
3382
3383   /* If the constructor component is a call, determine if it can hide a
3384      potential overlap with the lhs through an INDIRECT_REF like above.
3385      ??? Ugh - this is completely broken.  In fact this whole analysis
3386      doesn't look conservative.  */
3387   if (TREE_CODE (t) == CALL_EXPR)
3388     {
3389       tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3390
3391       for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3392         if (POINTER_TYPE_P (TREE_VALUE (type))
3393             && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3394             && alias_sets_conflict_p (data->lhs_alias_set,
3395                                       get_alias_set
3396                                         (TREE_TYPE (TREE_VALUE (type)))))
3397           return t;
3398     }
3399
3400   if (IS_TYPE_OR_DECL_P (t))
3401     *walk_subtrees = 0;
3402   return NULL;
3403 }
3404
3405 /* A subroutine of gimplify_init_constructor.  Pre-evaluate EXPR,
3406    force values that overlap with the lhs (as described by *DATA)
3407    into temporaries.  */
3408
3409 static void
3410 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3411                             struct gimplify_init_ctor_preeval_data *data)
3412 {
3413   enum gimplify_status one;
3414
3415   /* If the value is constant, then there's nothing to pre-evaluate.  */
3416   if (TREE_CONSTANT (*expr_p))
3417     {
3418       /* Ensure it does not have side effects, it might contain a reference to
3419          the object we're initializing.  */
3420       gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3421       return;
3422     }
3423
3424   /* If the type has non-trivial constructors, we can't pre-evaluate.  */
3425   if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3426     return;
3427
3428   /* Recurse for nested constructors.  */
3429   if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3430     {
3431       unsigned HOST_WIDE_INT ix;
3432       constructor_elt *ce;
3433       vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3434
3435       FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3436         gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3437
3438       return;
3439     }
3440
3441   /* If this is a variable sized type, we must remember the size.  */
3442   maybe_with_size_expr (expr_p);
3443
3444   /* Gimplify the constructor element to something appropriate for the rhs
3445      of a MODIFY_EXPR.  Given that we know the LHS is an aggregate, we know
3446      the gimplifier will consider this a store to memory.  Doing this
3447      gimplification now means that we won't have to deal with complicated
3448      language-specific trees, nor trees like SAVE_EXPR that can induce
3449      exponential search behavior.  */
3450   one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3451   if (one == GS_ERROR)
3452     {
3453       *expr_p = NULL;
3454       return;
3455     }
3456
3457   /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3458      with the lhs, since "a = { .x=a }" doesn't make sense.  This will
3459      always be true for all scalars, since is_gimple_mem_rhs insists on a
3460      temporary variable for them.  */
3461   if (DECL_P (*expr_p))
3462     return;
3463
3464   /* If this is of variable size, we have no choice but to assume it doesn't
3465      overlap since we can't make a temporary for it.  */
3466   if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3467     return;
3468
3469   /* Otherwise, we must search for overlap ...  */
3470   if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3471     return;
3472
3473   /* ... and if found, force the value into a temporary.  */
3474   *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3475 }
3476
3477 /* A subroutine of gimplify_init_ctor_eval.  Create a loop for
3478    a RANGE_EXPR in a CONSTRUCTOR for an array.
3479
3480       var = lower;
3481     loop_entry:
3482       object[var] = value;
3483       if (var == upper)
3484         goto loop_exit;
3485       var = var + 1;
3486       goto loop_entry;
3487     loop_exit:
3488
3489    We increment var _after_ the loop exit check because we might otherwise
3490    fail if upper == TYPE_MAX_VALUE (type for upper).
3491
3492    Note that we never have to deal with SAVE_EXPRs here, because this has
3493    already been taken care of for us, in gimplify_init_ctor_preeval().  */
3494
3495 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3496                                      gimple_seq *, bool);
3497
3498 static void
3499 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3500                                tree value, tree array_elt_type,
3501                                gimple_seq *pre_p, bool cleared)
3502 {
3503   tree loop_entry_label, loop_exit_label, fall_thru_label;
3504   tree var, var_type, cref, tmp;
3505
3506   loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3507   loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3508   fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3509
3510   /* Create and initialize the index variable.  */
3511   var_type = TREE_TYPE (upper);
3512   var = create_tmp_var (var_type);
3513   gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3514
3515   /* Add the loop entry label.  */
3516   gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3517
3518   /* Build the reference.  */
3519   cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3520                  var, NULL_TREE, NULL_TREE);
3521
3522   /* If we are a constructor, just call gimplify_init_ctor_eval to do
3523      the store.  Otherwise just assign value to the reference.  */
3524
3525   if (TREE_CODE (value) == CONSTRUCTOR)
3526     /* NB we might have to call ourself recursively through
3527        gimplify_init_ctor_eval if the value is a constructor.  */
3528     gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3529                              pre_p, cleared);
3530   else
3531     gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3532
3533   /* We exit the loop when the index var is equal to the upper bound.  */
3534   gimplify_seq_add_stmt (pre_p,
3535                          gimple_build_cond (EQ_EXPR, var, upper,
3536                                             loop_exit_label, fall_thru_label));
3537
3538   gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3539
3540   /* Otherwise, increment the index var...  */
3541   tmp = build2 (PLUS_EXPR, var_type, var,
3542                 fold_convert (var_type, integer_one_node));
3543   gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3544
3545   /* ...and jump back to the loop entry.  */
3546   gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3547
3548   /* Add the loop exit label.  */
3549   gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3550 }
3551
3552 /* Return true if FDECL is accessing a field that is zero sized.  */
3553
3554 static bool
3555 zero_sized_field_decl (const_tree fdecl)
3556 {
3557   if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3558       && integer_zerop (DECL_SIZE (fdecl)))
3559     return true;
3560   return false;
3561 }
3562
3563 /* Return true if TYPE is zero sized.  */
3564
3565 static bool
3566 zero_sized_type (const_tree type)
3567 {
3568   if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3569       && integer_zerop (TYPE_SIZE (type)))
3570     return true;
3571   return false;
3572 }
3573
3574 /* A subroutine of gimplify_init_constructor.  Generate individual
3575    MODIFY_EXPRs for a CONSTRUCTOR.  OBJECT is the LHS against which the
3576    assignments should happen.  ELTS is the CONSTRUCTOR_ELTS of the
3577    CONSTRUCTOR.  CLEARED is true if the entire LHS object has been
3578    zeroed first.  */
3579
3580 static void
3581 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3582                          gimple_seq *pre_p, bool cleared)
3583 {
3584   tree array_elt_type = NULL;
3585   unsigned HOST_WIDE_INT ix;
3586   tree purpose, value;
3587
3588   if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3589     array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3590
3591   FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3592     {
3593       tree cref;
3594
3595       /* NULL values are created above for gimplification errors.  */
3596       if (value == NULL)
3597         continue;
3598
3599       if (cleared && initializer_zerop (value))
3600         continue;
3601
3602       /* ??? Here's to hoping the front end fills in all of the indices,
3603          so we don't have to figure out what's missing ourselves.  */
3604       gcc_assert (purpose);
3605
3606       /* Skip zero-sized fields, unless value has side-effects.  This can
3607          happen with calls to functions returning a zero-sized type, which
3608          we shouldn't discard.  As a number of downstream passes don't
3609          expect sets of zero-sized fields, we rely on the gimplification of
3610          the MODIFY_EXPR we make below to drop the assignment statement.  */
3611       if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3612         continue;
3613
3614       /* If we have a RANGE_EXPR, we have to build a loop to assign the
3615          whole range.  */
3616       if (TREE_CODE (purpose) == RANGE_EXPR)
3617         {
3618           tree lower = TREE_OPERAND (purpose, 0);
3619           tree upper = TREE_OPERAND (purpose, 1);
3620
3621           /* If the lower bound is equal to upper, just treat it as if
3622              upper was the index.  */
3623           if (simple_cst_equal (lower, upper))
3624             purpose = upper;
3625           else
3626             {
3627               gimplify_init_ctor_eval_range (object, lower, upper, value,
3628                                              array_elt_type, pre_p, cleared);
3629               continue;
3630             }
3631         }
3632
3633       if (array_elt_type)
3634         {
3635           /* Do not use bitsizetype for ARRAY_REF indices.  */
3636           if (TYPE_DOMAIN (TREE_TYPE (object)))
3637             purpose
3638               = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3639                               purpose);
3640           cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3641                          purpose, NULL_TREE, NULL_TREE);
3642         }
3643       else
3644         {
3645           gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3646           cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3647                          unshare_expr (object), purpose, NULL_TREE);
3648         }
3649
3650       if (TREE_CODE (value) == CONSTRUCTOR
3651           && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3652         gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3653                                  pre_p, cleared);
3654       else
3655         {
3656           tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3657           gimplify_and_add (init, pre_p);
3658           ggc_free (init);
3659         }
3660     }
3661 }
3662
3663 /* Return the appropriate RHS predicate for this LHS.  */
3664
3665 gimple_predicate
3666 rhs_predicate_for (tree lhs)
3667 {
3668   if (is_gimple_reg (lhs))
3669     return is_gimple_reg_rhs_or_call;
3670   else
3671     return is_gimple_mem_rhs_or_call;
3672 }
3673
3674 /* Gimplify a C99 compound literal expression.  This just means adding
3675    the DECL_EXPR before the current statement and using its anonymous
3676    decl instead.  */
3677
3678 static enum gimplify_status
3679 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3680                                 bool (*gimple_test_f) (tree),
3681                                 fallback_t fallback)
3682 {
3683   tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3684   tree decl = DECL_EXPR_DECL (decl_s);
3685   tree init = DECL_INITIAL (decl);
3686   /* Mark the decl as addressable if the compound literal
3687      expression is addressable now, otherwise it is marked too late
3688      after we gimplify the initialization expression.  */
3689   if (TREE_ADDRESSABLE (*expr_p))
3690     TREE_ADDRESSABLE (decl) = 1;
3691   /* Otherwise, if we don't need an lvalue and have a literal directly
3692      substitute it.  Check if it matches the gimple predicate, as
3693      otherwise we'd generate a new temporary, and we can as well just
3694      use the decl we already have.  */
3695   else if (!TREE_ADDRESSABLE (decl)
3696            && init
3697            && (fallback & fb_lvalue) == 0
3698            && gimple_test_f (init))
3699     {
3700       *expr_p = init;
3701       return GS_OK;
3702     }
3703
3704   /* Preliminarily mark non-addressed complex variables as eligible
3705      for promotion to gimple registers.  We'll transform their uses
3706      as we find them.  */
3707   if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3708        || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3709       && !TREE_THIS_VOLATILE (decl)
3710       && !needs_to_live_in_memory (decl))
3711     DECL_GIMPLE_REG_P (decl) = 1;
3712
3713   /* If the decl is not addressable, then it is being used in some
3714      expression or on the right hand side of a statement, and it can
3715      be put into a readonly data section.  */
3716   if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3717     TREE_READONLY (decl) = 1;
3718
3719   /* This decl isn't mentioned in the enclosing block, so add it to the
3720      list of temps.  FIXME it seems a bit of a kludge to say that
3721      anonymous artificial vars aren't pushed, but everything else is.  */
3722   if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3723     gimple_add_tmp_var (decl);
3724
3725   gimplify_and_add (decl_s, pre_p);
3726   *expr_p = decl;
3727   return GS_OK;
3728 }
3729
3730 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3731    return a new CONSTRUCTOR if something changed.  */
3732
3733 static tree
3734 optimize_compound_literals_in_ctor (tree orig_ctor)
3735 {
3736   tree ctor = orig_ctor;
3737   vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3738   unsigned int idx, num = vec_safe_length (elts);
3739
3740   for (idx = 0; idx < num; idx++)
3741     {
3742       tree value = (*elts)[idx].value;
3743       tree newval = value;
3744       if (TREE_CODE (value) == CONSTRUCTOR)
3745         newval = optimize_compound_literals_in_ctor (value);
3746       else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3747         {
3748           tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3749           tree decl = DECL_EXPR_DECL (decl_s);
3750           tree init = DECL_INITIAL (decl);
3751
3752           if (!TREE_ADDRESSABLE (value)
3753               && !TREE_ADDRESSABLE (decl)
3754               && init
3755               && TREE_CODE (init) == CONSTRUCTOR)
3756             newval = optimize_compound_literals_in_ctor (init);
3757         }
3758       if (newval == value)
3759         continue;
3760
3761       if (ctor == orig_ctor)
3762         {
3763           ctor = copy_node (orig_ctor);
3764           CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3765           elts = CONSTRUCTOR_ELTS (ctor);
3766         }
3767       (*elts)[idx].value = newval;
3768     }
3769   return ctor;
3770 }
3771
3772 /* A subroutine of gimplify_modify_expr.  Break out elements of a
3773    CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3774
3775    Note that we still need to clear any elements that don't have explicit
3776    initializers, so if not all elements are initialized we keep the
3777    original MODIFY_EXPR, we just remove all of the constructor elements.
3778
3779    If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3780    GS_ERROR if we would have to create a temporary when gimplifying
3781    this constructor.  Otherwise, return GS_OK.
3782
3783    If NOTIFY_TEMP_CREATION is false, just do the gimplification.  */
3784
3785 static enum gimplify_status
3786 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3787                            bool want_value, bool notify_temp_creation)
3788 {
3789   tree object, ctor, type;
3790   enum gimplify_status ret;
3791   vec<constructor_elt, va_gc> *elts;
3792
3793   gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3794
3795   if (!notify_temp_creation)
3796     {
3797       ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3798                            is_gimple_lvalue, fb_lvalue);
3799       if (ret == GS_ERROR)
3800         return ret;
3801     }
3802
3803   object = TREE_OPERAND (*expr_p, 0);
3804   ctor = TREE_OPERAND (*expr_p, 1) =
3805     optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3806   type = TREE_TYPE (ctor);
3807   elts = CONSTRUCTOR_ELTS (ctor);
3808   ret = GS_ALL_DONE;
3809
3810   switch (TREE_CODE (type))
3811     {
3812     case RECORD_TYPE:
3813     case UNION_TYPE:
3814     case QUAL_UNION_TYPE:
3815     case ARRAY_TYPE:
3816       {
3817         struct gimplify_init_ctor_preeval_data preeval_data;
3818         HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3819         bool cleared, complete_p, valid_const_initializer;
3820
3821         /* Aggregate types must lower constructors to initialization of
3822            individual elements.  The exception is that a CONSTRUCTOR node
3823            with no elements indicates zero-initialization of the whole.  */
3824         if (vec_safe_is_empty (elts))
3825           {
3826             if (notify_temp_creation)
3827               return GS_OK;
3828             break;
3829           }
3830
3831         /* Fetch information about the constructor to direct later processing.
3832            We might want to make static versions of it in various cases, and
3833            can only do so if it known to be a valid constant initializer.  */
3834         valid_const_initializer
3835           = categorize_ctor_elements (ctor, &num_nonzero_elements,
3836                                       &num_ctor_elements, &complete_p);
3837
3838         /* If a const aggregate variable is being initialized, then it
3839            should never be a lose to promote the variable to be static.  */
3840         if (valid_const_initializer
3841             && num_nonzero_elements > 1
3842             && TREE_READONLY (object)
3843             && TREE_CODE (object) == VAR_DECL
3844             && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3845           {
3846             if (notify_temp_creation)
3847               return GS_ERROR;
3848             DECL_INITIAL (object) = ctor;
3849             TREE_STATIC (object) = 1;
3850             if (!DECL_NAME (object))
3851               DECL_NAME (object) = create_tmp_var_name ("C");
3852             walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3853
3854             /* ??? C++ doesn't automatically append a .<number> to the
3855                assembler name, and even when it does, it looks at FE private
3856                data structures to figure out what that number should be,
3857                which are not set for this variable.  I suppose this is
3858                important for local statics for inline functions, which aren't
3859                "local" in the object file sense.  So in order to get a unique
3860                TU-local symbol, we must invoke the lhd version now.  */
3861             lhd_set_decl_assembler_name (object);
3862
3863             *expr_p = NULL_TREE;
3864             break;
3865           }
3866
3867         /* If there are "lots" of initialized elements, even discounting
3868            those that are not address constants (and thus *must* be
3869            computed at runtime), then partition the constructor into
3870            constant and non-constant parts.  Block copy the constant
3871            parts in, then generate code for the non-constant parts.  */
3872         /* TODO.  There's code in cp/typeck.c to do this.  */
3873
3874         if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3875           /* store_constructor will ignore the clearing of variable-sized
3876              objects.  Initializers for such objects must explicitly set
3877              every field that needs to be set.  */
3878           cleared = false;
3879         else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3880           /* If the constructor isn't complete, clear the whole object
3881              beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3882
3883              ??? This ought not to be needed.  For any element not present
3884              in the initializer, we should simply set them to zero.  Except
3885              we'd need to *find* the elements that are not present, and that
3886              requires trickery to avoid quadratic compile-time behavior in
3887              large cases or excessive memory use in small cases.  */
3888           cleared = true;
3889         else if (num_ctor_elements - num_nonzero_elements
3890                  > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3891                  && num_nonzero_elements < num_ctor_elements / 4)
3892           /* If there are "lots" of zeros, it's more efficient to clear
3893              the memory and then set the nonzero elements.  */
3894           cleared = true;
3895         else
3896           cleared = false;
3897
3898         /* If there are "lots" of initialized elements, and all of them
3899            are valid address constants, then the entire initializer can
3900            be dropped to memory, and then memcpy'd out.  Don't do this
3901            for sparse arrays, though, as it's more efficient to follow
3902            the standard CONSTRUCTOR behavior of memset followed by
3903            individual element initialization.  Also don't do this for small
3904            all-zero initializers (which aren't big enough to merit
3905            clearing), and don't try to make bitwise copies of
3906            TREE_ADDRESSABLE types.
3907
3908            We cannot apply such transformation when compiling chkp static
3909            initializer because creation of initializer image in the memory
3910            will require static initialization of bounds for it.  It should
3911            result in another gimplification of similar initializer and we
3912            may fall into infinite loop.  */
3913         if (valid_const_initializer
3914             && !(cleared || num_nonzero_elements == 0)
3915             && !TREE_ADDRESSABLE (type)
3916             && (!current_function_decl
3917                 || !lookup_attribute ("chkp ctor",
3918                                       DECL_ATTRIBUTES (current_function_decl))))
3919           {
3920             HOST_WIDE_INT size = int_size_in_bytes (type);
3921             unsigned int align;
3922
3923             /* ??? We can still get unbounded array types, at least
3924                from the C++ front end.  This seems wrong, but attempt
3925                to work around it for now.  */
3926             if (size < 0)
3927               {
3928                 size = int_size_in_bytes (TREE_TYPE (object));
3929                 if (size >= 0)
3930                   TREE_TYPE (ctor) = type = TREE_TYPE (object);
3931               }
3932
3933             /* Find the maximum alignment we can assume for the object.  */
3934             /* ??? Make use of DECL_OFFSET_ALIGN.  */
3935             if (DECL_P (object))
3936               align = DECL_ALIGN (object);
3937             else
3938               align = TYPE_ALIGN (type);
3939
3940             /* Do a block move either if the size is so small as to make
3941                each individual move a sub-unit move on average, or if it
3942                is so large as to make individual moves inefficient.  */
3943             if (size > 0
3944                 && num_nonzero_elements > 1
3945                 && (size < num_nonzero_elements
3946                     || !can_move_by_pieces (size, align)))
3947               {
3948                 if (notify_temp_creation)
3949                   return GS_ERROR;
3950
3951                 walk_tree (&ctor, force_labels_r, NULL, NULL);
3952                 ctor = tree_output_constant_def (ctor);
3953                 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3954                   ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3955                 TREE_OPERAND (*expr_p, 1) = ctor;
3956
3957                 /* This is no longer an assignment of a CONSTRUCTOR, but
3958                    we still may have processing to do on the LHS.  So
3959                    pretend we didn't do anything here to let that happen.  */
3960                 return GS_UNHANDLED;
3961               }
3962           }
3963
3964         /* If the target is volatile, we have non-zero elements and more than
3965            one field to assign, initialize the target from a temporary.  */
3966         if (TREE_THIS_VOLATILE (object)
3967             && !TREE_ADDRESSABLE (type)
3968             && num_nonzero_elements > 0
3969             && vec_safe_length (elts) > 1)
3970           {
3971             tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
3972             TREE_OPERAND (*expr_p, 0) = temp;
3973             *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3974                               *expr_p,
3975                               build2 (MODIFY_EXPR, void_type_node,
3976                                       object, temp));
3977             return GS_OK;
3978           }
3979
3980         if (notify_temp_creation)
3981           return GS_OK;
3982
3983         /* If there are nonzero elements and if needed, pre-evaluate to capture
3984            elements overlapping with the lhs into temporaries.  We must do this
3985            before clearing to fetch the values before they are zeroed-out.  */
3986         if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3987           {
3988             preeval_data.lhs_base_decl = get_base_address (object);
3989             if (!DECL_P (preeval_data.lhs_base_decl))
3990               preeval_data.lhs_base_decl = NULL;
3991             preeval_data.lhs_alias_set = get_alias_set (object);
3992
3993             gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3994                                         pre_p, post_p, &preeval_data);
3995           }
3996
3997         if (cleared)
3998           {
3999             /* Zap the CONSTRUCTOR element list, which simplifies this case.
4000                Note that we still have to gimplify, in order to handle the
4001                case of variable sized types.  Avoid shared tree structures.  */
4002             CONSTRUCTOR_ELTS (ctor) = NULL;
4003             TREE_SIDE_EFFECTS (ctor) = 0;
4004             object = unshare_expr (object);
4005             gimplify_stmt (expr_p, pre_p);
4006           }
4007
4008         /* If we have not block cleared the object, or if there are nonzero
4009            elements in the constructor, add assignments to the individual
4010            scalar fields of the object.  */
4011         if (!cleared || num_nonzero_elements > 0)
4012           gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4013
4014         *expr_p = NULL_TREE;
4015       }
4016       break;
4017
4018     case COMPLEX_TYPE:
4019       {
4020         tree r, i;
4021
4022         if (notify_temp_creation)
4023           return GS_OK;
4024
4025         /* Extract the real and imaginary parts out of the ctor.  */
4026         gcc_assert (elts->length () == 2);
4027         r = (*elts)[0].value;
4028         i = (*elts)[1].value;
4029         if (r == NULL || i == NULL)
4030           {
4031             tree zero = build_zero_cst (TREE_TYPE (type));
4032             if (r == NULL)
4033               r = zero;
4034             if (i == NULL)
4035               i = zero;
4036           }
4037
4038         /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4039            represent creation of a complex value.  */
4040         if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4041           {
4042             ctor = build_complex (type, r, i);
4043             TREE_OPERAND (*expr_p, 1) = ctor;
4044           }
4045         else
4046           {
4047             ctor = build2 (COMPLEX_EXPR, type, r, i);
4048             TREE_OPERAND (*expr_p, 1) = ctor;
4049             ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4050                                  pre_p,
4051                                  post_p,
4052                                  rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4053                                  fb_rvalue);
4054           }
4055       }
4056       break;
4057
4058     case VECTOR_TYPE:
4059       {
4060         unsigned HOST_WIDE_INT ix;
4061         constructor_elt *ce;
4062
4063         if (notify_temp_creation)
4064           return GS_OK;
4065
4066         /* Go ahead and simplify constant constructors to VECTOR_CST.  */
4067         if (TREE_CONSTANT (ctor))
4068           {
4069             bool constant_p = true;
4070             tree value;
4071
4072             /* Even when ctor is constant, it might contain non-*_CST
4073                elements, such as addresses or trapping values like
4074                1.0/0.0 - 1.0/0.0.  Such expressions don't belong
4075                in VECTOR_CST nodes.  */
4076             FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4077               if (!CONSTANT_CLASS_P (value))
4078                 {
4079                   constant_p = false;
4080                   break;
4081                 }
4082
4083             if (constant_p)
4084               {
4085                 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4086                 break;
4087               }
4088
4089             TREE_CONSTANT (ctor) = 0;
4090           }
4091
4092         /* Vector types use CONSTRUCTOR all the way through gimple
4093           compilation as a general initializer.  */
4094         FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4095           {
4096             enum gimplify_status tret;
4097             tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4098                                   fb_rvalue);
4099             if (tret == GS_ERROR)
4100               ret = GS_ERROR;
4101           }
4102         if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4103           TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4104       }
4105       break;
4106
4107     default:
4108       /* So how did we get a CONSTRUCTOR for a scalar type?  */
4109       gcc_unreachable ();
4110     }
4111
4112   if (ret == GS_ERROR)
4113     return GS_ERROR;
4114   else if (want_value)
4115     {
4116       *expr_p = object;
4117       return GS_OK;
4118     }
4119   else
4120     {
4121       /* If we have gimplified both sides of the initializer but have
4122          not emitted an assignment, do so now.  */
4123       if (*expr_p)
4124         {
4125           tree lhs = TREE_OPERAND (*expr_p, 0);
4126           tree rhs = TREE_OPERAND (*expr_p, 1);
4127           gassign *init = gimple_build_assign (lhs, rhs);
4128           gimplify_seq_add_stmt (pre_p, init);
4129           *expr_p = NULL;
4130         }
4131
4132       return GS_ALL_DONE;
4133     }
4134 }
4135
4136 /* Given a pointer value OP0, return a simplified version of an
4137    indirection through OP0, or NULL_TREE if no simplification is
4138    possible.  This may only be applied to a rhs of an expression.
4139    Note that the resulting type may be different from the type pointed
4140    to in the sense that it is still compatible from the langhooks
4141    point of view. */
4142
4143 static tree
4144 gimple_fold_indirect_ref_rhs (tree t)
4145 {
4146   return gimple_fold_indirect_ref (t);
4147 }
4148
4149 /* Subroutine of gimplify_modify_expr to do simplifications of
4150    MODIFY_EXPRs based on the code of the RHS.  We loop for as long as
4151    something changes.  */
4152
4153 static enum gimplify_status
4154 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4155                           gimple_seq *pre_p, gimple_seq *post_p,
4156                           bool want_value)
4157 {
4158   enum gimplify_status ret = GS_UNHANDLED;
4159   bool changed;
4160
4161   do
4162     {
4163       changed = false;
4164       switch (TREE_CODE (*from_p))
4165         {
4166         case VAR_DECL:
4167           /* If we're assigning from a read-only variable initialized with
4168              a constructor, do the direct assignment from the constructor,
4169              but only if neither source nor target are volatile since this
4170              latter assignment might end up being done on a per-field basis.  */
4171           if (DECL_INITIAL (*from_p)
4172               && TREE_READONLY (*from_p)
4173               && !TREE_THIS_VOLATILE (*from_p)
4174               && !TREE_THIS_VOLATILE (*to_p)
4175               && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4176             {
4177               tree old_from = *from_p;
4178               enum gimplify_status subret;
4179
4180               /* Move the constructor into the RHS.  */
4181               *from_p = unshare_expr (DECL_INITIAL (*from_p));
4182
4183               /* Let's see if gimplify_init_constructor will need to put
4184                  it in memory.  */
4185               subret = gimplify_init_constructor (expr_p, NULL, NULL,
4186                                                   false, true);
4187               if (subret == GS_ERROR)
4188                 {
4189                   /* If so, revert the change.  */
4190                   *from_p = old_from;
4191                 }
4192               else
4193                 {
4194                   ret = GS_OK;
4195                   changed = true;
4196                 }
4197             }
4198           break;
4199         case INDIRECT_REF:
4200           {
4201             /* If we have code like
4202
4203              *(const A*)(A*)&x
4204
4205              where the type of "x" is a (possibly cv-qualified variant
4206              of "A"), treat the entire expression as identical to "x".
4207              This kind of code arises in C++ when an object is bound
4208              to a const reference, and if "x" is a TARGET_EXPR we want
4209              to take advantage of the optimization below.  */
4210             bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4211             tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4212             if (t)
4213               {
4214                 if (TREE_THIS_VOLATILE (t) != volatile_p)
4215                   {
4216                     if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4217                       t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4218                                                     build_fold_addr_expr (t));
4219                     if (REFERENCE_CLASS_P (t))
4220                       TREE_THIS_VOLATILE (t) = volatile_p;
4221                   }
4222                 *from_p = t;
4223                 ret = GS_OK;
4224                 changed = true;
4225               }
4226             break;
4227           }
4228
4229         case TARGET_EXPR:
4230           {
4231             /* If we are initializing something from a TARGET_EXPR, strip the
4232                TARGET_EXPR and initialize it directly, if possible.  This can't
4233                be done if the initializer is void, since that implies that the
4234                temporary is set in some non-trivial way.
4235
4236                ??? What about code that pulls out the temp and uses it
4237                elsewhere? I think that such code never uses the TARGET_EXPR as
4238                an initializer.  If I'm wrong, we'll die because the temp won't
4239                have any RTL.  In that case, I guess we'll need to replace
4240                references somehow.  */
4241             tree init = TARGET_EXPR_INITIAL (*from_p);
4242
4243             if (init
4244                 && !VOID_TYPE_P (TREE_TYPE (init)))
4245               {
4246                 *from_p = init;
4247                 ret = GS_OK;
4248                 changed = true;
4249               }
4250           }
4251           break;
4252
4253         case COMPOUND_EXPR:
4254           /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4255              caught.  */
4256           gimplify_compound_expr (from_p, pre_p, true);
4257           ret = GS_OK;
4258           changed = true;
4259           break;
4260
4261         case CONSTRUCTOR:
4262           /* If we already made some changes, let the front end have a
4263              crack at this before we break it down.  */
4264           if (ret != GS_UNHANDLED)
4265             break;
4266           /* If we're initializing from a CONSTRUCTOR, break this into
4267              individual MODIFY_EXPRs.  */
4268           return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4269                                             false);
4270
4271         case COND_EXPR:
4272           /* If we're assigning to a non-register type, push the assignment
4273              down into the branches.  This is mandatory for ADDRESSABLE types,
4274              since we cannot generate temporaries for such, but it saves a
4275              copy in other cases as well.  */
4276           if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4277             {
4278               /* This code should mirror the code in gimplify_cond_expr. */
4279               enum tree_code code = TREE_CODE (*expr_p);
4280               tree cond = *from_p;
4281               tree result = *to_p;
4282
4283               ret = gimplify_expr (&result, pre_p, post_p,
4284                                    is_gimple_lvalue, fb_lvalue);
4285               if (ret != GS_ERROR)
4286                 ret = GS_OK;
4287
4288               if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4289                 TREE_OPERAND (cond, 1)
4290                   = build2 (code, void_type_node, result,
4291                             TREE_OPERAND (cond, 1));
4292               if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4293                 TREE_OPERAND (cond, 2)
4294                   = build2 (code, void_type_node, unshare_expr (result),
4295                             TREE_OPERAND (cond, 2));
4296
4297               TREE_TYPE (cond) = void_type_node;
4298               recalculate_side_effects (cond);
4299
4300               if (want_value)
4301                 {
4302                   gimplify_and_add (cond, pre_p);
4303                   *expr_p = unshare_expr (result);
4304                 }
4305               else
4306                 *expr_p = cond;
4307               return ret;
4308             }
4309           break;
4310
4311         case CALL_EXPR:
4312           /* For calls that return in memory, give *to_p as the CALL_EXPR's
4313              return slot so that we don't generate a temporary.  */
4314           if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4315               && aggregate_value_p (*from_p, *from_p))
4316             {
4317               bool use_target;
4318
4319               if (!(rhs_predicate_for (*to_p))(*from_p))
4320                 /* If we need a temporary, *to_p isn't accurate.  */
4321                 use_target = false;
4322               /* It's OK to use the return slot directly unless it's an NRV. */
4323               else if (TREE_CODE (*to_p) == RESULT_DECL
4324                        && DECL_NAME (*to_p) == NULL_TREE
4325                        && needs_to_live_in_memory (*to_p))
4326                 use_target = true;
4327               else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4328                        || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4329                 /* Don't force regs into memory.  */
4330                 use_target = false;
4331               else if (TREE_CODE (*expr_p) == INIT_EXPR)
4332                 /* It's OK to use the target directly if it's being
4333                    initialized. */
4334                 use_target = true;
4335               else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4336                 /* Always use the target and thus RSO for variable-sized types.
4337                    GIMPLE cannot deal with a variable-sized assignment
4338                    embedded in a call statement.  */
4339                 use_target = true;
4340               else if (TREE_CODE (*to_p) != SSA_NAME
4341                       && (!is_gimple_variable (*to_p)
4342                           || needs_to_live_in_memory (*to_p)))
4343                 /* Don't use the original target if it's already addressable;
4344                    if its address escapes, and the called function uses the
4345                    NRV optimization, a conforming program could see *to_p
4346                    change before the called function returns; see c++/19317.
4347                    When optimizing, the return_slot pass marks more functions
4348                    as safe after we have escape info.  */
4349                 use_target = false;
4350               else
4351                 use_target = true;
4352
4353               if (use_target)
4354                 {
4355                   CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4356                   mark_addressable (*to_p);
4357                 }
4358             }
4359           break;
4360
4361         case WITH_SIZE_EXPR:
4362           /* Likewise for calls that return an aggregate of non-constant size,
4363              since we would not be able to generate a temporary at all.  */
4364           if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4365             {
4366               *from_p = TREE_OPERAND (*from_p, 0);
4367               /* We don't change ret in this case because the
4368                  WITH_SIZE_EXPR might have been added in
4369                  gimplify_modify_expr, so returning GS_OK would lead to an
4370                  infinite loop.  */
4371               changed = true;
4372             }
4373           break;
4374
4375           /* If we're initializing from a container, push the initialization
4376              inside it.  */
4377         case CLEANUP_POINT_EXPR:
4378         case BIND_EXPR:
4379         case STATEMENT_LIST:
4380           {
4381             tree wrap = *from_p;
4382             tree t;
4383
4384             ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4385                                  fb_lvalue);
4386             if (ret != GS_ERROR)
4387               ret = GS_OK;
4388
4389             t = voidify_wrapper_expr (wrap, *expr_p);
4390             gcc_assert (t == *expr_p);
4391
4392             if (want_value)
4393               {
4394                 gimplify_and_add (wrap, pre_p);
4395                 *expr_p = unshare_expr (*to_p);
4396               }
4397             else
4398               *expr_p = wrap;
4399             return GS_OK;
4400           }
4401
4402         case COMPOUND_LITERAL_EXPR:
4403           {
4404             tree complit = TREE_OPERAND (*expr_p, 1);
4405             tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4406             tree decl = DECL_EXPR_DECL (decl_s);
4407             tree init = DECL_INITIAL (decl);
4408
4409             /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4410                into struct T x = { 0, 1, 2 } if the address of the
4411                compound literal has never been taken.  */
4412             if (!TREE_ADDRESSABLE (complit)
4413                 && !TREE_ADDRESSABLE (decl)
4414                 && init)
4415               {
4416                 *expr_p = copy_node (*expr_p);
4417                 TREE_OPERAND (*expr_p, 1) = init;
4418                 return GS_OK;
4419               }
4420           }
4421
4422         default:
4423           break;
4424         }
4425     }
4426   while (changed);
4427
4428   return ret;
4429 }
4430
4431
4432 /* Return true if T looks like a valid GIMPLE statement.  */
4433
4434 static bool
4435 is_gimple_stmt (tree t)
4436 {
4437   const enum tree_code code = TREE_CODE (t);
4438
4439   switch (code)
4440     {
4441     case NOP_EXPR:
4442       /* The only valid NOP_EXPR is the empty statement.  */
4443       return IS_EMPTY_STMT (t);
4444
4445     case BIND_EXPR:
4446     case COND_EXPR:
4447       /* These are only valid if they're void.  */
4448       return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4449
4450     case SWITCH_EXPR:
4451     case GOTO_EXPR:
4452     case RETURN_EXPR:
4453     case LABEL_EXPR:
4454     case CASE_LABEL_EXPR:
4455     case TRY_CATCH_EXPR:
4456     case TRY_FINALLY_EXPR:
4457     case EH_FILTER_EXPR:
4458     case CATCH_EXPR:
4459     case ASM_EXPR:
4460     case STATEMENT_LIST:
4461     case OACC_PARALLEL:
4462     case OACC_KERNELS:
4463     case OACC_DATA:
4464     case OACC_HOST_DATA:
4465     case OACC_DECLARE:
4466     case OACC_UPDATE:
4467     case OACC_ENTER_DATA:
4468     case OACC_EXIT_DATA:
4469     case OACC_CACHE:
4470     case OMP_PARALLEL:
4471     case OMP_FOR:
4472     case OMP_SIMD:
4473     case CILK_SIMD:
4474     case OMP_DISTRIBUTE:
4475     case OACC_LOOP:
4476     case OMP_SECTIONS:
4477     case OMP_SECTION:
4478     case OMP_SINGLE:
4479     case OMP_MASTER:
4480     case OMP_TASKGROUP:
4481     case OMP_ORDERED:
4482     case OMP_CRITICAL:
4483     case OMP_TASK:
4484       /* These are always void.  */
4485       return true;
4486
4487     case CALL_EXPR:
4488     case MODIFY_EXPR:
4489     case PREDICT_EXPR:
4490       /* These are valid regardless of their type.  */
4491       return true;
4492
4493     default:
4494       return false;
4495     }
4496 }
4497
4498
4499 /* Promote partial stores to COMPLEX variables to total stores.  *EXPR_P is
4500    a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4501    DECL_GIMPLE_REG_P set.
4502
4503    IMPORTANT NOTE: This promotion is performed by introducing a load of the
4504    other, unmodified part of the complex object just before the total store.
4505    As a consequence, if the object is still uninitialized, an undefined value
4506    will be loaded into a register, which may result in a spurious exception
4507    if the register is floating-point and the value happens to be a signaling
4508    NaN for example.  Then the fully-fledged complex operations lowering pass
4509    followed by a DCE pass are necessary in order to fix things up.  */
4510
4511 static enum gimplify_status
4512 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4513                                    bool want_value)
4514 {
4515   enum tree_code code, ocode;
4516   tree lhs, rhs, new_rhs, other, realpart, imagpart;
4517
4518   lhs = TREE_OPERAND (*expr_p, 0);
4519   rhs = TREE_OPERAND (*expr_p, 1);
4520   code = TREE_CODE (lhs);
4521   lhs = TREE_OPERAND (lhs, 0);
4522
4523   ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4524   other = build1 (ocode, TREE_TYPE (rhs), lhs);
4525   TREE_NO_WARNING (other) = 1;
4526   other = get_formal_tmp_var (other, pre_p);
4527
4528   realpart = code == REALPART_EXPR ? rhs : other;
4529   imagpart = code == REALPART_EXPR ? other : rhs;
4530
4531   if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4532     new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4533   else
4534     new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4535
4536   gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4537   *expr_p = (want_value) ? rhs : NULL_TREE;
4538
4539   return GS_ALL_DONE;
4540 }
4541
4542 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4543
4544       modify_expr
4545               : varname '=' rhs
4546               | '*' ID '=' rhs
4547
4548     PRE_P points to the list where side effects that must happen before
4549         *EXPR_P should be stored.
4550
4551     POST_P points to the list where side effects that must happen after
4552         *EXPR_P should be stored.
4553
4554     WANT_VALUE is nonzero iff we want to use the value of this expression
4555         in another expression.  */
4556
4557 static enum gimplify_status
4558 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4559                       bool want_value)
4560 {
4561   tree *from_p = &TREE_OPERAND (*expr_p, 1);
4562   tree *to_p = &TREE_OPERAND (*expr_p, 0);
4563   enum gimplify_status ret = GS_UNHANDLED;
4564   gimple assign;
4565   location_t loc = EXPR_LOCATION (*expr_p);
4566   gimple_stmt_iterator gsi;
4567
4568   gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4569               || TREE_CODE (*expr_p) == INIT_EXPR);
4570
4571   /* Trying to simplify a clobber using normal logic doesn't work,
4572      so handle it here.  */
4573   if (TREE_CLOBBER_P (*from_p))
4574     {
4575       ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4576       if (ret == GS_ERROR)
4577         return ret;
4578       gcc_assert (!want_value
4579                   && (TREE_CODE (*to_p) == VAR_DECL
4580                       || TREE_CODE (*to_p) == MEM_REF));
4581       gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4582       *expr_p = NULL;
4583       return GS_ALL_DONE;
4584     }
4585
4586   /* Insert pointer conversions required by the middle-end that are not
4587      required by the frontend.  This fixes middle-end type checking for
4588      for example gcc.dg/redecl-6.c.  */
4589   if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4590     {
4591       STRIP_USELESS_TYPE_CONVERSION (*from_p);
4592       if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4593         *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4594     }
4595
4596   /* See if any simplifications can be done based on what the RHS is.  */
4597   ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4598                                   want_value);
4599   if (ret != GS_UNHANDLED)
4600     return ret;
4601
4602   /* For zero sized types only gimplify the left hand side and right hand
4603      side as statements and throw away the assignment.  Do this after
4604      gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4605      types properly.  */
4606   if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4607     {
4608       gimplify_stmt (from_p, pre_p);
4609       gimplify_stmt (to_p, pre_p);
4610       *expr_p = NULL_TREE;
4611       return GS_ALL_DONE;
4612     }
4613
4614   /* If the value being copied is of variable width, compute the length
4615      of the copy into a WITH_SIZE_EXPR.   Note that we need to do this
4616      before gimplifying any of the operands so that we can resolve any
4617      PLACEHOLDER_EXPRs in the size.  Also note that the RTL expander uses
4618      the size of the expression to be copied, not of the destination, so
4619      that is what we must do here.  */
4620   maybe_with_size_expr (from_p);
4621
4622   ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4623   if (ret == GS_ERROR)
4624     return ret;
4625
4626   /* As a special case, we have to temporarily allow for assignments
4627      with a CALL_EXPR on the RHS.  Since in GIMPLE a function call is
4628      a toplevel statement, when gimplifying the GENERIC expression
4629      MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4630      GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4631
4632      Instead, we need to create the tuple GIMPLE_CALL <a, foo>.  To
4633      prevent gimplify_expr from trying to create a new temporary for
4634      foo's LHS, we tell it that it should only gimplify until it
4635      reaches the CALL_EXPR.  On return from gimplify_expr, the newly
4636      created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4637      and all we need to do here is set 'a' to be its LHS.  */
4638   ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4639                        fb_rvalue);
4640   if (ret == GS_ERROR)
4641     return ret;
4642
4643   /* Now see if the above changed *from_p to something we handle specially.  */
4644   ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4645                                   want_value);
4646   if (ret != GS_UNHANDLED)
4647     return ret;
4648
4649   /* If we've got a variable sized assignment between two lvalues (i.e. does
4650      not involve a call), then we can make things a bit more straightforward
4651      by converting the assignment to memcpy or memset.  */
4652   if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4653     {
4654       tree from = TREE_OPERAND (*from_p, 0);
4655       tree size = TREE_OPERAND (*from_p, 1);
4656
4657       if (TREE_CODE (from) == CONSTRUCTOR)
4658         return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4659
4660       if (is_gimple_addressable (from))
4661         {
4662           *from_p = from;
4663           return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4664                                                  pre_p);
4665         }
4666     }
4667
4668   /* Transform partial stores to non-addressable complex variables into
4669      total stores.  This allows us to use real instead of virtual operands
4670      for these variables, which improves optimization.  */
4671   if ((TREE_CODE (*to_p) == REALPART_EXPR
4672        || TREE_CODE (*to_p) == IMAGPART_EXPR)
4673       && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4674     return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4675
4676   /* Try to alleviate the effects of the gimplification creating artificial
4677      temporaries (see for example is_gimple_reg_rhs) on the debug info.  */
4678   if (!gimplify_ctxp->into_ssa
4679       && TREE_CODE (*from_p) == VAR_DECL
4680       && DECL_IGNORED_P (*from_p)
4681       && DECL_P (*to_p)
4682       && !DECL_IGNORED_P (*to_p))
4683     {
4684       if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4685         DECL_NAME (*from_p)
4686           = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4687       DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4688       SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4689    }
4690
4691   if (want_value && TREE_THIS_VOLATILE (*to_p))
4692     *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4693
4694   if (TREE_CODE (*from_p) == CALL_EXPR)
4695     {
4696       /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4697          instead of a GIMPLE_ASSIGN.  */
4698       gcall *call_stmt;
4699       if (CALL_EXPR_FN (*from_p) == NULL_TREE)
4700         {
4701           /* Gimplify internal functions created in the FEs.  */
4702           int nargs = call_expr_nargs (*from_p), i;
4703           enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
4704           auto_vec<tree> vargs (nargs);
4705
4706           for (i = 0; i < nargs; i++)
4707             {
4708               gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
4709                             EXPR_LOCATION (*from_p));
4710               vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
4711             }
4712           call_stmt = gimple_build_call_internal_vec (ifn, vargs);
4713           gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
4714         }
4715       else
4716         {
4717           tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4718           CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4719           STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4720           tree fndecl = get_callee_fndecl (*from_p);
4721           if (fndecl
4722               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4723               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4724               && call_expr_nargs (*from_p) == 3)
4725             call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4726                                                     CALL_EXPR_ARG (*from_p, 0),
4727                                                     CALL_EXPR_ARG (*from_p, 1),
4728                                                     CALL_EXPR_ARG (*from_p, 2));
4729           else
4730             {
4731               call_stmt = gimple_build_call_from_tree (*from_p);
4732               gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
4733             }
4734         }
4735       notice_special_calls (call_stmt);
4736       if (!gimple_call_noreturn_p (call_stmt))
4737         gimple_call_set_lhs (call_stmt, *to_p);
4738       assign = call_stmt;
4739     }
4740   else
4741     {
4742       assign = gimple_build_assign (*to_p, *from_p);
4743       gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4744     }
4745
4746   if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4747     {
4748       /* We should have got an SSA name from the start.  */
4749       gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4750     }
4751
4752   gimplify_seq_add_stmt (pre_p, assign);
4753   gsi = gsi_last (*pre_p);
4754   maybe_fold_stmt (&gsi);
4755
4756   if (want_value)
4757     {
4758       *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4759       return GS_OK;
4760     }
4761   else
4762     *expr_p = NULL;
4763
4764   return GS_ALL_DONE;
4765 }
4766
4767 /* Gimplify a comparison between two variable-sized objects.  Do this
4768    with a call to BUILT_IN_MEMCMP.  */
4769
4770 static enum gimplify_status
4771 gimplify_variable_sized_compare (tree *expr_p)
4772 {
4773   location_t loc = EXPR_LOCATION (*expr_p);
4774   tree op0 = TREE_OPERAND (*expr_p, 0);
4775   tree op1 = TREE_OPERAND (*expr_p, 1);
4776   tree t, arg, dest, src, expr;
4777
4778   arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4779   arg = unshare_expr (arg);
4780   arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4781   src = build_fold_addr_expr_loc (loc, op1);
4782   dest = build_fold_addr_expr_loc (loc, op0);
4783   t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4784   t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4785
4786   expr
4787     = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4788   SET_EXPR_LOCATION (expr, loc);
4789   *expr_p = expr;
4790
4791   return GS_OK;
4792 }
4793
4794 /* Gimplify a comparison between two aggregate objects of integral scalar
4795    mode as a comparison between the bitwise equivalent scalar values.  */
4796
4797 static enum gimplify_status
4798 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4799 {
4800   location_t loc = EXPR_LOCATION (*expr_p);
4801   tree op0 = TREE_OPERAND (*expr_p, 0);
4802   tree op1 = TREE_OPERAND (*expr_p, 1);
4803
4804   tree type = TREE_TYPE (op0);
4805   tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4806
4807   op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4808   op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4809
4810   *expr_p
4811     = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4812
4813   return GS_OK;
4814 }
4815
4816 /* Gimplify an expression sequence.  This function gimplifies each
4817    expression and rewrites the original expression with the last
4818    expression of the sequence in GIMPLE form.
4819
4820    PRE_P points to the list where the side effects for all the
4821        expressions in the sequence will be emitted.
4822
4823    WANT_VALUE is true when the result of the last COMPOUND_EXPR is used.  */
4824
4825 static enum gimplify_status
4826 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4827 {
4828   tree t = *expr_p;
4829
4830   do
4831     {
4832       tree *sub_p = &TREE_OPERAND (t, 0);
4833
4834       if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4835         gimplify_compound_expr (sub_p, pre_p, false);
4836       else
4837         gimplify_stmt (sub_p, pre_p);
4838
4839       t = TREE_OPERAND (t, 1);
4840     }
4841   while (TREE_CODE (t) == COMPOUND_EXPR);
4842
4843   *expr_p = t;
4844   if (want_value)
4845     return GS_OK;
4846   else
4847     {
4848       gimplify_stmt (expr_p, pre_p);
4849       return GS_ALL_DONE;
4850     }
4851 }
4852
4853 /* Gimplify a SAVE_EXPR node.  EXPR_P points to the expression to
4854    gimplify.  After gimplification, EXPR_P will point to a new temporary
4855    that holds the original value of the SAVE_EXPR node.
4856
4857    PRE_P points to the list where side effects that must happen before
4858    *EXPR_P should be stored.  */
4859
4860 static enum gimplify_status
4861 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4862 {
4863   enum gimplify_status ret = GS_ALL_DONE;
4864   tree val;
4865
4866   gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4867   val = TREE_OPERAND (*expr_p, 0);
4868
4869   /* If the SAVE_EXPR has not been resolved, then evaluate it once.  */
4870   if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4871     {
4872       /* The operand may be a void-valued expression such as SAVE_EXPRs
4873          generated by the Java frontend for class initialization.  It is
4874          being executed only for its side-effects.  */
4875       if (TREE_TYPE (val) == void_type_node)
4876         {
4877           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4878                                is_gimple_stmt, fb_none);
4879           val = NULL;
4880         }
4881       else
4882         val = get_initialized_tmp_var (val, pre_p, post_p);
4883
4884       TREE_OPERAND (*expr_p, 0) = val;
4885       SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4886     }
4887
4888   *expr_p = val;
4889
4890   return ret;
4891 }
4892
4893 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4894
4895       unary_expr
4896               : ...
4897               | '&' varname
4898               ...
4899
4900     PRE_P points to the list where side effects that must happen before
4901         *EXPR_P should be stored.
4902
4903     POST_P points to the list where side effects that must happen after
4904         *EXPR_P should be stored.  */
4905
4906 static enum gimplify_status
4907 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4908 {
4909   tree expr = *expr_p;
4910   tree op0 = TREE_OPERAND (expr, 0);
4911   enum gimplify_status ret;
4912   location_t loc = EXPR_LOCATION (*expr_p);
4913
4914   switch (TREE_CODE (op0))
4915     {
4916     case INDIRECT_REF:
4917     do_indirect_ref:
4918       /* Check if we are dealing with an expression of the form '&*ptr'.
4919          While the front end folds away '&*ptr' into 'ptr', these
4920          expressions may be generated internally by the compiler (e.g.,
4921          builtins like __builtin_va_end).  */
4922       /* Caution: the silent array decomposition semantics we allow for
4923          ADDR_EXPR means we can't always discard the pair.  */
4924       /* Gimplification of the ADDR_EXPR operand may drop
4925          cv-qualification conversions, so make sure we add them if
4926          needed.  */
4927       {
4928         tree op00 = TREE_OPERAND (op0, 0);
4929         tree t_expr = TREE_TYPE (expr);
4930         tree t_op00 = TREE_TYPE (op00);
4931
4932         if (!useless_type_conversion_p (t_expr, t_op00))
4933           op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4934         *expr_p = op00;
4935         ret = GS_OK;
4936       }
4937       break;
4938
4939     case VIEW_CONVERT_EXPR:
4940       /* Take the address of our operand and then convert it to the type of
4941          this ADDR_EXPR.
4942
4943          ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4944          all clear.  The impact of this transformation is even less clear.  */
4945
4946       /* If the operand is a useless conversion, look through it.  Doing so
4947          guarantees that the ADDR_EXPR and its operand will remain of the
4948          same type.  */
4949       if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4950         op0 = TREE_OPERAND (op0, 0);
4951
4952       *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4953                                   build_fold_addr_expr_loc (loc,
4954                                                         TREE_OPERAND (op0, 0)));
4955       ret = GS_OK;
4956       break;
4957
4958     default:
4959       /* If we see a call to a declared builtin or see its address
4960          being taken (we can unify those cases here) then we can mark
4961          the builtin for implicit generation by GCC.  */
4962       if (TREE_CODE (op0) == FUNCTION_DECL
4963           && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
4964           && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
4965         set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
4966
4967       /* We use fb_either here because the C frontend sometimes takes
4968          the address of a call that returns a struct; see
4969          gcc.dg/c99-array-lval-1.c.  The gimplifier will correctly make
4970          the implied temporary explicit.  */
4971
4972       /* Make the operand addressable.  */
4973       ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4974                            is_gimple_addressable, fb_either);
4975       if (ret == GS_ERROR)
4976         break;
4977
4978       /* Then mark it.  Beware that it may not be possible to do so directly
4979          if a temporary has been created by the gimplification.  */
4980       prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4981
4982       op0 = TREE_OPERAND (expr, 0);
4983
4984       /* For various reasons, the gimplification of the expression
4985          may have made a new INDIRECT_REF.  */
4986       if (TREE_CODE (op0) == INDIRECT_REF)
4987         goto do_indirect_ref;
4988
4989       mark_addressable (TREE_OPERAND (expr, 0));
4990
4991       /* The FEs may end up building ADDR_EXPRs early on a decl with
4992          an incomplete type.  Re-build ADDR_EXPRs in canonical form
4993          here.  */
4994       if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4995         *expr_p = build_fold_addr_expr (op0);
4996
4997       /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly.  */
4998       recompute_tree_invariant_for_addr_expr (*expr_p);
4999
5000       /* If we re-built the ADDR_EXPR add a conversion to the original type
5001          if required.  */
5002       if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5003         *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5004
5005       break;
5006     }
5007
5008   return ret;
5009 }
5010
5011 /* Gimplify the operands of an ASM_EXPR.  Input operands should be a gimple
5012    value; output operands should be a gimple lvalue.  */
5013
5014 static enum gimplify_status
5015 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5016 {
5017   tree expr;
5018   int noutputs;
5019   const char **oconstraints;
5020   int i;
5021   tree link;
5022   const char *constraint;
5023   bool allows_mem, allows_reg, is_inout;
5024   enum gimplify_status ret, tret;
5025   gasm *stmt;
5026   vec<tree, va_gc> *inputs;
5027   vec<tree, va_gc> *outputs;
5028   vec<tree, va_gc> *clobbers;
5029   vec<tree, va_gc> *labels;
5030   tree link_next;
5031
5032   expr = *expr_p;
5033   noutputs = list_length (ASM_OUTPUTS (expr));
5034   oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5035
5036   inputs = NULL;
5037   outputs = NULL;
5038   clobbers = NULL;
5039   labels = NULL;
5040
5041   ret = GS_ALL_DONE;
5042   link_next = NULL_TREE;
5043   for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5044     {
5045       bool ok;
5046       size_t constraint_len;
5047
5048       link_next = TREE_CHAIN (link);
5049
5050       oconstraints[i]
5051         = constraint
5052         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5053       constraint_len = strlen (constraint);
5054       if (constraint_len == 0)
5055         continue;
5056
5057       ok = parse_output_constraint (&constraint, i, 0, 0,
5058                                     &allows_mem, &allows_reg, &is_inout);
5059       if (!ok)
5060         {
5061           ret = GS_ERROR;
5062           is_inout = false;
5063         }
5064
5065       if (!allows_reg && allows_mem)
5066         mark_addressable (TREE_VALUE (link));
5067
5068       tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5069                             is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5070                             fb_lvalue | fb_mayfail);
5071       if (tret == GS_ERROR)
5072         {
5073           error ("invalid lvalue in asm output %d", i);
5074           ret = tret;
5075         }
5076
5077       vec_safe_push (outputs, link);
5078       TREE_CHAIN (link) = NULL_TREE;
5079
5080       if (is_inout)
5081         {
5082           /* An input/output operand.  To give the optimizers more
5083              flexibility, split it into separate input and output
5084              operands.  */
5085           tree input;
5086           char buf[10];
5087
5088           /* Turn the in/out constraint into an output constraint.  */
5089           char *p = xstrdup (constraint);
5090           p[0] = '=';
5091           TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5092
5093           /* And add a matching input constraint.  */
5094           if (allows_reg)
5095             {
5096               sprintf (buf, "%d", i);
5097
5098               /* If there are multiple alternatives in the constraint,
5099                  handle each of them individually.  Those that allow register
5100                  will be replaced with operand number, the others will stay
5101                  unchanged.  */
5102               if (strchr (p, ',') != NULL)
5103                 {
5104                   size_t len = 0, buflen = strlen (buf);
5105                   char *beg, *end, *str, *dst;
5106
5107                   for (beg = p + 1;;)
5108                     {
5109                       end = strchr (beg, ',');
5110                       if (end == NULL)
5111                         end = strchr (beg, '\0');
5112                       if ((size_t) (end - beg) < buflen)
5113                         len += buflen + 1;
5114                       else
5115                         len += end - beg + 1;
5116                       if (*end)
5117                         beg = end + 1;
5118                       else
5119                         break;
5120                     }
5121
5122                   str = (char *) alloca (len);
5123                   for (beg = p + 1, dst = str;;)
5124                     {
5125                       const char *tem;
5126                       bool mem_p, reg_p, inout_p;
5127
5128                       end = strchr (beg, ',');
5129                       if (end)
5130                         *end = '\0';
5131                       beg[-1] = '=';
5132                       tem = beg - 1;
5133                       parse_output_constraint (&tem, i, 0, 0,
5134                                                &mem_p, &reg_p, &inout_p);
5135                       if (dst != str)
5136                         *dst++ = ',';
5137                       if (reg_p)
5138                         {
5139                           memcpy (dst, buf, buflen);
5140                           dst += buflen;
5141                         }
5142                       else
5143                         {
5144                           if (end)
5145                             len = end - beg;
5146                           else
5147                             len = strlen (beg);
5148                           memcpy (dst, beg, len);
5149                           dst += len;
5150                         }
5151                       if (end)
5152                         beg = end + 1;
5153                       else
5154                         break;
5155                     }
5156                   *dst = '\0';
5157                   input = build_string (dst - str, str);
5158                 }
5159               else
5160                 input = build_string (strlen (buf), buf);
5161             }
5162           else
5163             input = build_string (constraint_len - 1, constraint + 1);
5164
5165           free (p);
5166
5167           input = build_tree_list (build_tree_list (NULL_TREE, input),
5168                                    unshare_expr (TREE_VALUE (link)));
5169           ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5170         }
5171     }
5172
5173   link_next = NULL_TREE;
5174   for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5175     {
5176       link_next = TREE_CHAIN (link);
5177       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5178       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5179                               oconstraints, &allows_mem, &allows_reg);
5180
5181       /* If we can't make copies, we can only accept memory.  */
5182       if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5183         {
5184           if (allows_mem)
5185             allows_reg = 0;
5186           else
5187             {
5188               error ("impossible constraint in %<asm%>");
5189               error ("non-memory input %d must stay in memory", i);
5190               return GS_ERROR;
5191             }
5192         }
5193
5194       /* If the operand is a memory input, it should be an lvalue.  */
5195       if (!allows_reg && allows_mem)
5196         {
5197           tree inputv = TREE_VALUE (link);
5198           STRIP_NOPS (inputv);
5199           if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5200               || TREE_CODE (inputv) == PREINCREMENT_EXPR
5201               || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5202               || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5203             TREE_VALUE (link) = error_mark_node;
5204           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5205                                 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5206           mark_addressable (TREE_VALUE (link));
5207           if (tret == GS_ERROR)
5208             {
5209               if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5210                 input_location = EXPR_LOCATION (TREE_VALUE (link));
5211               error ("memory input %d is not directly addressable", i);
5212               ret = tret;
5213             }
5214         }
5215       else
5216         {
5217           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5218                                 is_gimple_asm_val, fb_rvalue);
5219           if (tret == GS_ERROR)
5220             ret = tret;
5221         }
5222
5223       TREE_CHAIN (link) = NULL_TREE;
5224       vec_safe_push (inputs, link);
5225     }
5226
5227   link_next = NULL_TREE;
5228   for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5229     {
5230       link_next = TREE_CHAIN (link);
5231       TREE_CHAIN (link) = NULL_TREE;
5232       vec_safe_push (clobbers, link);
5233     }
5234
5235   link_next = NULL_TREE;
5236   for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5237     {
5238       link_next = TREE_CHAIN (link);
5239       TREE_CHAIN (link) = NULL_TREE;
5240       vec_safe_push (labels, link);
5241     }
5242
5243   /* Do not add ASMs with errors to the gimple IL stream.  */
5244   if (ret != GS_ERROR)
5245     {
5246       stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5247                                    inputs, outputs, clobbers, labels);
5248
5249       gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5250       gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5251
5252       gimplify_seq_add_stmt (pre_p, stmt);
5253     }
5254
5255   return ret;
5256 }
5257
5258 /* Gimplify a CLEANUP_POINT_EXPR.  Currently this works by adding
5259    GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5260    gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5261    return to this function.
5262
5263    FIXME should we complexify the prequeue handling instead?  Or use flags
5264    for all the cleanups and let the optimizer tighten them up?  The current
5265    code seems pretty fragile; it will break on a cleanup within any
5266    non-conditional nesting.  But any such nesting would be broken, anyway;
5267    we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5268    and continues out of it.  We can do that at the RTL level, though, so
5269    having an optimizer to tighten up try/finally regions would be a Good
5270    Thing.  */
5271
5272 static enum gimplify_status
5273 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5274 {
5275   gimple_stmt_iterator iter;
5276   gimple_seq body_sequence = NULL;
5277
5278   tree temp = voidify_wrapper_expr (*expr_p, NULL);
5279
5280   /* We only care about the number of conditions between the innermost
5281      CLEANUP_POINT_EXPR and the cleanup.  So save and reset the count and
5282      any cleanups collected outside the CLEANUP_POINT_EXPR.  */
5283   int old_conds = gimplify_ctxp->conditions;
5284   gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5285   bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5286   gimplify_ctxp->conditions = 0;
5287   gimplify_ctxp->conditional_cleanups = NULL;
5288   gimplify_ctxp->in_cleanup_point_expr = true;
5289
5290   gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5291
5292   gimplify_ctxp->conditions = old_conds;
5293   gimplify_ctxp->conditional_cleanups = old_cleanups;
5294   gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5295
5296   for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5297     {
5298       gimple wce = gsi_stmt (iter);
5299
5300       if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5301         {
5302           if (gsi_one_before_end_p (iter))
5303             {
5304               /* Note that gsi_insert_seq_before and gsi_remove do not
5305                  scan operands, unlike some other sequence mutators.  */
5306               if (!gimple_wce_cleanup_eh_only (wce))
5307                 gsi_insert_seq_before_without_update (&iter,
5308                                                       gimple_wce_cleanup (wce),
5309                                                       GSI_SAME_STMT);
5310               gsi_remove (&iter, true);
5311               break;
5312             }
5313           else
5314             {
5315               gtry *gtry;
5316               gimple_seq seq;
5317               enum gimple_try_flags kind;
5318
5319               if (gimple_wce_cleanup_eh_only (wce))
5320                 kind = GIMPLE_TRY_CATCH;
5321               else
5322                 kind = GIMPLE_TRY_FINALLY;
5323               seq = gsi_split_seq_after (iter);
5324
5325               gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5326               /* Do not use gsi_replace here, as it may scan operands.
5327                  We want to do a simple structural modification only.  */
5328               gsi_set_stmt (&iter, gtry);
5329               iter = gsi_start (gtry->eval);
5330             }
5331         }
5332       else
5333         gsi_next (&iter);
5334     }
5335
5336   gimplify_seq_add_seq (pre_p, body_sequence);
5337   if (temp)
5338     {
5339       *expr_p = temp;
5340       return GS_OK;
5341     }
5342   else
5343     {
5344       *expr_p = NULL;
5345       return GS_ALL_DONE;
5346     }
5347 }
5348
5349 /* Insert a cleanup marker for gimplify_cleanup_point_expr.  CLEANUP
5350    is the cleanup action required.  EH_ONLY is true if the cleanup should
5351    only be executed if an exception is thrown, not on normal exit.  */
5352
5353 static void
5354 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5355 {
5356   gimple wce;
5357   gimple_seq cleanup_stmts = NULL;
5358
5359   /* Errors can result in improperly nested cleanups.  Which results in
5360      confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR.  */
5361   if (seen_error ())
5362     return;
5363
5364   if (gimple_conditional_context ())
5365     {
5366       /* If we're in a conditional context, this is more complex.  We only
5367          want to run the cleanup if we actually ran the initialization that
5368          necessitates it, but we want to run it after the end of the
5369          conditional context.  So we wrap the try/finally around the
5370          condition and use a flag to determine whether or not to actually
5371          run the destructor.  Thus
5372
5373            test ? f(A()) : 0
5374
5375          becomes (approximately)
5376
5377            flag = 0;
5378            try {
5379              if (test) { A::A(temp); flag = 1; val = f(temp); }
5380              else { val = 0; }
5381            } finally {
5382              if (flag) A::~A(temp);
5383            }
5384            val
5385       */
5386       tree flag = create_tmp_var (boolean_type_node, "cleanup");
5387       gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
5388       gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
5389
5390       cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5391       gimplify_stmt (&cleanup, &cleanup_stmts);
5392       wce = gimple_build_wce (cleanup_stmts);
5393
5394       gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5395       gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5396       gimplify_seq_add_stmt (pre_p, ftrue);
5397
5398       /* Because of this manipulation, and the EH edges that jump
5399          threading cannot redirect, the temporary (VAR) will appear
5400          to be used uninitialized.  Don't warn.  */
5401       TREE_NO_WARNING (var) = 1;
5402     }
5403   else
5404     {
5405       gimplify_stmt (&cleanup, &cleanup_stmts);
5406       wce = gimple_build_wce (cleanup_stmts);
5407       gimple_wce_set_cleanup_eh_only (wce, eh_only);
5408       gimplify_seq_add_stmt (pre_p, wce);
5409     }
5410 }
5411
5412 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR.  */
5413
5414 static enum gimplify_status
5415 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5416 {
5417   tree targ = *expr_p;
5418   tree temp = TARGET_EXPR_SLOT (targ);
5419   tree init = TARGET_EXPR_INITIAL (targ);
5420   enum gimplify_status ret;
5421
5422   if (init)
5423     {
5424       tree cleanup = NULL_TREE;
5425
5426       /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5427          to the temps list.  Handle also variable length TARGET_EXPRs.  */
5428       if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5429         {
5430           if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5431             gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5432           gimplify_vla_decl (temp, pre_p);
5433         }
5434       else
5435         gimple_add_tmp_var (temp);
5436
5437       /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5438          expression is supposed to initialize the slot.  */
5439       if (VOID_TYPE_P (TREE_TYPE (init)))
5440         ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5441       else
5442         {
5443           tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5444           init = init_expr;
5445           ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5446           init = NULL;
5447           ggc_free (init_expr);
5448         }
5449       if (ret == GS_ERROR)
5450         {
5451           /* PR c++/28266 Make sure this is expanded only once. */
5452           TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5453           return GS_ERROR;
5454         }
5455       if (init)
5456         gimplify_and_add (init, pre_p);
5457
5458       /* If needed, push the cleanup for the temp.  */
5459       if (TARGET_EXPR_CLEANUP (targ))
5460         {
5461           if (CLEANUP_EH_ONLY (targ))
5462             gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5463                                  CLEANUP_EH_ONLY (targ), pre_p);
5464           else
5465             cleanup = TARGET_EXPR_CLEANUP (targ);
5466         }
5467
5468       /* Add a clobber for the temporary going out of scope, like
5469          gimplify_bind_expr.  */
5470       if (gimplify_ctxp->in_cleanup_point_expr
5471           && needs_to_live_in_memory (temp)
5472           && flag_stack_reuse == SR_ALL)
5473         {
5474           tree clobber = build_constructor (TREE_TYPE (temp),
5475                                             NULL);
5476           TREE_THIS_VOLATILE (clobber) = true;
5477           clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5478           if (cleanup)
5479             cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5480                               clobber);
5481           else
5482             cleanup = clobber;
5483         }
5484
5485       if (cleanup)
5486         gimple_push_cleanup (temp, cleanup, false, pre_p);
5487
5488       /* Only expand this once.  */
5489       TREE_OPERAND (targ, 3) = init;
5490       TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5491     }
5492   else
5493     /* We should have expanded this before.  */
5494     gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5495
5496   *expr_p = temp;
5497   return GS_OK;
5498 }
5499
5500 /* Gimplification of expression trees.  */
5501
5502 /* Gimplify an expression which appears at statement context.  The
5503    corresponding GIMPLE statements are added to *SEQ_P.  If *SEQ_P is
5504    NULL, a new sequence is allocated.
5505
5506    Return true if we actually added a statement to the queue.  */
5507
5508 bool
5509 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5510 {
5511   gimple_seq_node last;
5512
5513   last = gimple_seq_last (*seq_p);
5514   gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5515   return last != gimple_seq_last (*seq_p);
5516 }
5517
5518 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5519    to CTX.  If entries already exist, force them to be some flavor of private.
5520    If there is no enclosing parallel, do nothing.  */
5521
5522 void
5523 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5524 {
5525   splay_tree_node n;
5526
5527   if (decl == NULL || !DECL_P (decl))
5528     return;
5529
5530   do
5531     {
5532       n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5533       if (n != NULL)
5534         {
5535           if (n->value & GOVD_SHARED)
5536             n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5537           else if (n->value & GOVD_MAP)
5538             n->value |= GOVD_MAP_TO_ONLY;
5539           else
5540             return;
5541         }
5542       else if (ctx->region_type == ORT_TARGET)
5543         omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5544       else if (ctx->region_type != ORT_WORKSHARE
5545                && ctx->region_type != ORT_SIMD
5546                && ctx->region_type != ORT_TARGET_DATA)
5547         omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5548
5549       ctx = ctx->outer_context;
5550     }
5551   while (ctx);
5552 }
5553
5554 /* Similarly for each of the type sizes of TYPE.  */
5555
5556 static void
5557 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5558 {
5559   if (type == NULL || type == error_mark_node)
5560     return;
5561   type = TYPE_MAIN_VARIANT (type);
5562
5563   if (ctx->privatized_types->add (type))
5564     return;
5565
5566   switch (TREE_CODE (type))
5567     {
5568     case INTEGER_TYPE:
5569     case ENUMERAL_TYPE:
5570     case BOOLEAN_TYPE:
5571     case REAL_TYPE:
5572     case FIXED_POINT_TYPE:
5573       omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5574       omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5575       break;
5576
5577     case ARRAY_TYPE:
5578       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5579       omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5580       break;
5581
5582     case RECORD_TYPE:
5583     case UNION_TYPE:
5584     case QUAL_UNION_TYPE:
5585       {
5586         tree field;
5587         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5588           if (TREE_CODE (field) == FIELD_DECL)
5589             {
5590               omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5591               omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5592             }
5593       }
5594       break;
5595
5596     case POINTER_TYPE:
5597     case REFERENCE_TYPE:
5598       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5599       break;
5600
5601     default:
5602       break;
5603     }
5604
5605   omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5606   omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5607   lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5608 }
5609
5610 /* Add an entry for DECL in the OMP context CTX with FLAGS.  */
5611
5612 static void
5613 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5614 {
5615   splay_tree_node n;
5616   unsigned int nflags;
5617   tree t;
5618
5619   if (error_operand_p (decl))
5620     return;
5621
5622   /* Never elide decls whose type has TREE_ADDRESSABLE set.  This means
5623      there are constructors involved somewhere.  */
5624   if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5625       || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5626     flags |= GOVD_SEEN;
5627
5628   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5629   if (n != NULL && n->value != GOVD_ALIGNED)
5630     {
5631       /* We shouldn't be re-adding the decl with the same data
5632          sharing class.  */
5633       gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5634       /* The only combination of data sharing classes we should see is
5635          FIRSTPRIVATE and LASTPRIVATE.  */
5636       nflags = n->value | flags;
5637       gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5638                   == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5639                   || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5640       n->value = nflags;
5641       return;
5642     }
5643
5644   /* When adding a variable-sized variable, we have to handle all sorts
5645      of additional bits of data: the pointer replacement variable, and
5646      the parameters of the type.  */
5647   if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5648     {
5649       /* Add the pointer replacement variable as PRIVATE if the variable
5650          replacement is private, else FIRSTPRIVATE since we'll need the
5651          address of the original variable either for SHARED, or for the
5652          copy into or out of the context.  */
5653       if (!(flags & GOVD_LOCAL))
5654         {
5655           if (flags & GOVD_MAP)
5656             nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5657           else if (flags & GOVD_PRIVATE)
5658             nflags = GOVD_PRIVATE;
5659           else
5660             nflags = GOVD_FIRSTPRIVATE;
5661           nflags |= flags & GOVD_SEEN;
5662           t = DECL_VALUE_EXPR (decl);
5663           gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5664           t = TREE_OPERAND (t, 0);
5665           gcc_assert (DECL_P (t));
5666           omp_add_variable (ctx, t, nflags);
5667         }
5668
5669       /* Add all of the variable and type parameters (which should have
5670          been gimplified to a formal temporary) as FIRSTPRIVATE.  */
5671       omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5672       omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5673       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5674
5675       /* The variable-sized variable itself is never SHARED, only some form
5676          of PRIVATE.  The sharing would take place via the pointer variable
5677          which we remapped above.  */
5678       if (flags & GOVD_SHARED)
5679         flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5680                 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5681
5682       /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5683          alloca statement we generate for the variable, so make sure it
5684          is available.  This isn't automatically needed for the SHARED
5685          case, since we won't be allocating local storage then.
5686          For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5687          in this case omp_notice_variable will be called later
5688          on when it is gimplified.  */
5689       else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5690                && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5691         omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5692     }
5693   else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5694            && lang_hooks.decls.omp_privatize_by_reference (decl))
5695     {
5696       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5697
5698       /* Similar to the direct variable sized case above, we'll need the
5699          size of references being privatized.  */
5700       if ((flags & GOVD_SHARED) == 0)
5701         {
5702           t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5703           if (TREE_CODE (t) != INTEGER_CST)
5704             omp_notice_variable (ctx, t, true);
5705         }
5706     }
5707
5708   if (n != NULL)
5709     n->value |= flags;
5710   else
5711     splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5712 }
5713
5714 /* Notice a threadprivate variable DECL used in OMP context CTX.
5715    This just prints out diagnostics about threadprivate variable uses
5716    in untied tasks.  If DECL2 is non-NULL, prevent this warning
5717    on that variable.  */
5718
5719 static bool
5720 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5721                                    tree decl2)
5722 {
5723   splay_tree_node n;
5724   struct gimplify_omp_ctx *octx;
5725
5726   for (octx = ctx; octx; octx = octx->outer_context)
5727     if (octx->region_type == ORT_TARGET)
5728       {
5729         n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5730         if (n == NULL)
5731           {
5732             error ("threadprivate variable %qE used in target region",
5733                    DECL_NAME (decl));
5734             error_at (octx->location, "enclosing target region");
5735             splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5736           }
5737         if (decl2)
5738           splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5739       }
5740
5741   if (ctx->region_type != ORT_UNTIED_TASK)
5742     return false;
5743   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5744   if (n == NULL)
5745     {
5746       error ("threadprivate variable %qE used in untied task",
5747              DECL_NAME (decl));
5748       error_at (ctx->location, "enclosing task");
5749       splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5750     }
5751   if (decl2)
5752     splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5753   return false;
5754 }
5755
5756 /* Record the fact that DECL was used within the OMP context CTX.
5757    IN_CODE is true when real code uses DECL, and false when we should
5758    merely emit default(none) errors.  Return true if DECL is going to
5759    be remapped and thus DECL shouldn't be gimplified into its
5760    DECL_VALUE_EXPR (if any).  */
5761
5762 static bool
5763 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5764 {
5765   splay_tree_node n;
5766   unsigned flags = in_code ? GOVD_SEEN : 0;
5767   bool ret = false, shared;
5768
5769   if (error_operand_p (decl))
5770     return false;
5771
5772   /* Threadprivate variables are predetermined.  */
5773   if (is_global_var (decl))
5774     {
5775       if (DECL_THREAD_LOCAL_P (decl))
5776         return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5777
5778       if (DECL_HAS_VALUE_EXPR_P (decl))
5779         {
5780           tree value = get_base_address (DECL_VALUE_EXPR (decl));
5781
5782           if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5783             return omp_notice_threadprivate_variable (ctx, decl, value);
5784         }
5785     }
5786
5787   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5788   if (ctx->region_type == ORT_TARGET)
5789     {
5790       ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5791       if (n == NULL)
5792         {
5793           if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5794             {
5795               error ("%qD referenced in target region does not have "
5796                      "a mappable type", decl);
5797               omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5798             }
5799           else
5800             omp_add_variable (ctx, decl, GOVD_MAP | flags);
5801         }
5802       else
5803         {
5804           /* If nothing changed, there's nothing left to do.  */
5805           if ((n->value & flags) == flags)
5806             return ret;
5807           n->value |= flags;
5808         }
5809       goto do_outer;
5810     }
5811
5812   if (n == NULL)
5813     {
5814       enum omp_clause_default_kind default_kind, kind;
5815       struct gimplify_omp_ctx *octx;
5816
5817       if (ctx->region_type == ORT_WORKSHARE
5818           || ctx->region_type == ORT_SIMD
5819           || ctx->region_type == ORT_TARGET_DATA)
5820         goto do_outer;
5821
5822       /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5823          remapped firstprivate instead of shared.  To some extent this is
5824          addressed in omp_firstprivatize_type_sizes, but not effectively.  */
5825       default_kind = ctx->default_kind;
5826       kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5827       if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5828         default_kind = kind;
5829
5830       switch (default_kind)
5831         {
5832         case OMP_CLAUSE_DEFAULT_NONE:
5833           if ((ctx->region_type & ORT_PARALLEL) != 0)
5834             {
5835               error ("%qE not specified in enclosing parallel",
5836                      DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5837               error_at (ctx->location, "enclosing parallel");
5838             }
5839           else if ((ctx->region_type & ORT_TASK) != 0)
5840             {
5841               error ("%qE not specified in enclosing task",
5842                      DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5843               error_at (ctx->location, "enclosing task");
5844             }
5845           else if (ctx->region_type == ORT_TEAMS)
5846             {
5847               error ("%qE not specified in enclosing teams construct",
5848                      DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5849               error_at (ctx->location, "enclosing teams construct");
5850             }
5851           else
5852             gcc_unreachable ();
5853           /* FALLTHRU */
5854         case OMP_CLAUSE_DEFAULT_SHARED:
5855           flags |= GOVD_SHARED;
5856           break;
5857         case OMP_CLAUSE_DEFAULT_PRIVATE:
5858           flags |= GOVD_PRIVATE;
5859           break;
5860         case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5861           flags |= GOVD_FIRSTPRIVATE;
5862           break;
5863         case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5864           /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED.  */
5865           gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5866           if (ctx->outer_context)
5867             omp_notice_variable (ctx->outer_context, decl, in_code);
5868           for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5869             {
5870               splay_tree_node n2;
5871
5872               if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5873                 continue;
5874               n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5875               if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5876                 {
5877                   flags |= GOVD_FIRSTPRIVATE;
5878                   break;
5879                 }
5880               if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5881                 break;
5882             }
5883           if (flags & GOVD_FIRSTPRIVATE)
5884             break;
5885           if (octx == NULL
5886               && (TREE_CODE (decl) == PARM_DECL
5887                   || (!is_global_var (decl)
5888                       && DECL_CONTEXT (decl) == current_function_decl)))
5889             {
5890               flags |= GOVD_FIRSTPRIVATE;
5891               break;
5892             }
5893           flags |= GOVD_SHARED;
5894           break;
5895         default:
5896           gcc_unreachable ();
5897         }
5898
5899       if ((flags & GOVD_PRIVATE)
5900           && lang_hooks.decls.omp_private_outer_ref (decl))
5901         flags |= GOVD_PRIVATE_OUTER_REF;
5902
5903       omp_add_variable (ctx, decl, flags);
5904
5905       shared = (flags & GOVD_SHARED) != 0;
5906       ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5907       goto do_outer;
5908     }
5909
5910   if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5911       && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5912       && DECL_SIZE (decl)
5913       && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5914     {
5915       splay_tree_node n2;
5916       tree t = DECL_VALUE_EXPR (decl);
5917       gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5918       t = TREE_OPERAND (t, 0);
5919       gcc_assert (DECL_P (t));
5920       n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5921       n2->value |= GOVD_SEEN;
5922     }
5923
5924   shared = ((flags | n->value) & GOVD_SHARED) != 0;
5925   ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5926
5927   /* If nothing changed, there's nothing left to do.  */
5928   if ((n->value & flags) == flags)
5929     return ret;
5930   flags |= n->value;
5931   n->value = flags;
5932
5933  do_outer:
5934   /* If the variable is private in the current context, then we don't
5935      need to propagate anything to an outer context.  */
5936   if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5937     return ret;
5938   if (ctx->outer_context
5939       && omp_notice_variable (ctx->outer_context, decl, in_code))
5940     return true;
5941   return ret;
5942 }
5943
5944 /* Verify that DECL is private within CTX.  If there's specific information
5945    to the contrary in the innermost scope, generate an error.  */
5946
5947 static bool
5948 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5949 {
5950   splay_tree_node n;
5951
5952   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5953   if (n != NULL)
5954     {
5955       if (n->value & GOVD_SHARED)
5956         {
5957           if (ctx == gimplify_omp_ctxp)
5958             {
5959               if (simd)
5960                 error ("iteration variable %qE is predetermined linear",
5961                        DECL_NAME (decl));
5962               else
5963                 error ("iteration variable %qE should be private",
5964                        DECL_NAME (decl));
5965               n->value = GOVD_PRIVATE;
5966               return true;
5967             }
5968           else
5969             return false;
5970         }
5971       else if ((n->value & GOVD_EXPLICIT) != 0
5972                && (ctx == gimplify_omp_ctxp
5973                    || (ctx->region_type == ORT_COMBINED_PARALLEL
5974                        && gimplify_omp_ctxp->outer_context == ctx)))
5975         {
5976           if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5977             error ("iteration variable %qE should not be firstprivate",
5978                    DECL_NAME (decl));
5979           else if ((n->value & GOVD_REDUCTION) != 0)
5980             error ("iteration variable %qE should not be reduction",
5981                    DECL_NAME (decl));
5982           else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5983             error ("iteration variable %qE should not be lastprivate",
5984                    DECL_NAME (decl));
5985           else if (simd && (n->value & GOVD_PRIVATE) != 0)
5986             error ("iteration variable %qE should not be private",
5987                    DECL_NAME (decl));
5988           else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5989             error ("iteration variable %qE is predetermined linear",
5990                    DECL_NAME (decl));
5991         }
5992       return (ctx == gimplify_omp_ctxp
5993               || (ctx->region_type == ORT_COMBINED_PARALLEL
5994                   && gimplify_omp_ctxp->outer_context == ctx));
5995     }
5996
5997   if (ctx->region_type != ORT_WORKSHARE
5998       && ctx->region_type != ORT_SIMD)
5999     return false;
6000   else if (ctx->outer_context)
6001     return omp_is_private (ctx->outer_context, decl, simd);
6002   return false;
6003 }
6004
6005 /* Return true if DECL is private within a parallel region
6006    that binds to the current construct's context or in parallel
6007    region's REDUCTION clause.  */
6008
6009 static bool
6010 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
6011 {
6012   splay_tree_node n;
6013
6014   do
6015     {
6016       ctx = ctx->outer_context;
6017       if (ctx == NULL)
6018         return !(is_global_var (decl)
6019                  /* References might be private, but might be shared too,
6020                     when checking for copyprivate, assume they might be
6021                     private, otherwise assume they might be shared.  */
6022                  || (!copyprivate
6023                      && lang_hooks.decls.omp_privatize_by_reference (decl)));
6024
6025       if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
6026         continue;
6027
6028       n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6029       if (n != NULL)
6030         return (n->value & GOVD_SHARED) == 0;
6031     }
6032   while (ctx->region_type == ORT_WORKSHARE
6033          || ctx->region_type == ORT_SIMD);
6034   return false;
6035 }
6036
6037 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
6038    and previous omp contexts.  */
6039
6040 static void
6041 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6042                            enum omp_region_type region_type)
6043 {
6044   struct gimplify_omp_ctx *ctx, *outer_ctx;
6045   tree c;
6046
6047   ctx = new_omp_context (region_type);
6048   outer_ctx = ctx->outer_context;
6049
6050   while ((c = *list_p) != NULL)
6051     {
6052       bool remove = false;
6053       bool notice_outer = true;
6054       const char *check_non_private = NULL;
6055       unsigned int flags;
6056       tree decl;
6057
6058       switch (OMP_CLAUSE_CODE (c))
6059         {
6060         case OMP_CLAUSE_PRIVATE:
6061           flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6062           if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6063             {
6064               flags |= GOVD_PRIVATE_OUTER_REF;
6065               OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6066             }
6067           else
6068             notice_outer = false;
6069           goto do_add;
6070         case OMP_CLAUSE_SHARED:
6071           flags = GOVD_SHARED | GOVD_EXPLICIT;
6072           goto do_add;
6073         case OMP_CLAUSE_FIRSTPRIVATE:
6074           flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6075           check_non_private = "firstprivate";
6076           goto do_add;
6077         case OMP_CLAUSE_LASTPRIVATE:
6078           flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6079           check_non_private = "lastprivate";
6080           goto do_add;
6081         case OMP_CLAUSE_REDUCTION:
6082           flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6083           check_non_private = "reduction";
6084           goto do_add;
6085         case OMP_CLAUSE_LINEAR:
6086           if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6087                              is_gimple_val, fb_rvalue) == GS_ERROR)
6088             {
6089               remove = true;
6090               break;
6091             }
6092           flags = GOVD_LINEAR | GOVD_EXPLICIT;
6093           goto do_add;
6094
6095         case OMP_CLAUSE_MAP:
6096           decl = OMP_CLAUSE_DECL (c);
6097           if (error_operand_p (decl))
6098             {
6099               remove = true;
6100               break;
6101             }
6102           if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6103             OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6104                                   : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6105           if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6106                              NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6107             {
6108               remove = true;
6109               break;
6110             }
6111           if (!DECL_P (decl))
6112             {
6113               if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6114                                  NULL, is_gimple_lvalue, fb_lvalue)
6115                   == GS_ERROR)
6116                 {
6117                   remove = true;
6118                   break;
6119                 }
6120               break;
6121             }
6122           flags = GOVD_MAP | GOVD_EXPLICIT;
6123           goto do_add;
6124
6125         case OMP_CLAUSE_DEPEND:
6126           if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6127             {
6128               gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6129                              NULL, is_gimple_val, fb_rvalue);
6130               OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6131             }
6132           if (error_operand_p (OMP_CLAUSE_DECL (c)))
6133             {
6134               remove = true;
6135               break;
6136             }
6137           OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6138           if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6139                              is_gimple_val, fb_rvalue) == GS_ERROR)
6140             {
6141               remove = true;
6142               break;
6143             }
6144           break;
6145
6146         case OMP_CLAUSE_TO:
6147         case OMP_CLAUSE_FROM:
6148         case OMP_CLAUSE__CACHE_:
6149           decl = OMP_CLAUSE_DECL (c);
6150           if (error_operand_p (decl))
6151             {
6152               remove = true;
6153               break;
6154             }
6155           if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6156             OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6157                                   : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6158           if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6159                              NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6160             {
6161               remove = true;
6162               break;
6163             }
6164           if (!DECL_P (decl))
6165             {
6166               if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6167                                  NULL, is_gimple_lvalue, fb_lvalue)
6168                   == GS_ERROR)
6169                 {
6170                   remove = true;
6171                   break;
6172                 }
6173               break;
6174             }
6175           goto do_notice;
6176
6177         do_add:
6178           decl = OMP_CLAUSE_DECL (c);
6179           if (error_operand_p (decl))
6180             {
6181               remove = true;
6182               break;
6183             }
6184           omp_add_variable (ctx, decl, flags);
6185           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6186               && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6187             {
6188               omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6189                                 GOVD_LOCAL | GOVD_SEEN);
6190               gimplify_omp_ctxp = ctx;
6191               push_gimplify_context ();
6192
6193               OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6194               OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6195
6196               gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6197                                 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6198               pop_gimplify_context
6199                 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6200               push_gimplify_context ();
6201               gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6202                                 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6203               pop_gimplify_context
6204                 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6205               OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6206               OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6207
6208               gimplify_omp_ctxp = outer_ctx;
6209             }
6210           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6211                    && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6212             {
6213               gimplify_omp_ctxp = ctx;
6214               push_gimplify_context ();
6215               if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6216                 {
6217                   tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6218                                       NULL, NULL);
6219                   TREE_SIDE_EFFECTS (bind) = 1;
6220                   BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6221                   OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6222                 }
6223               gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6224                                 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6225               pop_gimplify_context
6226                 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6227               OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6228
6229               gimplify_omp_ctxp = outer_ctx;
6230             }
6231           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6232                    && OMP_CLAUSE_LINEAR_STMT (c))
6233             {
6234               gimplify_omp_ctxp = ctx;
6235               push_gimplify_context ();
6236               if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6237                 {
6238                   tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6239                                       NULL, NULL);
6240                   TREE_SIDE_EFFECTS (bind) = 1;
6241                   BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6242                   OMP_CLAUSE_LINEAR_STMT (c) = bind;
6243                 }
6244               gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6245                                 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6246               pop_gimplify_context
6247                 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6248               OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6249
6250               gimplify_omp_ctxp = outer_ctx;
6251             }
6252           if (notice_outer)
6253             goto do_notice;
6254           break;
6255
6256         case OMP_CLAUSE_COPYIN:
6257         case OMP_CLAUSE_COPYPRIVATE:
6258           decl = OMP_CLAUSE_DECL (c);
6259           if (error_operand_p (decl))
6260             {
6261               remove = true;
6262               break;
6263             }
6264           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6265               && !remove
6266               && !omp_check_private (ctx, decl, true))
6267             {
6268               remove = true;
6269               if (is_global_var (decl))
6270                 {
6271                   if (DECL_THREAD_LOCAL_P (decl))
6272                     remove = false;
6273                   else if (DECL_HAS_VALUE_EXPR_P (decl))
6274                     {
6275                       tree value = get_base_address (DECL_VALUE_EXPR (decl));
6276
6277                       if (value
6278                           && DECL_P (value)
6279                           && DECL_THREAD_LOCAL_P (value))
6280                         remove = false;
6281                     }
6282                 }
6283               if (remove)
6284                 error_at (OMP_CLAUSE_LOCATION (c),
6285                           "copyprivate variable %qE is not threadprivate"
6286                           " or private in outer context", DECL_NAME (decl));
6287             }
6288         do_notice:
6289           if (outer_ctx)
6290             omp_notice_variable (outer_ctx, decl, true);
6291           if (check_non_private
6292               && region_type == ORT_WORKSHARE
6293               && omp_check_private (ctx, decl, false))
6294             {
6295               error ("%s variable %qE is private in outer context",
6296                      check_non_private, DECL_NAME (decl));
6297               remove = true;
6298             }
6299           break;
6300
6301         case OMP_CLAUSE_FINAL:
6302         case OMP_CLAUSE_IF:
6303           OMP_CLAUSE_OPERAND (c, 0)
6304             = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6305           /* Fall through.  */
6306
6307         case OMP_CLAUSE_SCHEDULE:
6308         case OMP_CLAUSE_NUM_THREADS:
6309         case OMP_CLAUSE_NUM_TEAMS:
6310         case OMP_CLAUSE_THREAD_LIMIT:
6311         case OMP_CLAUSE_DIST_SCHEDULE:
6312         case OMP_CLAUSE_DEVICE:
6313         case OMP_CLAUSE__CILK_FOR_COUNT_:
6314         case OMP_CLAUSE_ASYNC:
6315         case OMP_CLAUSE_WAIT:
6316         case OMP_CLAUSE_NUM_GANGS:
6317         case OMP_CLAUSE_NUM_WORKERS:
6318         case OMP_CLAUSE_VECTOR_LENGTH:
6319         case OMP_CLAUSE_GANG:
6320         case OMP_CLAUSE_WORKER:
6321         case OMP_CLAUSE_VECTOR:
6322           if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6323                              is_gimple_val, fb_rvalue) == GS_ERROR)
6324             remove = true;
6325           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_GANG
6326               && gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
6327                                 is_gimple_val, fb_rvalue) == GS_ERROR)
6328             remove = true;
6329           break;
6330
6331         case OMP_CLAUSE_DEVICE_RESIDENT:
6332         case OMP_CLAUSE_USE_DEVICE:
6333         case OMP_CLAUSE_INDEPENDENT:
6334           remove = true;
6335           break;
6336
6337         case OMP_CLAUSE_NOWAIT:
6338         case OMP_CLAUSE_ORDERED:
6339         case OMP_CLAUSE_UNTIED:
6340         case OMP_CLAUSE_COLLAPSE:
6341         case OMP_CLAUSE_AUTO:
6342         case OMP_CLAUSE_SEQ:
6343         case OMP_CLAUSE_MERGEABLE:
6344         case OMP_CLAUSE_PROC_BIND:
6345         case OMP_CLAUSE_SAFELEN:
6346           break;
6347
6348         case OMP_CLAUSE_ALIGNED:
6349           decl = OMP_CLAUSE_DECL (c);
6350           if (error_operand_p (decl))
6351             {
6352               remove = true;
6353               break;
6354             }
6355           if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6356                              is_gimple_val, fb_rvalue) == GS_ERROR)
6357             {
6358               remove = true;
6359               break;
6360             }
6361           if (!is_global_var (decl)
6362               && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6363             omp_add_variable (ctx, decl, GOVD_ALIGNED);
6364           break;
6365
6366         case OMP_CLAUSE_DEFAULT:
6367           ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6368           break;
6369
6370         default:
6371           gcc_unreachable ();
6372         }
6373
6374       if (remove)
6375         *list_p = OMP_CLAUSE_CHAIN (c);
6376       else
6377         list_p = &OMP_CLAUSE_CHAIN (c);
6378     }
6379
6380   gimplify_omp_ctxp = ctx;
6381 }
6382
6383 struct gimplify_adjust_omp_clauses_data
6384 {
6385   tree *list_p;
6386   gimple_seq *pre_p;
6387 };
6388
6389 /* For all variables that were not actually used within the context,
6390    remove PRIVATE, SHARED, and FIRSTPRIVATE clauses.  */
6391
6392 static int
6393 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6394 {
6395   tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6396   gimple_seq *pre_p
6397     = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6398   tree decl = (tree) n->key;
6399   unsigned flags = n->value;
6400   enum omp_clause_code code;
6401   tree clause;
6402   bool private_debug;
6403
6404   if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6405     return 0;
6406   if ((flags & GOVD_SEEN) == 0)
6407     return 0;
6408   if (flags & GOVD_DEBUG_PRIVATE)
6409     {
6410       gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6411       private_debug = true;
6412     }
6413   else if (flags & GOVD_MAP)
6414     private_debug = false;
6415   else
6416     private_debug
6417       = lang_hooks.decls.omp_private_debug_clause (decl,
6418                                                    !!(flags & GOVD_SHARED));
6419   if (private_debug)
6420     code = OMP_CLAUSE_PRIVATE;
6421   else if (flags & GOVD_MAP)
6422     code = OMP_CLAUSE_MAP;
6423   else if (flags & GOVD_SHARED)
6424     {
6425       if (is_global_var (decl))
6426         {
6427           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6428           while (ctx != NULL)
6429             {
6430               splay_tree_node on
6431                 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6432               if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6433                                       | GOVD_PRIVATE | GOVD_REDUCTION
6434                                       | GOVD_LINEAR | GOVD_MAP)) != 0)
6435                 break;
6436               ctx = ctx->outer_context;
6437             }
6438           if (ctx == NULL)
6439             return 0;
6440         }
6441       code = OMP_CLAUSE_SHARED;
6442     }
6443   else if (flags & GOVD_PRIVATE)
6444     code = OMP_CLAUSE_PRIVATE;
6445   else if (flags & GOVD_FIRSTPRIVATE)
6446     code = OMP_CLAUSE_FIRSTPRIVATE;
6447   else if (flags & GOVD_LASTPRIVATE)
6448     code = OMP_CLAUSE_LASTPRIVATE;
6449   else if (flags & GOVD_ALIGNED)
6450     return 0;
6451   else
6452     gcc_unreachable ();
6453
6454   clause = build_omp_clause (input_location, code);
6455   OMP_CLAUSE_DECL (clause) = decl;
6456   OMP_CLAUSE_CHAIN (clause) = *list_p;
6457   if (private_debug)
6458     OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6459   else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6460     OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6461   else if (code == OMP_CLAUSE_MAP)
6462     {
6463       OMP_CLAUSE_SET_MAP_KIND (clause,
6464                                flags & GOVD_MAP_TO_ONLY
6465                                ? GOMP_MAP_TO
6466                                : GOMP_MAP_TOFROM);
6467       if (DECL_SIZE (decl)
6468           && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6469         {
6470           tree decl2 = DECL_VALUE_EXPR (decl);
6471           gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6472           decl2 = TREE_OPERAND (decl2, 0);
6473           gcc_assert (DECL_P (decl2));
6474           tree mem = build_simple_mem_ref (decl2);
6475           OMP_CLAUSE_DECL (clause) = mem;
6476           OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6477           if (gimplify_omp_ctxp->outer_context)
6478             {
6479               struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6480               omp_notice_variable (ctx, decl2, true);
6481               omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6482             }
6483           tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6484                                       OMP_CLAUSE_MAP);
6485           OMP_CLAUSE_DECL (nc) = decl;
6486           OMP_CLAUSE_SIZE (nc) = size_zero_node;
6487           OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
6488           OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6489           OMP_CLAUSE_CHAIN (clause) = nc;
6490         }
6491       else
6492         OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6493     }
6494   if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6495     {
6496       tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6497       OMP_CLAUSE_DECL (nc) = decl;
6498       OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6499       OMP_CLAUSE_CHAIN (nc) = *list_p;
6500       OMP_CLAUSE_CHAIN (clause) = nc;
6501       struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6502       gimplify_omp_ctxp = ctx->outer_context;
6503       lang_hooks.decls.omp_finish_clause (nc, pre_p);
6504       gimplify_omp_ctxp = ctx;
6505     }
6506   *list_p = clause;
6507   struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6508   gimplify_omp_ctxp = ctx->outer_context;
6509   lang_hooks.decls.omp_finish_clause (clause, pre_p);
6510   gimplify_omp_ctxp = ctx;
6511   return 0;
6512 }
6513
6514 static void
6515 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6516 {
6517   struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6518   tree c, decl;
6519
6520   while ((c = *list_p) != NULL)
6521     {
6522       splay_tree_node n;
6523       bool remove = false;
6524
6525       switch (OMP_CLAUSE_CODE (c))
6526         {
6527         case OMP_CLAUSE_PRIVATE:
6528         case OMP_CLAUSE_SHARED:
6529         case OMP_CLAUSE_FIRSTPRIVATE:
6530         case OMP_CLAUSE_LINEAR:
6531           decl = OMP_CLAUSE_DECL (c);
6532           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6533           remove = !(n->value & GOVD_SEEN);
6534           if (! remove)
6535             {
6536               bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6537               if ((n->value & GOVD_DEBUG_PRIVATE)
6538                   || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6539                 {
6540                   gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6541                               || ((n->value & GOVD_DATA_SHARE_CLASS)
6542                                   == GOVD_PRIVATE));
6543                   OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6544                   OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6545                 }
6546               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6547                   && ctx->outer_context
6548                   && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6549                        && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6550                 {
6551                   if (ctx->outer_context->combined_loop
6552                       && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6553                     {
6554                       n = splay_tree_lookup (ctx->outer_context->variables,
6555                                              (splay_tree_key) decl);
6556                       if (n == NULL
6557                           || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6558                         {
6559                           int flags = GOVD_FIRSTPRIVATE;
6560                           /* #pragma omp distribute does not allow
6561                              lastprivate clause.  */
6562                           if (!ctx->outer_context->distribute)
6563                             flags |= GOVD_LASTPRIVATE;
6564                           if (n == NULL)
6565                             omp_add_variable (ctx->outer_context, decl,
6566                                               flags | GOVD_SEEN);
6567                           else
6568                             n->value |= flags | GOVD_SEEN;
6569                         }
6570                     }
6571                   else if (!is_global_var (decl))
6572                     omp_notice_variable (ctx->outer_context, decl, true);
6573                 }
6574             }
6575           break;
6576
6577         case OMP_CLAUSE_LASTPRIVATE:
6578           /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6579              accurately reflect the presence of a FIRSTPRIVATE clause.  */
6580           decl = OMP_CLAUSE_DECL (c);
6581           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6582           OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6583             = (n->value & GOVD_FIRSTPRIVATE) != 0;
6584           break;
6585
6586         case OMP_CLAUSE_ALIGNED:
6587           decl = OMP_CLAUSE_DECL (c);
6588           if (!is_global_var (decl))
6589             {
6590               n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6591               remove = n == NULL || !(n->value & GOVD_SEEN);
6592               if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6593                 {
6594                   struct gimplify_omp_ctx *octx;
6595                   if (n != NULL
6596                       && (n->value & (GOVD_DATA_SHARE_CLASS
6597                                       & ~GOVD_FIRSTPRIVATE)))
6598                     remove = true;
6599                   else
6600                     for (octx = ctx->outer_context; octx;
6601                          octx = octx->outer_context)
6602                       {
6603                         n = splay_tree_lookup (octx->variables,
6604                                                (splay_tree_key) decl);
6605                         if (n == NULL)
6606                           continue;
6607                         if (n->value & GOVD_LOCAL)
6608                           break;
6609                         /* We have to avoid assigning a shared variable
6610                            to itself when trying to add
6611                            __builtin_assume_aligned.  */
6612                         if (n->value & GOVD_SHARED)
6613                           {
6614                             remove = true;
6615                             break;
6616                           }
6617                       }
6618                 }
6619             }
6620           else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6621             {
6622               n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6623               if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6624                 remove = true;
6625             }
6626           break;
6627
6628         case OMP_CLAUSE_MAP:
6629           decl = OMP_CLAUSE_DECL (c);
6630           if (!DECL_P (decl))
6631             break;
6632           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6633           if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6634             remove = true;
6635           else if (DECL_SIZE (decl)
6636                    && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6637                    && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER)
6638             {
6639               /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
6640                  for these, TREE_CODE (DECL_SIZE (decl)) will always be
6641                  INTEGER_CST.  */
6642               gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
6643
6644               tree decl2 = DECL_VALUE_EXPR (decl);
6645               gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6646               decl2 = TREE_OPERAND (decl2, 0);
6647               gcc_assert (DECL_P (decl2));
6648               tree mem = build_simple_mem_ref (decl2);
6649               OMP_CLAUSE_DECL (c) = mem;
6650               OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6651               if (ctx->outer_context)
6652                 {
6653                   omp_notice_variable (ctx->outer_context, decl2, true);
6654                   omp_notice_variable (ctx->outer_context,
6655                                        OMP_CLAUSE_SIZE (c), true);
6656                 }
6657               tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6658                                           OMP_CLAUSE_MAP);
6659               OMP_CLAUSE_DECL (nc) = decl;
6660               OMP_CLAUSE_SIZE (nc) = size_zero_node;
6661               OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
6662               OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6663               OMP_CLAUSE_CHAIN (c) = nc;
6664               c = nc;
6665             }
6666           else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6667             OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6668           break;
6669
6670         case OMP_CLAUSE_TO:
6671         case OMP_CLAUSE_FROM:
6672         case OMP_CLAUSE__CACHE_:
6673           decl = OMP_CLAUSE_DECL (c);
6674           if (!DECL_P (decl))
6675             break;
6676           if (DECL_SIZE (decl)
6677               && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6678             {
6679               tree decl2 = DECL_VALUE_EXPR (decl);
6680               gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6681               decl2 = TREE_OPERAND (decl2, 0);
6682               gcc_assert (DECL_P (decl2));
6683               tree mem = build_simple_mem_ref (decl2);
6684               OMP_CLAUSE_DECL (c) = mem;
6685               OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6686               if (ctx->outer_context)
6687                 {
6688                   omp_notice_variable (ctx->outer_context, decl2, true);
6689                   omp_notice_variable (ctx->outer_context,
6690                                        OMP_CLAUSE_SIZE (c), true);
6691                 }
6692             }
6693           else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6694             OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6695           break;
6696
6697         case OMP_CLAUSE_REDUCTION:
6698         case OMP_CLAUSE_COPYIN:
6699         case OMP_CLAUSE_COPYPRIVATE:
6700         case OMP_CLAUSE_IF:
6701         case OMP_CLAUSE_NUM_THREADS:
6702         case OMP_CLAUSE_NUM_TEAMS:
6703         case OMP_CLAUSE_THREAD_LIMIT:
6704         case OMP_CLAUSE_DIST_SCHEDULE:
6705         case OMP_CLAUSE_DEVICE:
6706         case OMP_CLAUSE_SCHEDULE:
6707         case OMP_CLAUSE_NOWAIT:
6708         case OMP_CLAUSE_ORDERED:
6709         case OMP_CLAUSE_DEFAULT:
6710         case OMP_CLAUSE_UNTIED:
6711         case OMP_CLAUSE_COLLAPSE:
6712         case OMP_CLAUSE_FINAL:
6713         case OMP_CLAUSE_MERGEABLE:
6714         case OMP_CLAUSE_PROC_BIND:
6715         case OMP_CLAUSE_SAFELEN:
6716         case OMP_CLAUSE_DEPEND:
6717         case OMP_CLAUSE__CILK_FOR_COUNT_:
6718         case OMP_CLAUSE_ASYNC:
6719         case OMP_CLAUSE_WAIT:
6720         case OMP_CLAUSE_DEVICE_RESIDENT:
6721         case OMP_CLAUSE_USE_DEVICE:
6722         case OMP_CLAUSE_INDEPENDENT:
6723         case OMP_CLAUSE_NUM_GANGS:
6724         case OMP_CLAUSE_NUM_WORKERS:
6725         case OMP_CLAUSE_VECTOR_LENGTH:
6726         case OMP_CLAUSE_GANG:
6727         case OMP_CLAUSE_WORKER:
6728         case OMP_CLAUSE_VECTOR:
6729         case OMP_CLAUSE_AUTO:
6730         case OMP_CLAUSE_SEQ:
6731           break;
6732
6733         default:
6734           gcc_unreachable ();
6735         }
6736
6737       if (remove)
6738         *list_p = OMP_CLAUSE_CHAIN (c);
6739       else
6740         list_p = &OMP_CLAUSE_CHAIN (c);
6741     }
6742
6743   /* Add in any implicit data sharing.  */
6744   struct gimplify_adjust_omp_clauses_data data;
6745   data.list_p = list_p;
6746   data.pre_p = pre_p;
6747   splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6748
6749   gimplify_omp_ctxp = ctx->outer_context;
6750   delete_omp_context (ctx);
6751 }
6752
6753 /* Gimplify OACC_CACHE.  */
6754
6755 static void
6756 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
6757 {
6758   tree expr = *expr_p;
6759
6760   gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6761   gimplify_adjust_omp_clauses (pre_p, &OACC_CACHE_CLAUSES (expr));
6762
6763   /* TODO: Do something sensible with this information.  */
6764
6765   *expr_p = NULL_TREE;
6766 }
6767
6768 /* Gimplify the contents of an OMP_PARALLEL statement.  This involves
6769    gimplification of the body, as well as scanning the body for used
6770    variables.  We need to do this scan now, because variable-sized
6771    decls will be decomposed during gimplification.  */
6772
6773 static void
6774 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6775 {
6776   tree expr = *expr_p;
6777   gimple g;
6778   gimple_seq body = NULL;
6779
6780   gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6781                              OMP_PARALLEL_COMBINED (expr)
6782                              ? ORT_COMBINED_PARALLEL
6783                              : ORT_PARALLEL);
6784
6785   push_gimplify_context ();
6786
6787   g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6788   if (gimple_code (g) == GIMPLE_BIND)
6789     pop_gimplify_context (g);
6790   else
6791     pop_gimplify_context (NULL);
6792
6793   gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6794
6795   g = gimple_build_omp_parallel (body,
6796                                  OMP_PARALLEL_CLAUSES (expr),
6797                                  NULL_TREE, NULL_TREE);
6798   if (OMP_PARALLEL_COMBINED (expr))
6799     gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6800   gimplify_seq_add_stmt (pre_p, g);
6801   *expr_p = NULL_TREE;
6802 }
6803
6804 /* Gimplify the contents of an OMP_TASK statement.  This involves
6805    gimplification of the body, as well as scanning the body for used
6806    variables.  We need to do this scan now, because variable-sized
6807    decls will be decomposed during gimplification.  */
6808
6809 static void
6810 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6811 {
6812   tree expr = *expr_p;
6813   gimple g;
6814   gimple_seq body = NULL;
6815
6816   gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6817                              find_omp_clause (OMP_TASK_CLAUSES (expr),
6818                                               OMP_CLAUSE_UNTIED)
6819                              ? ORT_UNTIED_TASK : ORT_TASK);
6820
6821   push_gimplify_context ();
6822
6823   g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6824   if (gimple_code (g) == GIMPLE_BIND)
6825     pop_gimplify_context (g);
6826   else
6827     pop_gimplify_context (NULL);
6828
6829   gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6830
6831   g = gimple_build_omp_task (body,
6832                              OMP_TASK_CLAUSES (expr),
6833                              NULL_TREE, NULL_TREE,
6834                              NULL_TREE, NULL_TREE, NULL_TREE);
6835   gimplify_seq_add_stmt (pre_p, g);
6836   *expr_p = NULL_TREE;
6837 }
6838
6839 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6840    with non-NULL OMP_FOR_INIT.  */
6841
6842 static tree
6843 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6844 {
6845   *walk_subtrees = 0;
6846   switch (TREE_CODE (*tp))
6847     {
6848     case OMP_FOR:
6849       *walk_subtrees = 1;
6850       /* FALLTHRU */
6851     case OMP_SIMD:
6852       if (OMP_FOR_INIT (*tp) != NULL_TREE)
6853         return *tp;
6854       break;
6855     case BIND_EXPR:
6856     case STATEMENT_LIST:
6857     case OMP_PARALLEL:
6858       *walk_subtrees = 1;
6859       break;
6860     default:
6861       break;
6862     }
6863   return NULL_TREE;
6864 }
6865
6866 /* Gimplify the gross structure of an OMP_FOR statement.  */
6867
6868 static enum gimplify_status
6869 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6870 {
6871   tree for_stmt, orig_for_stmt, decl, var, t;
6872   enum gimplify_status ret = GS_ALL_DONE;
6873   enum gimplify_status tret;
6874   gomp_for *gfor;
6875   gimple_seq for_body, for_pre_body;
6876   int i;
6877   bool simd;
6878   bitmap has_decl_expr = NULL;
6879
6880   orig_for_stmt = for_stmt = *expr_p;
6881
6882   switch (TREE_CODE (for_stmt))
6883     {
6884     case OMP_FOR:
6885     case CILK_FOR:
6886     case OMP_DISTRIBUTE:
6887     case OACC_LOOP:
6888       simd = false;
6889       break;
6890     case OMP_SIMD:
6891     case CILK_SIMD:
6892       simd = true;
6893       break;
6894     default:
6895       gcc_unreachable ();
6896     }
6897
6898   gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6899                              simd ? ORT_SIMD : ORT_WORKSHARE);
6900   if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6901     gimplify_omp_ctxp->distribute = true;
6902
6903   /* Handle OMP_FOR_INIT.  */
6904   for_pre_body = NULL;
6905   if (simd && OMP_FOR_PRE_BODY (for_stmt))
6906     {
6907       has_decl_expr = BITMAP_ALLOC (NULL);
6908       if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6909           && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6910              == VAR_DECL)
6911         {
6912           t = OMP_FOR_PRE_BODY (for_stmt);
6913           bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6914         }
6915       else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6916         {
6917           tree_stmt_iterator si;
6918           for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6919                tsi_next (&si))
6920             {
6921               t = tsi_stmt (si);
6922               if (TREE_CODE (t) == DECL_EXPR
6923                   && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6924                 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6925             }
6926         }
6927     }
6928   gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6929   OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6930
6931   if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6932     {
6933       gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
6934       for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6935                             NULL, NULL);
6936       gcc_assert (for_stmt != NULL_TREE);
6937       gimplify_omp_ctxp->combined_loop = true;
6938     }
6939
6940   for_body = NULL;
6941   gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6942               == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6943   gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6944               == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6945   for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6946     {
6947       t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6948       gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6949       decl = TREE_OPERAND (t, 0);
6950       gcc_assert (DECL_P (decl));
6951       gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6952                   || POINTER_TYPE_P (TREE_TYPE (decl)));
6953
6954       /* Make sure the iteration variable is private.  */
6955       tree c = NULL_TREE;
6956       tree c2 = NULL_TREE;
6957       if (orig_for_stmt != for_stmt)
6958         /* Do this only on innermost construct for combined ones.  */;
6959       else if (simd)
6960         {
6961           splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6962                                                  (splay_tree_key)decl);
6963           omp_is_private (gimplify_omp_ctxp, decl,
6964                           1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6965                                != 1));
6966           if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6967             omp_notice_variable (gimplify_omp_ctxp, decl, true);
6968           else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6969             {
6970               c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6971               OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6972               if (has_decl_expr
6973                   && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6974                 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6975               OMP_CLAUSE_DECL (c) = decl;
6976               OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6977               OMP_FOR_CLAUSES (for_stmt) = c;
6978               omp_add_variable (gimplify_omp_ctxp, decl,
6979                                 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6980             }
6981           else
6982             {
6983               bool lastprivate
6984                 = (!has_decl_expr
6985                    || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6986               if (lastprivate
6987                   && gimplify_omp_ctxp->outer_context
6988                   && gimplify_omp_ctxp->outer_context->region_type
6989                      == ORT_WORKSHARE
6990                   && gimplify_omp_ctxp->outer_context->combined_loop
6991                   && !gimplify_omp_ctxp->outer_context->distribute)
6992                 {
6993                   struct gimplify_omp_ctx *outer
6994                     = gimplify_omp_ctxp->outer_context;
6995                   n = splay_tree_lookup (outer->variables,
6996                                          (splay_tree_key) decl);
6997                   if (n != NULL
6998                       && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6999                     lastprivate = false;
7000                   else if (omp_check_private (outer, decl, false))
7001                     error ("lastprivate variable %qE is private in outer "
7002                            "context", DECL_NAME (decl));
7003                   else
7004                     {
7005                       omp_add_variable (outer, decl,
7006                                         GOVD_LASTPRIVATE | GOVD_SEEN);
7007                       if (outer->outer_context)
7008                         omp_notice_variable (outer->outer_context, decl, true);
7009                     }
7010                 }
7011               c = build_omp_clause (input_location,
7012                                     lastprivate ? OMP_CLAUSE_LASTPRIVATE
7013                                                 : OMP_CLAUSE_PRIVATE);
7014               OMP_CLAUSE_DECL (c) = decl;
7015               OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
7016               OMP_FOR_CLAUSES (for_stmt) = c;
7017               omp_add_variable (gimplify_omp_ctxp, decl,
7018                                 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
7019                                 | GOVD_EXPLICIT | GOVD_SEEN);
7020               c = NULL_TREE;
7021             }
7022         }
7023       else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
7024         omp_notice_variable (gimplify_omp_ctxp, decl, true);
7025       else
7026         omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
7027
7028       /* If DECL is not a gimple register, create a temporary variable to act
7029          as an iteration counter.  This is valid, since DECL cannot be
7030          modified in the body of the loop.  Similarly for any iteration vars
7031          in simd with collapse > 1 where the iterator vars must be
7032          lastprivate.  */
7033       if (orig_for_stmt != for_stmt)
7034         var = decl;
7035       else if (!is_gimple_reg (decl)
7036                || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
7037         {
7038           var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7039           TREE_OPERAND (t, 0) = var;
7040
7041           gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
7042
7043           if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7044             {
7045               c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7046               OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
7047               OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
7048               OMP_CLAUSE_DECL (c2) = var;
7049               OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
7050               OMP_FOR_CLAUSES (for_stmt) = c2;
7051               omp_add_variable (gimplify_omp_ctxp, var,
7052                                 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
7053               if (c == NULL_TREE)
7054                 {
7055                   c = c2;
7056                   c2 = NULL_TREE;
7057                 }
7058             }
7059           else
7060             omp_add_variable (gimplify_omp_ctxp, var,
7061                               GOVD_PRIVATE | GOVD_SEEN);
7062         }
7063       else
7064         var = decl;
7065
7066       tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7067                             is_gimple_val, fb_rvalue);
7068       ret = MIN (ret, tret);
7069       if (ret == GS_ERROR)
7070         return ret;
7071
7072       /* Handle OMP_FOR_COND.  */
7073       t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7074       gcc_assert (COMPARISON_CLASS_P (t));
7075       gcc_assert (TREE_OPERAND (t, 0) == decl);
7076
7077       tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7078                             is_gimple_val, fb_rvalue);
7079       ret = MIN (ret, tret);
7080
7081       /* Handle OMP_FOR_INCR.  */
7082       t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7083       switch (TREE_CODE (t))
7084         {
7085         case PREINCREMENT_EXPR:
7086         case POSTINCREMENT_EXPR:
7087           {
7088             tree decl = TREE_OPERAND (t, 0);
7089             /* c_omp_for_incr_canonicalize_ptr() should have been
7090                called to massage things appropriately.  */
7091             gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7092
7093             if (orig_for_stmt != for_stmt)
7094               break;
7095             t = build_int_cst (TREE_TYPE (decl), 1);
7096             if (c)
7097               OMP_CLAUSE_LINEAR_STEP (c) = t;
7098             t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7099             t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7100             TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7101             break;
7102           }
7103
7104         case PREDECREMENT_EXPR:
7105         case POSTDECREMENT_EXPR:
7106           /* c_omp_for_incr_canonicalize_ptr() should have been
7107              called to massage things appropriately.  */
7108           gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7109           if (orig_for_stmt != for_stmt)
7110             break;
7111           t = build_int_cst (TREE_TYPE (decl), -1);
7112           if (c)
7113             OMP_CLAUSE_LINEAR_STEP (c) = t;
7114           t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7115           t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7116           TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7117           break;
7118
7119         case MODIFY_EXPR:
7120           gcc_assert (TREE_OPERAND (t, 0) == decl);
7121           TREE_OPERAND (t, 0) = var;
7122
7123           t = TREE_OPERAND (t, 1);
7124           switch (TREE_CODE (t))
7125             {
7126             case PLUS_EXPR:
7127               if (TREE_OPERAND (t, 1) == decl)
7128                 {
7129                   TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
7130                   TREE_OPERAND (t, 0) = var;
7131                   break;
7132                 }
7133
7134               /* Fallthru.  */
7135             case MINUS_EXPR:
7136             case POINTER_PLUS_EXPR:
7137               gcc_assert (TREE_OPERAND (t, 0) == decl);
7138               TREE_OPERAND (t, 0) = var;
7139               break;
7140             default:
7141               gcc_unreachable ();
7142             }
7143
7144           tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7145                                 is_gimple_val, fb_rvalue);
7146           ret = MIN (ret, tret);
7147           if (c)
7148             {
7149               tree step = TREE_OPERAND (t, 1);
7150               tree stept = TREE_TYPE (decl);
7151               if (POINTER_TYPE_P (stept))
7152                 stept = sizetype;
7153               step = fold_convert (stept, step);
7154               if (TREE_CODE (t) == MINUS_EXPR)
7155                 step = fold_build1 (NEGATE_EXPR, stept, step);
7156               OMP_CLAUSE_LINEAR_STEP (c) = step;
7157               if (step != TREE_OPERAND (t, 1))
7158                 {
7159                   tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7160                                         &for_pre_body, NULL,
7161                                         is_gimple_val, fb_rvalue);
7162                   ret = MIN (ret, tret);
7163                 }
7164             }
7165           break;
7166
7167         default:
7168           gcc_unreachable ();
7169         }
7170
7171       if (c2)
7172         {
7173           gcc_assert (c);
7174           OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7175         }
7176
7177       if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7178           && orig_for_stmt == for_stmt)
7179         {
7180           for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7181             if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7182                   && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7183                  || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7184                      && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7185                      && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7186                 && OMP_CLAUSE_DECL (c) == decl)
7187               {
7188                 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7189                 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7190                 gcc_assert (TREE_OPERAND (t, 0) == var);
7191                 t = TREE_OPERAND (t, 1);
7192                 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7193                             || TREE_CODE (t) == MINUS_EXPR
7194                             || TREE_CODE (t) == POINTER_PLUS_EXPR);
7195                 gcc_assert (TREE_OPERAND (t, 0) == var);
7196                 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7197                             TREE_OPERAND (t, 1));
7198                 gimple_seq *seq;
7199                 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7200                   seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7201                 else
7202                   seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7203                 gimplify_assign (decl, t, seq);
7204             }
7205         }
7206     }
7207
7208   BITMAP_FREE (has_decl_expr);
7209
7210   gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7211
7212   if (orig_for_stmt != for_stmt)
7213     for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7214       {
7215         t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7216         decl = TREE_OPERAND (t, 0);
7217         var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7218         omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7219         TREE_OPERAND (t, 0) = var;
7220         t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7221         TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7222         TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7223       }
7224
7225   gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7226
7227   int kind;
7228   switch (TREE_CODE (orig_for_stmt))
7229     {
7230     case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7231     case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7232     case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7233     case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7234     case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7235     case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
7236     default:
7237       gcc_unreachable ();
7238     }
7239   gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7240                                TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7241                                for_pre_body);
7242   if (orig_for_stmt != for_stmt)
7243     gimple_omp_for_set_combined_p (gfor, true);
7244   if (gimplify_omp_ctxp
7245       && (gimplify_omp_ctxp->combined_loop
7246           || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7247               && gimplify_omp_ctxp->outer_context
7248               && gimplify_omp_ctxp->outer_context->combined_loop)))
7249     {
7250       gimple_omp_for_set_combined_into_p (gfor, true);
7251       if (gimplify_omp_ctxp->combined_loop)
7252         gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7253       else
7254         gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7255     }
7256
7257   for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7258     {
7259       t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7260       gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7261       gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7262       t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7263       gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7264       gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7265       t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7266       gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7267     }
7268
7269   gimplify_seq_add_stmt (pre_p, gfor);
7270   if (ret != GS_ALL_DONE)
7271     return GS_ERROR;
7272   *expr_p = NULL_TREE;
7273   return GS_ALL_DONE;
7274 }
7275
7276 /* Gimplify the gross structure of several OMP constructs.  */
7277
7278 static void
7279 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7280 {
7281   tree expr = *expr_p;
7282   gimple stmt;
7283   gimple_seq body = NULL;
7284   enum omp_region_type ort;
7285
7286   switch (TREE_CODE (expr))
7287     {
7288     case OMP_SECTIONS:
7289     case OMP_SINGLE:
7290       ort = ORT_WORKSHARE;
7291       break;
7292     case OACC_KERNELS:
7293     case OACC_PARALLEL:
7294     case OMP_TARGET:
7295       ort = ORT_TARGET;
7296       break;
7297     case OACC_DATA:
7298     case OMP_TARGET_DATA:
7299       ort = ORT_TARGET_DATA;
7300       break;
7301     case OMP_TEAMS:
7302       ort = ORT_TEAMS;
7303       break;
7304     default:
7305       gcc_unreachable ();
7306     }
7307   gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7308   if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7309     {
7310       push_gimplify_context ();
7311       gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7312       if (gimple_code (g) == GIMPLE_BIND)
7313         pop_gimplify_context (g);
7314       else
7315         pop_gimplify_context (NULL);
7316       if (ort == ORT_TARGET_DATA)
7317         {
7318           enum built_in_function end_ix;
7319           switch (TREE_CODE (expr))
7320             {
7321             case OACC_DATA:
7322               end_ix = BUILT_IN_GOACC_DATA_END;
7323               break;
7324             case OMP_TARGET_DATA:
7325               end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
7326               break;
7327             default:
7328               gcc_unreachable ();
7329             }
7330           tree fn = builtin_decl_explicit (end_ix);
7331           g = gimple_build_call (fn, 0);
7332           gimple_seq cleanup = NULL;
7333           gimple_seq_add_stmt (&cleanup, g);
7334           g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7335           body = NULL;
7336           gimple_seq_add_stmt (&body, g);
7337         }
7338     }
7339   else
7340     gimplify_and_add (OMP_BODY (expr), &body);
7341   gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7342
7343   switch (TREE_CODE (expr))
7344     {
7345     case OACC_DATA:
7346       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
7347                                       OMP_CLAUSES (expr));
7348       break;
7349     case OACC_KERNELS:
7350       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
7351                                       OMP_CLAUSES (expr));
7352       break;
7353     case OACC_PARALLEL:
7354       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
7355                                       OMP_CLAUSES (expr));
7356       break;
7357     case OMP_SECTIONS:
7358       stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7359       break;
7360     case OMP_SINGLE:
7361       stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7362       break;
7363     case OMP_TARGET:
7364       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7365                                       OMP_CLAUSES (expr));
7366       break;
7367     case OMP_TARGET_DATA:
7368       stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7369                                       OMP_CLAUSES (expr));
7370       break;
7371     case OMP_TEAMS:
7372       stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7373       break;
7374     default:
7375       gcc_unreachable ();
7376     }
7377
7378   gimplify_seq_add_stmt (pre_p, stmt);
7379   *expr_p = NULL_TREE;
7380 }
7381
7382 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
7383    target update constructs.  */
7384
7385 static void
7386 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7387 {
7388   tree expr = *expr_p, clauses;
7389   int kind;
7390   gomp_target *stmt;
7391
7392   switch (TREE_CODE (expr))
7393     {
7394     case OACC_ENTER_DATA:
7395       clauses = OACC_ENTER_DATA_CLAUSES (expr);
7396       kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7397       break;
7398     case OACC_EXIT_DATA:
7399       clauses = OACC_EXIT_DATA_CLAUSES (expr);
7400       kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7401       break;
7402     case OACC_UPDATE:
7403       clauses = OACC_UPDATE_CLAUSES (expr);
7404       kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
7405       break;
7406     case OMP_TARGET_UPDATE:
7407       clauses = OMP_TARGET_UPDATE_CLAUSES (expr);
7408       kind = GF_OMP_TARGET_KIND_UPDATE;
7409       break;
7410     default:
7411       gcc_unreachable ();
7412     }
7413   gimplify_scan_omp_clauses (&clauses, pre_p, ORT_WORKSHARE);
7414   gimplify_adjust_omp_clauses (pre_p, &clauses);
7415   stmt = gimple_build_omp_target (NULL, kind, clauses);
7416
7417   gimplify_seq_add_stmt (pre_p, stmt);
7418   *expr_p = NULL_TREE;
7419 }
7420
7421 /* A subroutine of gimplify_omp_atomic.  The front end is supposed to have
7422    stabilized the lhs of the atomic operation as *ADDR.  Return true if
7423    EXPR is this stabilized form.  */
7424
7425 static bool
7426 goa_lhs_expr_p (tree expr, tree addr)
7427 {
7428   /* Also include casts to other type variants.  The C front end is fond
7429      of adding these for e.g. volatile variables.  This is like
7430      STRIP_TYPE_NOPS but includes the main variant lookup.  */
7431   STRIP_USELESS_TYPE_CONVERSION (expr);
7432
7433   if (TREE_CODE (expr) == INDIRECT_REF)
7434     {
7435       expr = TREE_OPERAND (expr, 0);
7436       while (expr != addr
7437              && (CONVERT_EXPR_P (expr)
7438                  || TREE_CODE (expr) == NON_LVALUE_EXPR)
7439              && TREE_CODE (expr) == TREE_CODE (addr)
7440              && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7441         {
7442           expr = TREE_OPERAND (expr, 0);
7443           addr = TREE_OPERAND (addr, 0);
7444         }
7445       if (expr == addr)
7446         return true;
7447       return (TREE_CODE (addr) == ADDR_EXPR
7448               && TREE_CODE (expr) == ADDR_EXPR
7449               && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7450     }
7451   if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7452     return true;
7453   return false;
7454 }
7455
7456 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR.  If an
7457    expression does not involve the lhs, evaluate it into a temporary.
7458    Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7459    or -1 if an error was encountered.  */
7460
7461 static int
7462 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7463                     tree lhs_var)
7464 {
7465   tree expr = *expr_p;
7466   int saw_lhs;
7467
7468   if (goa_lhs_expr_p (expr, lhs_addr))
7469     {
7470       *expr_p = lhs_var;
7471       return 1;
7472     }
7473   if (is_gimple_val (expr))
7474     return 0;
7475
7476   saw_lhs = 0;
7477   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7478     {
7479     case tcc_binary:
7480     case tcc_comparison:
7481       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7482                                      lhs_var);
7483     case tcc_unary:
7484       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7485                                      lhs_var);
7486       break;
7487     case tcc_expression:
7488       switch (TREE_CODE (expr))
7489         {
7490         case TRUTH_ANDIF_EXPR:
7491         case TRUTH_ORIF_EXPR:
7492         case TRUTH_AND_EXPR:
7493         case TRUTH_OR_EXPR:
7494         case TRUTH_XOR_EXPR:
7495           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7496                                          lhs_addr, lhs_var);
7497         case TRUTH_NOT_EXPR:
7498           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7499                                          lhs_addr, lhs_var);
7500           break;
7501         case COMPOUND_EXPR:
7502           /* Break out any preevaluations from cp_build_modify_expr.  */
7503           for (; TREE_CODE (expr) == COMPOUND_EXPR;
7504                expr = TREE_OPERAND (expr, 1))
7505             gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7506           *expr_p = expr;
7507           return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7508         default:
7509           break;
7510         }
7511       break;
7512     default:
7513       break;
7514     }
7515
7516   if (saw_lhs == 0)
7517     {
7518       enum gimplify_status gs;
7519       gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7520       if (gs != GS_ALL_DONE)
7521         saw_lhs = -1;
7522     }
7523
7524   return saw_lhs;
7525 }
7526
7527 /* Gimplify an OMP_ATOMIC statement.  */
7528
7529 static enum gimplify_status
7530 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7531 {
7532   tree addr = TREE_OPERAND (*expr_p, 0);
7533   tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7534              ? NULL : TREE_OPERAND (*expr_p, 1);
7535   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7536   tree tmp_load;
7537   gomp_atomic_load *loadstmt;
7538   gomp_atomic_store *storestmt;
7539
7540   tmp_load = create_tmp_reg (type);
7541   if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7542     return GS_ERROR;
7543
7544   if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7545       != GS_ALL_DONE)
7546     return GS_ERROR;
7547
7548   loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7549   gimplify_seq_add_stmt (pre_p, loadstmt);
7550   if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7551       != GS_ALL_DONE)
7552     return GS_ERROR;
7553
7554   if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7555     rhs = tmp_load;
7556   storestmt = gimple_build_omp_atomic_store (rhs);
7557   gimplify_seq_add_stmt (pre_p, storestmt);
7558   if (OMP_ATOMIC_SEQ_CST (*expr_p))
7559     {
7560       gimple_omp_atomic_set_seq_cst (loadstmt);
7561       gimple_omp_atomic_set_seq_cst (storestmt);
7562     }
7563   switch (TREE_CODE (*expr_p))
7564     {
7565     case OMP_ATOMIC_READ:
7566     case OMP_ATOMIC_CAPTURE_OLD:
7567       *expr_p = tmp_load;
7568       gimple_omp_atomic_set_need_value (loadstmt);
7569       break;
7570     case OMP_ATOMIC_CAPTURE_NEW:
7571       *expr_p = rhs;
7572       gimple_omp_atomic_set_need_value (storestmt);
7573       break;
7574     default:
7575       *expr_p = NULL;
7576       break;
7577     }
7578
7579   return GS_ALL_DONE;
7580 }
7581
7582 /* Gimplify a TRANSACTION_EXPR.  This involves gimplification of the
7583    body, and adding some EH bits.  */
7584
7585 static enum gimplify_status
7586 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7587 {
7588   tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7589   gimple body_stmt;
7590   gtransaction *trans_stmt;
7591   gimple_seq body = NULL;
7592   int subcode = 0;
7593
7594   /* Wrap the transaction body in a BIND_EXPR so we have a context
7595      where to put decls for OMP.  */
7596   if (TREE_CODE (tbody) != BIND_EXPR)
7597     {
7598       tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7599       TREE_SIDE_EFFECTS (bind) = 1;
7600       SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7601       TRANSACTION_EXPR_BODY (expr) = bind;
7602     }
7603
7604   push_gimplify_context ();
7605   temp = voidify_wrapper_expr (*expr_p, NULL);
7606
7607   body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7608   pop_gimplify_context (body_stmt);
7609
7610   trans_stmt = gimple_build_transaction (body, NULL);
7611   if (TRANSACTION_EXPR_OUTER (expr))
7612     subcode = GTMA_IS_OUTER;
7613   else if (TRANSACTION_EXPR_RELAXED (expr))
7614     subcode = GTMA_IS_RELAXED;
7615   gimple_transaction_set_subcode (trans_stmt, subcode);
7616
7617   gimplify_seq_add_stmt (pre_p, trans_stmt);
7618
7619   if (temp)
7620     {
7621       *expr_p = temp;
7622       return GS_OK;
7623     }
7624
7625   *expr_p = NULL_TREE;
7626   return GS_ALL_DONE;
7627 }
7628
7629 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE.  If the
7630    expression produces a value to be used as an operand inside a GIMPLE
7631    statement, the value will be stored back in *EXPR_P.  This value will
7632    be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7633    an SSA_NAME.  The corresponding sequence of GIMPLE statements is
7634    emitted in PRE_P and POST_P.
7635
7636    Additionally, this process may overwrite parts of the input
7637    expression during gimplification.  Ideally, it should be
7638    possible to do non-destructive gimplification.
7639
7640    EXPR_P points to the GENERIC expression to convert to GIMPLE.  If
7641       the expression needs to evaluate to a value to be used as
7642       an operand in a GIMPLE statement, this value will be stored in
7643       *EXPR_P on exit.  This happens when the caller specifies one
7644       of fb_lvalue or fb_rvalue fallback flags.
7645
7646    PRE_P will contain the sequence of GIMPLE statements corresponding
7647        to the evaluation of EXPR and all the side-effects that must
7648        be executed before the main expression.  On exit, the last
7649        statement of PRE_P is the core statement being gimplified.  For
7650        instance, when gimplifying 'if (++a)' the last statement in
7651        PRE_P will be 'if (t.1)' where t.1 is the result of
7652        pre-incrementing 'a'.
7653
7654    POST_P will contain the sequence of GIMPLE statements corresponding
7655        to the evaluation of all the side-effects that must be executed
7656        after the main expression.  If this is NULL, the post
7657        side-effects are stored at the end of PRE_P.
7658
7659        The reason why the output is split in two is to handle post
7660        side-effects explicitly.  In some cases, an expression may have
7661        inner and outer post side-effects which need to be emitted in
7662        an order different from the one given by the recursive
7663        traversal.  For instance, for the expression (*p--)++ the post
7664        side-effects of '--' must actually occur *after* the post
7665        side-effects of '++'.  However, gimplification will first visit
7666        the inner expression, so if a separate POST sequence was not
7667        used, the resulting sequence would be:
7668
7669             1   t.1 = *p
7670             2   p = p - 1
7671             3   t.2 = t.1 + 1
7672             4   *p = t.2
7673
7674        However, the post-decrement operation in line #2 must not be
7675        evaluated until after the store to *p at line #4, so the
7676        correct sequence should be:
7677
7678             1   t.1 = *p
7679             2   t.2 = t.1 + 1
7680             3   *p = t.2
7681             4   p = p - 1
7682
7683        So, by specifying a separate post queue, it is possible
7684        to emit the post side-effects in the correct order.
7685        If POST_P is NULL, an internal queue will be used.  Before
7686        returning to the caller, the sequence POST_P is appended to
7687        the main output sequence PRE_P.
7688
7689    GIMPLE_TEST_F points to a function that takes a tree T and
7690        returns nonzero if T is in the GIMPLE form requested by the
7691        caller.  The GIMPLE predicates are in gimple.c.
7692
7693    FALLBACK tells the function what sort of a temporary we want if
7694        gimplification cannot produce an expression that complies with
7695        GIMPLE_TEST_F.
7696
7697        fb_none means that no temporary should be generated
7698        fb_rvalue means that an rvalue is OK to generate
7699        fb_lvalue means that an lvalue is OK to generate
7700        fb_either means that either is OK, but an lvalue is preferable.
7701        fb_mayfail means that gimplification may fail (in which case
7702        GS_ERROR will be returned)
7703
7704    The return value is either GS_ERROR or GS_ALL_DONE, since this
7705    function iterates until EXPR is completely gimplified or an error
7706    occurs.  */
7707
7708 enum gimplify_status
7709 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7710                bool (*gimple_test_f) (tree), fallback_t fallback)
7711 {
7712   tree tmp;
7713   gimple_seq internal_pre = NULL;
7714   gimple_seq internal_post = NULL;
7715   tree save_expr;
7716   bool is_statement;
7717   location_t saved_location;
7718   enum gimplify_status ret;
7719   gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7720
7721   save_expr = *expr_p;
7722   if (save_expr == NULL_TREE)
7723     return GS_ALL_DONE;
7724
7725   /* If we are gimplifying a top-level statement, PRE_P must be valid.  */
7726   is_statement = gimple_test_f == is_gimple_stmt;
7727   if (is_statement)
7728     gcc_assert (pre_p);
7729
7730   /* Consistency checks.  */
7731   if (gimple_test_f == is_gimple_reg)
7732     gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7733   else if (gimple_test_f == is_gimple_val
7734            || gimple_test_f == is_gimple_call_addr
7735            || gimple_test_f == is_gimple_condexpr
7736            || gimple_test_f == is_gimple_mem_rhs
7737            || gimple_test_f == is_gimple_mem_rhs_or_call
7738            || gimple_test_f == is_gimple_reg_rhs
7739            || gimple_test_f == is_gimple_reg_rhs_or_call
7740            || gimple_test_f == is_gimple_asm_val
7741            || gimple_test_f == is_gimple_mem_ref_addr)
7742     gcc_assert (fallback & fb_rvalue);
7743   else if (gimple_test_f == is_gimple_min_lval
7744            || gimple_test_f == is_gimple_lvalue)
7745     gcc_assert (fallback & fb_lvalue);
7746   else if (gimple_test_f == is_gimple_addressable)
7747     gcc_assert (fallback & fb_either);
7748   else if (gimple_test_f == is_gimple_stmt)
7749     gcc_assert (fallback == fb_none);
7750   else
7751     {
7752       /* We should have recognized the GIMPLE_TEST_F predicate to
7753          know what kind of fallback to use in case a temporary is
7754          needed to hold the value or address of *EXPR_P.  */
7755       gcc_unreachable ();
7756     }
7757
7758   /* We used to check the predicate here and return immediately if it
7759      succeeds.  This is wrong; the design is for gimplification to be
7760      idempotent, and for the predicates to only test for valid forms, not
7761      whether they are fully simplified.  */
7762   if (pre_p == NULL)
7763     pre_p = &internal_pre;
7764
7765   if (post_p == NULL)
7766     post_p = &internal_post;
7767
7768   /* Remember the last statements added to PRE_P and POST_P.  Every
7769      new statement added by the gimplification helpers needs to be
7770      annotated with location information.  To centralize the
7771      responsibility, we remember the last statement that had been
7772      added to both queues before gimplifying *EXPR_P.  If
7773      gimplification produces new statements in PRE_P and POST_P, those
7774      statements will be annotated with the same location information
7775      as *EXPR_P.  */
7776   pre_last_gsi = gsi_last (*pre_p);
7777   post_last_gsi = gsi_last (*post_p);
7778
7779   saved_location = input_location;
7780   if (save_expr != error_mark_node
7781       && EXPR_HAS_LOCATION (*expr_p))
7782     input_location = EXPR_LOCATION (*expr_p);
7783
7784   /* Loop over the specific gimplifiers until the toplevel node
7785      remains the same.  */
7786   do
7787     {
7788       /* Strip away as many useless type conversions as possible
7789          at the toplevel.  */
7790       STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7791
7792       /* Remember the expr.  */
7793       save_expr = *expr_p;
7794
7795       /* Die, die, die, my darling.  */
7796       if (save_expr == error_mark_node
7797           || (TREE_TYPE (save_expr)
7798               && TREE_TYPE (save_expr) == error_mark_node))
7799         {
7800           ret = GS_ERROR;
7801           break;
7802         }
7803
7804       /* Do any language-specific gimplification.  */
7805       ret = ((enum gimplify_status)
7806              lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7807       if (ret == GS_OK)
7808         {
7809           if (*expr_p == NULL_TREE)
7810             break;
7811           if (*expr_p != save_expr)
7812             continue;
7813         }
7814       else if (ret != GS_UNHANDLED)
7815         break;
7816
7817       /* Make sure that all the cases set 'ret' appropriately.  */
7818       ret = GS_UNHANDLED;
7819       switch (TREE_CODE (*expr_p))
7820         {
7821           /* First deal with the special cases.  */
7822
7823         case POSTINCREMENT_EXPR:
7824         case POSTDECREMENT_EXPR:
7825         case PREINCREMENT_EXPR:
7826         case PREDECREMENT_EXPR:
7827           ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7828                                         fallback != fb_none,
7829                                         TREE_TYPE (*expr_p));
7830           break;
7831
7832         case VIEW_CONVERT_EXPR:
7833           if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7834               && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7835             {
7836               ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7837                                    post_p, is_gimple_val, fb_rvalue);
7838               recalculate_side_effects (*expr_p);
7839               break;
7840             }
7841           /* Fallthru.  */
7842
7843         case ARRAY_REF:
7844         case ARRAY_RANGE_REF:
7845         case REALPART_EXPR:
7846         case IMAGPART_EXPR:
7847         case COMPONENT_REF:
7848           ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7849                                         fallback ? fallback : fb_rvalue);
7850           break;
7851
7852         case COND_EXPR:
7853           ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7854
7855           /* C99 code may assign to an array in a structure value of a
7856              conditional expression, and this has undefined behavior
7857              only on execution, so create a temporary if an lvalue is
7858              required.  */
7859           if (fallback == fb_lvalue)
7860             {
7861               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7862               mark_addressable (*expr_p);
7863               ret = GS_OK;
7864             }
7865           break;
7866
7867         case CALL_EXPR:
7868           ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7869
7870           /* C99 code may assign to an array in a structure returned
7871              from a function, and this has undefined behavior only on
7872              execution, so create a temporary if an lvalue is
7873              required.  */
7874           if (fallback == fb_lvalue)
7875             {
7876               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7877               mark_addressable (*expr_p);
7878               ret = GS_OK;
7879             }
7880           break;
7881
7882         case TREE_LIST:
7883           gcc_unreachable ();
7884
7885         case COMPOUND_EXPR:
7886           ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7887           break;
7888
7889         case COMPOUND_LITERAL_EXPR:
7890           ret = gimplify_compound_literal_expr (expr_p, pre_p,
7891                                                 gimple_test_f, fallback);
7892           break;
7893
7894         case MODIFY_EXPR:
7895         case INIT_EXPR:
7896           ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7897                                       fallback != fb_none);
7898           break;
7899
7900         case TRUTH_ANDIF_EXPR:
7901         case TRUTH_ORIF_EXPR:
7902           {
7903             /* Preserve the original type of the expression and the
7904                source location of the outer expression.  */
7905             tree org_type = TREE_TYPE (*expr_p);
7906             *expr_p = gimple_boolify (*expr_p);
7907             *expr_p = build3_loc (input_location, COND_EXPR,
7908                                   org_type, *expr_p,
7909                                   fold_convert_loc
7910                                     (input_location,
7911                                      org_type, boolean_true_node),
7912                                   fold_convert_loc
7913                                     (input_location,
7914                                      org_type, boolean_false_node));
7915             ret = GS_OK;
7916             break;
7917           }
7918
7919         case TRUTH_NOT_EXPR:
7920           {
7921             tree type = TREE_TYPE (*expr_p);
7922             /* The parsers are careful to generate TRUTH_NOT_EXPR
7923                only with operands that are always zero or one.
7924                We do not fold here but handle the only interesting case
7925                manually, as fold may re-introduce the TRUTH_NOT_EXPR.  */
7926             *expr_p = gimple_boolify (*expr_p);
7927             if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7928               *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7929                                     TREE_TYPE (*expr_p),
7930                                     TREE_OPERAND (*expr_p, 0));
7931             else
7932               *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7933                                     TREE_TYPE (*expr_p),
7934                                     TREE_OPERAND (*expr_p, 0),
7935                                     build_int_cst (TREE_TYPE (*expr_p), 1));
7936             if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7937               *expr_p = fold_convert_loc (input_location, type, *expr_p);
7938             ret = GS_OK;
7939             break;
7940           }
7941
7942         case ADDR_EXPR:
7943           ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7944           break;
7945
7946         case ANNOTATE_EXPR:
7947           {
7948             tree cond = TREE_OPERAND (*expr_p, 0);
7949             tree kind = TREE_OPERAND (*expr_p, 1);
7950             tree type = TREE_TYPE (cond);
7951             if (!INTEGRAL_TYPE_P (type))
7952               {
7953                 *expr_p = cond;
7954                 ret = GS_OK;
7955                 break;
7956               }
7957             tree tmp = create_tmp_var (type);
7958             gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7959             gcall *call
7960               = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7961             gimple_call_set_lhs (call, tmp);
7962             gimplify_seq_add_stmt (pre_p, call);
7963             *expr_p = tmp;
7964             ret = GS_ALL_DONE;
7965             break;
7966           }
7967
7968         case VA_ARG_EXPR:
7969           ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7970           break;
7971
7972         CASE_CONVERT:
7973           if (IS_EMPTY_STMT (*expr_p))
7974             {
7975               ret = GS_ALL_DONE;
7976               break;
7977             }
7978
7979           if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7980               || fallback == fb_none)
7981             {
7982               /* Just strip a conversion to void (or in void context) and
7983                  try again.  */
7984               *expr_p = TREE_OPERAND (*expr_p, 0);
7985               ret = GS_OK;
7986               break;
7987             }
7988
7989           ret = gimplify_conversion (expr_p);
7990           if (ret == GS_ERROR)
7991             break;
7992           if (*expr_p != save_expr)
7993             break;
7994           /* FALLTHRU */
7995
7996         case FIX_TRUNC_EXPR:
7997           /* unary_expr: ... | '(' cast ')' val | ...  */
7998           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7999                                is_gimple_val, fb_rvalue);
8000           recalculate_side_effects (*expr_p);
8001           break;
8002
8003         case INDIRECT_REF:
8004           {
8005             bool volatilep = TREE_THIS_VOLATILE (*expr_p);
8006             bool notrap = TREE_THIS_NOTRAP (*expr_p);
8007             tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
8008
8009             *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
8010             if (*expr_p != save_expr)
8011               {
8012                 ret = GS_OK;
8013                 break;
8014               }
8015
8016             ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8017                                  is_gimple_reg, fb_rvalue);
8018             if (ret == GS_ERROR)
8019               break;
8020
8021             recalculate_side_effects (*expr_p);
8022             *expr_p = fold_build2_loc (input_location, MEM_REF,
8023                                        TREE_TYPE (*expr_p),
8024                                        TREE_OPERAND (*expr_p, 0),
8025                                        build_int_cst (saved_ptr_type, 0));
8026             TREE_THIS_VOLATILE (*expr_p) = volatilep;
8027             TREE_THIS_NOTRAP (*expr_p) = notrap;
8028             ret = GS_OK;
8029             break;
8030           }
8031
8032         /* We arrive here through the various re-gimplifcation paths.  */
8033         case MEM_REF:
8034           /* First try re-folding the whole thing.  */
8035           tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
8036                              TREE_OPERAND (*expr_p, 0),
8037                              TREE_OPERAND (*expr_p, 1));
8038           if (tmp)
8039             {
8040               *expr_p = tmp;
8041               recalculate_side_effects (*expr_p);
8042               ret = GS_OK;
8043               break;
8044             }
8045           /* Avoid re-gimplifying the address operand if it is already
8046              in suitable form.  Re-gimplifying would mark the address
8047              operand addressable.  Always gimplify when not in SSA form
8048              as we still may have to gimplify decls with value-exprs.  */
8049           if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
8050               || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
8051             {
8052               ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8053                                    is_gimple_mem_ref_addr, fb_rvalue);
8054               if (ret == GS_ERROR)
8055                 break;
8056             }
8057           recalculate_side_effects (*expr_p);
8058           ret = GS_ALL_DONE;
8059           break;
8060
8061         /* Constants need not be gimplified.  */
8062         case INTEGER_CST:
8063         case REAL_CST:
8064         case FIXED_CST:
8065         case STRING_CST:
8066         case COMPLEX_CST:
8067         case VECTOR_CST:
8068           /* Drop the overflow flag on constants, we do not want
8069              that in the GIMPLE IL.  */
8070           if (TREE_OVERFLOW_P (*expr_p))
8071             *expr_p = drop_tree_overflow (*expr_p);
8072           ret = GS_ALL_DONE;
8073           break;
8074
8075         case CONST_DECL:
8076           /* If we require an lvalue, such as for ADDR_EXPR, retain the
8077              CONST_DECL node.  Otherwise the decl is replaceable by its
8078              value.  */
8079           /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either.  */
8080           if (fallback & fb_lvalue)
8081             ret = GS_ALL_DONE;
8082           else
8083             {
8084               *expr_p = DECL_INITIAL (*expr_p);
8085               ret = GS_OK;
8086             }
8087           break;
8088
8089         case DECL_EXPR:
8090           ret = gimplify_decl_expr (expr_p, pre_p);
8091           break;
8092
8093         case BIND_EXPR:
8094           ret = gimplify_bind_expr (expr_p, pre_p);
8095           break;
8096
8097         case LOOP_EXPR:
8098           ret = gimplify_loop_expr (expr_p, pre_p);
8099           break;
8100
8101         case SWITCH_EXPR:
8102           ret = gimplify_switch_expr (expr_p, pre_p);
8103           break;
8104
8105         case EXIT_EXPR:
8106           ret = gimplify_exit_expr (expr_p);
8107           break;
8108
8109         case GOTO_EXPR:
8110           /* If the target is not LABEL, then it is a computed jump
8111              and the target needs to be gimplified.  */
8112           if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
8113             {
8114               ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
8115                                    NULL, is_gimple_val, fb_rvalue);
8116               if (ret == GS_ERROR)
8117                 break;
8118             }
8119           gimplify_seq_add_stmt (pre_p,
8120                           gimple_build_goto (GOTO_DESTINATION (*expr_p)));
8121           ret = GS_ALL_DONE;
8122           break;
8123
8124         case PREDICT_EXPR:
8125           gimplify_seq_add_stmt (pre_p,
8126                         gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
8127                                               PREDICT_EXPR_OUTCOME (*expr_p)));
8128           ret = GS_ALL_DONE;
8129           break;
8130
8131         case LABEL_EXPR:
8132           ret = GS_ALL_DONE;
8133           gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
8134                       == current_function_decl);
8135           gimplify_seq_add_stmt (pre_p,
8136                           gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
8137           break;
8138
8139         case CASE_LABEL_EXPR:
8140           ret = gimplify_case_label_expr (expr_p, pre_p);
8141           break;
8142
8143         case RETURN_EXPR:
8144           ret = gimplify_return_expr (*expr_p, pre_p);
8145           break;
8146
8147         case CONSTRUCTOR:
8148           /* Don't reduce this in place; let gimplify_init_constructor work its
8149              magic.  Buf if we're just elaborating this for side effects, just
8150              gimplify any element that has side-effects.  */
8151           if (fallback == fb_none)
8152             {
8153               unsigned HOST_WIDE_INT ix;
8154               tree val;
8155               tree temp = NULL_TREE;
8156               FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
8157                 if (TREE_SIDE_EFFECTS (val))
8158                   append_to_statement_list (val, &temp);
8159
8160               *expr_p = temp;
8161               ret = temp ? GS_OK : GS_ALL_DONE;
8162             }
8163           /* C99 code may assign to an array in a constructed
8164              structure or union, and this has undefined behavior only
8165              on execution, so create a temporary if an lvalue is
8166              required.  */
8167           else if (fallback == fb_lvalue)
8168             {
8169               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8170               mark_addressable (*expr_p);
8171               ret = GS_OK;
8172             }
8173           else
8174             ret = GS_ALL_DONE;
8175           break;
8176
8177           /* The following are special cases that are not handled by the
8178              original GIMPLE grammar.  */
8179
8180           /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
8181              eliminated.  */
8182         case SAVE_EXPR:
8183           ret = gimplify_save_expr (expr_p, pre_p, post_p);
8184           break;
8185
8186         case BIT_FIELD_REF:
8187           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8188                                post_p, is_gimple_lvalue, fb_either);
8189           recalculate_side_effects (*expr_p);
8190           break;
8191
8192         case TARGET_MEM_REF:
8193           {
8194             enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
8195
8196             if (TMR_BASE (*expr_p))
8197               r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
8198                                   post_p, is_gimple_mem_ref_addr, fb_either);
8199             if (TMR_INDEX (*expr_p))
8200               r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
8201                                   post_p, is_gimple_val, fb_rvalue);
8202             if (TMR_INDEX2 (*expr_p))
8203               r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
8204                                   post_p, is_gimple_val, fb_rvalue);
8205             /* TMR_STEP and TMR_OFFSET are always integer constants.  */
8206             ret = MIN (r0, r1);
8207           }
8208           break;
8209
8210         case NON_LVALUE_EXPR:
8211           /* This should have been stripped above.  */
8212           gcc_unreachable ();
8213
8214         case ASM_EXPR:
8215           ret = gimplify_asm_expr (expr_p, pre_p, post_p);
8216           break;
8217
8218         case TRY_FINALLY_EXPR:
8219         case TRY_CATCH_EXPR:
8220           {
8221             gimple_seq eval, cleanup;
8222             gtry *try_;
8223
8224             /* Calls to destructors are generated automatically in FINALLY/CATCH
8225                block. They should have location as UNKNOWN_LOCATION. However,
8226                gimplify_call_expr will reset these call stmts to input_location
8227                if it finds stmt's location is unknown. To prevent resetting for
8228                destructors, we set the input_location to unknown.
8229                Note that this only affects the destructor calls in FINALLY/CATCH
8230                block, and will automatically reset to its original value by the
8231                end of gimplify_expr.  */
8232             input_location = UNKNOWN_LOCATION;
8233             eval = cleanup = NULL;
8234             gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8235             gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8236             /* Don't create bogus GIMPLE_TRY with empty cleanup.  */
8237             if (gimple_seq_empty_p (cleanup))
8238               {
8239                 gimple_seq_add_seq (pre_p, eval);
8240                 ret = GS_ALL_DONE;
8241                 break;
8242               }
8243             try_ = gimple_build_try (eval, cleanup,
8244                                      TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8245                                      ? GIMPLE_TRY_FINALLY
8246                                      : GIMPLE_TRY_CATCH);
8247             if (EXPR_HAS_LOCATION (save_expr))
8248               gimple_set_location (try_, EXPR_LOCATION (save_expr));
8249             else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8250               gimple_set_location (try_, saved_location);
8251             if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8252               gimple_try_set_catch_is_cleanup (try_,
8253                                                TRY_CATCH_IS_CLEANUP (*expr_p));
8254             gimplify_seq_add_stmt (pre_p, try_);
8255             ret = GS_ALL_DONE;
8256             break;
8257           }
8258
8259         case CLEANUP_POINT_EXPR:
8260           ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8261           break;
8262
8263         case TARGET_EXPR:
8264           ret = gimplify_target_expr (expr_p, pre_p, post_p);
8265           break;
8266
8267         case CATCH_EXPR:
8268           {
8269             gimple c;
8270             gimple_seq handler = NULL;
8271             gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8272             c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8273             gimplify_seq_add_stmt (pre_p, c);
8274             ret = GS_ALL_DONE;
8275             break;
8276           }
8277
8278         case EH_FILTER_EXPR:
8279           {
8280             gimple ehf;
8281             gimple_seq failure = NULL;
8282
8283             gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8284             ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8285             gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8286             gimplify_seq_add_stmt (pre_p, ehf);
8287             ret = GS_ALL_DONE;
8288             break;
8289           }
8290
8291         case OBJ_TYPE_REF:
8292           {
8293             enum gimplify_status r0, r1;
8294             r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8295                                 post_p, is_gimple_val, fb_rvalue);
8296             r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8297                                 post_p, is_gimple_val, fb_rvalue);
8298             TREE_SIDE_EFFECTS (*expr_p) = 0;
8299             ret = MIN (r0, r1);
8300           }
8301           break;
8302
8303         case LABEL_DECL:
8304           /* We get here when taking the address of a label.  We mark
8305              the label as "forced"; meaning it can never be removed and
8306              it is a potential target for any computed goto.  */
8307           FORCED_LABEL (*expr_p) = 1;
8308           ret = GS_ALL_DONE;
8309           break;
8310
8311         case STATEMENT_LIST:
8312           ret = gimplify_statement_list (expr_p, pre_p);
8313           break;
8314
8315         case WITH_SIZE_EXPR:
8316           {
8317             gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8318                            post_p == &internal_post ? NULL : post_p,
8319                            gimple_test_f, fallback);
8320             gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8321                            is_gimple_val, fb_rvalue);
8322             ret = GS_ALL_DONE;
8323           }
8324           break;
8325
8326         case VAR_DECL:
8327         case PARM_DECL:
8328           ret = gimplify_var_or_parm_decl (expr_p);
8329           break;
8330
8331         case RESULT_DECL:
8332           /* When within an OMP context, notice uses of variables.  */
8333           if (gimplify_omp_ctxp)
8334             omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8335           ret = GS_ALL_DONE;
8336           break;
8337
8338         case SSA_NAME:
8339           /* Allow callbacks into the gimplifier during optimization.  */
8340           ret = GS_ALL_DONE;
8341           break;
8342
8343         case OMP_PARALLEL:
8344           gimplify_omp_parallel (expr_p, pre_p);
8345           ret = GS_ALL_DONE;
8346           break;
8347
8348         case OMP_TASK:
8349           gimplify_omp_task (expr_p, pre_p);
8350           ret = GS_ALL_DONE;
8351           break;
8352
8353         case OMP_FOR:
8354         case OMP_SIMD:
8355         case CILK_SIMD:
8356         case CILK_FOR:
8357         case OMP_DISTRIBUTE:
8358         case OACC_LOOP:
8359           ret = gimplify_omp_for (expr_p, pre_p);
8360           break;
8361
8362         case OACC_CACHE:
8363           gimplify_oacc_cache (expr_p, pre_p);
8364           ret = GS_ALL_DONE;
8365           break;
8366
8367         case OACC_HOST_DATA:
8368         case OACC_DECLARE:
8369           sorry ("directive not yet implemented");
8370           ret = GS_ALL_DONE;
8371           break;
8372
8373         case OACC_KERNELS:
8374           if (OACC_KERNELS_COMBINED (*expr_p))
8375             sorry ("directive not yet implemented");
8376           else
8377             gimplify_omp_workshare (expr_p, pre_p);
8378           ret = GS_ALL_DONE;
8379           break;
8380
8381         case OACC_PARALLEL:
8382           if (OACC_PARALLEL_COMBINED (*expr_p))
8383             sorry ("directive not yet implemented");
8384           else
8385             gimplify_omp_workshare (expr_p, pre_p);
8386           ret = GS_ALL_DONE;
8387           break;
8388
8389         case OACC_DATA:
8390         case OMP_SECTIONS:
8391         case OMP_SINGLE:
8392         case OMP_TARGET:
8393         case OMP_TARGET_DATA:
8394         case OMP_TEAMS:
8395           gimplify_omp_workshare (expr_p, pre_p);
8396           ret = GS_ALL_DONE;
8397           break;
8398
8399         case OACC_ENTER_DATA:
8400         case OACC_EXIT_DATA:
8401         case OACC_UPDATE:
8402         case OMP_TARGET_UPDATE:
8403           gimplify_omp_target_update (expr_p, pre_p);
8404           ret = GS_ALL_DONE;
8405           break;
8406
8407         case OMP_SECTION:
8408         case OMP_MASTER:
8409         case OMP_TASKGROUP:
8410         case OMP_ORDERED:
8411         case OMP_CRITICAL:
8412           {
8413             gimple_seq body = NULL;
8414             gimple g;
8415
8416             gimplify_and_add (OMP_BODY (*expr_p), &body);
8417             switch (TREE_CODE (*expr_p))
8418               {
8419               case OMP_SECTION:
8420                 g = gimple_build_omp_section (body);
8421                 break;
8422               case OMP_MASTER:
8423                 g = gimple_build_omp_master (body);
8424                 break;
8425               case OMP_TASKGROUP:
8426                 {
8427                   gimple_seq cleanup = NULL;
8428                   tree fn
8429                     = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8430                   g = gimple_build_call (fn, 0);
8431                   gimple_seq_add_stmt (&cleanup, g);
8432                   g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8433                   body = NULL;
8434                   gimple_seq_add_stmt (&body, g);
8435                   g = gimple_build_omp_taskgroup (body);
8436                 }
8437                 break;
8438               case OMP_ORDERED:
8439                 g = gimple_build_omp_ordered (body);
8440                 break;
8441               case OMP_CRITICAL:
8442                 g = gimple_build_omp_critical (body,
8443                                                OMP_CRITICAL_NAME (*expr_p));
8444                 break;
8445               default:
8446                 gcc_unreachable ();
8447               }
8448             gimplify_seq_add_stmt (pre_p, g);
8449             ret = GS_ALL_DONE;
8450             break;
8451           }
8452
8453         case OMP_ATOMIC:
8454         case OMP_ATOMIC_READ:
8455         case OMP_ATOMIC_CAPTURE_OLD:
8456         case OMP_ATOMIC_CAPTURE_NEW:
8457           ret = gimplify_omp_atomic (expr_p, pre_p);
8458           break;
8459
8460         case TRANSACTION_EXPR:
8461           ret = gimplify_transaction (expr_p, pre_p);
8462           break;
8463
8464         case TRUTH_AND_EXPR:
8465         case TRUTH_OR_EXPR:
8466         case TRUTH_XOR_EXPR:
8467           {
8468             tree orig_type = TREE_TYPE (*expr_p);
8469             tree new_type, xop0, xop1;
8470             *expr_p = gimple_boolify (*expr_p);
8471             new_type = TREE_TYPE (*expr_p);
8472             if (!useless_type_conversion_p (orig_type, new_type))
8473               {
8474                 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8475                 ret = GS_OK;
8476                 break;
8477               }
8478
8479           /* Boolified binary truth expressions are semantically equivalent
8480              to bitwise binary expressions.  Canonicalize them to the
8481              bitwise variant.  */
8482             switch (TREE_CODE (*expr_p))
8483               {
8484               case TRUTH_AND_EXPR:
8485                 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8486                 break;
8487               case TRUTH_OR_EXPR:
8488                 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8489                 break;
8490               case TRUTH_XOR_EXPR:
8491                 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8492                 break;
8493               default:
8494                 break;
8495               }
8496             /* Now make sure that operands have compatible type to
8497                expression's new_type.  */
8498             xop0 = TREE_OPERAND (*expr_p, 0);
8499             xop1 = TREE_OPERAND (*expr_p, 1);
8500             if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8501               TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8502                                                             new_type,
8503                                                             xop0);
8504             if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8505               TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8506                                                             new_type,
8507                                                             xop1);
8508             /* Continue classified as tcc_binary.  */
8509             goto expr_2;
8510           }
8511
8512         case FMA_EXPR:
8513         case VEC_COND_EXPR:
8514         case VEC_PERM_EXPR:
8515           /* Classified as tcc_expression.  */
8516           goto expr_3;
8517
8518         case POINTER_PLUS_EXPR:
8519           {
8520             enum gimplify_status r0, r1;
8521             r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8522                                 post_p, is_gimple_val, fb_rvalue);
8523             r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8524                                 post_p, is_gimple_val, fb_rvalue);
8525             recalculate_side_effects (*expr_p);
8526             ret = MIN (r0, r1);
8527             /* Convert &X + CST to invariant &MEM[&X, CST].  Do this
8528                after gimplifying operands - this is similar to how
8529                it would be folding all gimplified stmts on creation
8530                to have them canonicalized, which is what we eventually
8531                should do anyway.  */
8532             if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8533                 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8534               {
8535                 *expr_p = build_fold_addr_expr_with_type_loc
8536                    (input_location,
8537                     fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8538                                  TREE_OPERAND (*expr_p, 0),
8539                                  fold_convert (ptr_type_node,
8540                                                TREE_OPERAND (*expr_p, 1))),
8541                     TREE_TYPE (*expr_p));
8542                 ret = MIN (ret, GS_OK);
8543               }
8544             break;
8545           }
8546
8547         case CILK_SYNC_STMT:
8548           {
8549             if (!fn_contains_cilk_spawn_p (cfun))
8550               {
8551                 error_at (EXPR_LOCATION (*expr_p),
8552                           "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8553                 ret = GS_ERROR;
8554               }
8555             else
8556               {
8557                 gimplify_cilk_sync (expr_p, pre_p);
8558                 ret = GS_ALL_DONE;
8559               }
8560             break;
8561           }
8562         
8563         default:
8564           switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8565             {
8566             case tcc_comparison:
8567               /* Handle comparison of objects of non scalar mode aggregates
8568                  with a call to memcmp.  It would be nice to only have to do
8569                  this for variable-sized objects, but then we'd have to allow
8570                  the same nest of reference nodes we allow for MODIFY_EXPR and
8571                  that's too complex.
8572
8573                  Compare scalar mode aggregates as scalar mode values.  Using
8574                  memcmp for them would be very inefficient at best, and is
8575                  plain wrong if bitfields are involved.  */
8576                 {
8577                   tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8578
8579                   /* Vector comparisons need no boolification.  */
8580                   if (TREE_CODE (type) == VECTOR_TYPE)
8581                     goto expr_2;
8582                   else if (!AGGREGATE_TYPE_P (type))
8583                     {
8584                       tree org_type = TREE_TYPE (*expr_p);
8585                       *expr_p = gimple_boolify (*expr_p);
8586                       if (!useless_type_conversion_p (org_type,
8587                                                       TREE_TYPE (*expr_p)))
8588                         {
8589                           *expr_p = fold_convert_loc (input_location,
8590                                                       org_type, *expr_p);
8591                           ret = GS_OK;
8592                         }
8593                       else
8594                         goto expr_2;
8595                     }
8596                   else if (TYPE_MODE (type) != BLKmode)
8597                     ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8598                   else
8599                     ret = gimplify_variable_sized_compare (expr_p);
8600
8601                   break;
8602                 }
8603
8604             /* If *EXPR_P does not need to be special-cased, handle it
8605                according to its class.  */
8606             case tcc_unary:
8607               ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8608                                    post_p, is_gimple_val, fb_rvalue);
8609               break;
8610
8611             case tcc_binary:
8612             expr_2:
8613               {
8614                 enum gimplify_status r0, r1;
8615
8616                 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8617                                     post_p, is_gimple_val, fb_rvalue);
8618                 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8619                                     post_p, is_gimple_val, fb_rvalue);
8620
8621                 ret = MIN (r0, r1);
8622                 break;
8623               }
8624
8625             expr_3:
8626               {
8627                 enum gimplify_status r0, r1, r2;
8628
8629                 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8630                                     post_p, is_gimple_val, fb_rvalue);
8631                 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8632                                     post_p, is_gimple_val, fb_rvalue);
8633                 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8634                                     post_p, is_gimple_val, fb_rvalue);
8635
8636                 ret = MIN (MIN (r0, r1), r2);
8637                 break;
8638               }
8639
8640             case tcc_declaration:
8641             case tcc_constant:
8642               ret = GS_ALL_DONE;
8643               goto dont_recalculate;
8644
8645             default:
8646               gcc_unreachable ();
8647             }
8648
8649           recalculate_side_effects (*expr_p);
8650
8651         dont_recalculate:
8652           break;
8653         }
8654
8655       gcc_assert (*expr_p || ret != GS_OK);
8656     }
8657   while (ret == GS_OK);
8658
8659   /* If we encountered an error_mark somewhere nested inside, either
8660      stub out the statement or propagate the error back out.  */
8661   if (ret == GS_ERROR)
8662     {
8663       if (is_statement)
8664         *expr_p = NULL;
8665       goto out;
8666     }
8667
8668   /* This was only valid as a return value from the langhook, which
8669      we handled.  Make sure it doesn't escape from any other context.  */
8670   gcc_assert (ret != GS_UNHANDLED);
8671
8672   if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8673     {
8674       /* We aren't looking for a value, and we don't have a valid
8675          statement.  If it doesn't have side-effects, throw it away.  */
8676       if (!TREE_SIDE_EFFECTS (*expr_p))
8677         *expr_p = NULL;
8678       else if (!TREE_THIS_VOLATILE (*expr_p))
8679         {
8680           /* This is probably a _REF that contains something nested that
8681              has side effects.  Recurse through the operands to find it.  */
8682           enum tree_code code = TREE_CODE (*expr_p);
8683
8684           switch (code)
8685             {
8686             case COMPONENT_REF:
8687             case REALPART_EXPR:
8688             case IMAGPART_EXPR:
8689             case VIEW_CONVERT_EXPR:
8690               gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8691                              gimple_test_f, fallback);
8692               break;
8693
8694             case ARRAY_REF:
8695             case ARRAY_RANGE_REF:
8696               gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8697                              gimple_test_f, fallback);
8698               gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8699                              gimple_test_f, fallback);
8700               break;
8701
8702             default:
8703                /* Anything else with side-effects must be converted to
8704                   a valid statement before we get here.  */
8705               gcc_unreachable ();
8706             }
8707
8708           *expr_p = NULL;
8709         }
8710       else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8711                && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8712         {
8713           /* Historically, the compiler has treated a bare reference
8714              to a non-BLKmode volatile lvalue as forcing a load.  */
8715           tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8716
8717           /* Normally, we do not want to create a temporary for a
8718              TREE_ADDRESSABLE type because such a type should not be
8719              copied by bitwise-assignment.  However, we make an
8720              exception here, as all we are doing here is ensuring that
8721              we read the bytes that make up the type.  We use
8722              create_tmp_var_raw because create_tmp_var will abort when
8723              given a TREE_ADDRESSABLE type.  */
8724           tree tmp = create_tmp_var_raw (type, "vol");
8725           gimple_add_tmp_var (tmp);
8726           gimplify_assign (tmp, *expr_p, pre_p);
8727           *expr_p = NULL;
8728         }
8729       else
8730         /* We can't do anything useful with a volatile reference to
8731            an incomplete type, so just throw it away.  Likewise for
8732            a BLKmode type, since any implicit inner load should
8733            already have been turned into an explicit one by the
8734            gimplification process.  */
8735         *expr_p = NULL;
8736     }
8737
8738   /* If we are gimplifying at the statement level, we're done.  Tack
8739      everything together and return.  */
8740   if (fallback == fb_none || is_statement)
8741     {
8742       /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8743          it out for GC to reclaim it.  */
8744       *expr_p = NULL_TREE;
8745
8746       if (!gimple_seq_empty_p (internal_pre)
8747           || !gimple_seq_empty_p (internal_post))
8748         {
8749           gimplify_seq_add_seq (&internal_pre, internal_post);
8750           gimplify_seq_add_seq (pre_p, internal_pre);
8751         }
8752
8753       /* The result of gimplifying *EXPR_P is going to be the last few
8754          statements in *PRE_P and *POST_P.  Add location information
8755          to all the statements that were added by the gimplification
8756          helpers.  */
8757       if (!gimple_seq_empty_p (*pre_p))
8758         annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8759
8760       if (!gimple_seq_empty_p (*post_p))
8761         annotate_all_with_location_after (*post_p, post_last_gsi,
8762                                           input_location);
8763
8764       goto out;
8765     }
8766
8767 #ifdef ENABLE_GIMPLE_CHECKING
8768   if (*expr_p)
8769     {
8770       enum tree_code code = TREE_CODE (*expr_p);
8771       /* These expressions should already be in gimple IR form.  */
8772       gcc_assert (code != MODIFY_EXPR
8773                   && code != ASM_EXPR
8774                   && code != BIND_EXPR
8775                   && code != CATCH_EXPR
8776                   && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8777                   && code != EH_FILTER_EXPR
8778                   && code != GOTO_EXPR
8779                   && code != LABEL_EXPR
8780                   && code != LOOP_EXPR
8781                   && code != SWITCH_EXPR
8782                   && code != TRY_FINALLY_EXPR
8783                   && code != OACC_PARALLEL
8784                   && code != OACC_KERNELS
8785                   && code != OACC_DATA
8786                   && code != OACC_HOST_DATA
8787                   && code != OACC_DECLARE
8788                   && code != OACC_UPDATE
8789                   && code != OACC_ENTER_DATA
8790                   && code != OACC_EXIT_DATA
8791                   && code != OACC_CACHE
8792                   && code != OMP_CRITICAL
8793                   && code != OMP_FOR
8794                   && code != OACC_LOOP
8795                   && code != OMP_MASTER
8796                   && code != OMP_TASKGROUP
8797                   && code != OMP_ORDERED
8798                   && code != OMP_PARALLEL
8799                   && code != OMP_SECTIONS
8800                   && code != OMP_SECTION
8801                   && code != OMP_SINGLE);
8802     }
8803 #endif
8804
8805   /* Otherwise we're gimplifying a subexpression, so the resulting
8806      value is interesting.  If it's a valid operand that matches
8807      GIMPLE_TEST_F, we're done. Unless we are handling some
8808      post-effects internally; if that's the case, we need to copy into
8809      a temporary before adding the post-effects to POST_P.  */
8810   if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8811     goto out;
8812
8813   /* Otherwise, we need to create a new temporary for the gimplified
8814      expression.  */
8815
8816   /* We can't return an lvalue if we have an internal postqueue.  The
8817      object the lvalue refers to would (probably) be modified by the
8818      postqueue; we need to copy the value out first, which means an
8819      rvalue.  */
8820   if ((fallback & fb_lvalue)
8821       && gimple_seq_empty_p (internal_post)
8822       && is_gimple_addressable (*expr_p))
8823     {
8824       /* An lvalue will do.  Take the address of the expression, store it
8825          in a temporary, and replace the expression with an INDIRECT_REF of
8826          that temporary.  */
8827       tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8828       gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8829       *expr_p = build_simple_mem_ref (tmp);
8830     }
8831   else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8832     {
8833       /* An rvalue will do.  Assign the gimplified expression into a
8834          new temporary TMP and replace the original expression with
8835          TMP.  First, make sure that the expression has a type so that
8836          it can be assigned into a temporary.  */
8837       gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8838       *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8839     }
8840   else
8841     {
8842 #ifdef ENABLE_GIMPLE_CHECKING
8843       if (!(fallback & fb_mayfail))
8844         {
8845           fprintf (stderr, "gimplification failed:\n");
8846           print_generic_expr (stderr, *expr_p, 0);
8847           debug_tree (*expr_p);
8848           internal_error ("gimplification failed");
8849         }
8850 #endif
8851       gcc_assert (fallback & fb_mayfail);
8852
8853       /* If this is an asm statement, and the user asked for the
8854          impossible, don't die.  Fail and let gimplify_asm_expr
8855          issue an error.  */
8856       ret = GS_ERROR;
8857       goto out;
8858     }
8859
8860   /* Make sure the temporary matches our predicate.  */
8861   gcc_assert ((*gimple_test_f) (*expr_p));
8862
8863   if (!gimple_seq_empty_p (internal_post))
8864     {
8865       annotate_all_with_location (internal_post, input_location);
8866       gimplify_seq_add_seq (pre_p, internal_post);
8867     }
8868
8869  out:
8870   input_location = saved_location;
8871   return ret;
8872 }
8873
8874 /* Look through TYPE for variable-sized objects and gimplify each such
8875    size that we find.  Add to LIST_P any statements generated.  */
8876
8877 void
8878 gimplify_type_sizes (tree type, gimple_seq *list_p)
8879 {
8880   tree field, t;
8881
8882   if (type == NULL || type == error_mark_node)
8883     return;
8884
8885   /* We first do the main variant, then copy into any other variants.  */
8886   type = TYPE_MAIN_VARIANT (type);
8887
8888   /* Avoid infinite recursion.  */
8889   if (TYPE_SIZES_GIMPLIFIED (type))
8890     return;
8891
8892   TYPE_SIZES_GIMPLIFIED (type) = 1;
8893
8894   switch (TREE_CODE (type))
8895     {
8896     case INTEGER_TYPE:
8897     case ENUMERAL_TYPE:
8898     case BOOLEAN_TYPE:
8899     case REAL_TYPE:
8900     case FIXED_POINT_TYPE:
8901       gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8902       gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8903
8904       for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8905         {
8906           TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8907           TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8908         }
8909       break;
8910
8911     case ARRAY_TYPE:
8912       /* These types may not have declarations, so handle them here.  */
8913       gimplify_type_sizes (TREE_TYPE (type), list_p);
8914       gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8915       /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8916          with assigned stack slots, for -O1+ -g they should be tracked
8917          by VTA.  */
8918       if (!(TYPE_NAME (type)
8919             && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8920             && DECL_IGNORED_P (TYPE_NAME (type)))
8921           && TYPE_DOMAIN (type)
8922           && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8923         {
8924           t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8925           if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8926             DECL_IGNORED_P (t) = 0;
8927           t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8928           if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8929             DECL_IGNORED_P (t) = 0;
8930         }
8931       break;
8932
8933     case RECORD_TYPE:
8934     case UNION_TYPE:
8935     case QUAL_UNION_TYPE:
8936       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8937         if (TREE_CODE (field) == FIELD_DECL)
8938           {
8939             gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8940             gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8941             gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8942             gimplify_type_sizes (TREE_TYPE (field), list_p);
8943           }
8944       break;
8945
8946     case POINTER_TYPE:
8947     case REFERENCE_TYPE:
8948         /* We used to recurse on the pointed-to type here, which turned out to
8949            be incorrect because its definition might refer to variables not
8950            yet initialized at this point if a forward declaration is involved.
8951
8952            It was actually useful for anonymous pointed-to types to ensure
8953            that the sizes evaluation dominates every possible later use of the
8954            values.  Restricting to such types here would be safe since there
8955            is no possible forward declaration around, but would introduce an
8956            undesirable middle-end semantic to anonymity.  We then defer to
8957            front-ends the responsibility of ensuring that the sizes are
8958            evaluated both early and late enough, e.g. by attaching artificial
8959            type declarations to the tree.  */
8960       break;
8961
8962     default:
8963       break;
8964     }
8965
8966   gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8967   gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8968
8969   for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8970     {
8971       TYPE_SIZE (t) = TYPE_SIZE (type);
8972       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8973       TYPE_SIZES_GIMPLIFIED (t) = 1;
8974     }
8975 }
8976
8977 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8978    a size or position, has had all of its SAVE_EXPRs evaluated.
8979    We add any required statements to *STMT_P.  */
8980
8981 void
8982 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8983 {
8984   tree expr = *expr_p;
8985
8986   /* We don't do anything if the value isn't there, is constant, or contains
8987      A PLACEHOLDER_EXPR.  We also don't want to do anything if it's already
8988      a VAR_DECL.  If it's a VAR_DECL from another function, the gimplifier
8989      will want to replace it with a new variable, but that will cause problems
8990      if this type is from outside the function.  It's OK to have that here.  */
8991   if (is_gimple_sizepos (expr))
8992     return;
8993
8994   *expr_p = unshare_expr (expr);
8995
8996   gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8997 }
8998
8999 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
9000    containing the sequence of corresponding GIMPLE statements.  If DO_PARMS
9001    is true, also gimplify the parameters.  */
9002
9003 gbind *
9004 gimplify_body (tree fndecl, bool do_parms)
9005 {
9006   location_t saved_location = input_location;
9007   gimple_seq parm_stmts, seq;
9008   gimple outer_stmt;
9009   gbind *outer_bind;
9010   struct cgraph_node *cgn;
9011
9012   timevar_push (TV_TREE_GIMPLIFY);
9013
9014   /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
9015      gimplification.  */
9016   default_rtl_profile ();
9017
9018   gcc_assert (gimplify_ctxp == NULL);
9019   push_gimplify_context ();
9020
9021   if (flag_openacc || flag_openmp)
9022     {
9023       gcc_assert (gimplify_omp_ctxp == NULL);
9024       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
9025         gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
9026     }
9027
9028   /* Unshare most shared trees in the body and in that of any nested functions.
9029      It would seem we don't have to do this for nested functions because
9030      they are supposed to be output and then the outer function gimplified
9031      first, but the g++ front end doesn't always do it that way.  */
9032   unshare_body (fndecl);
9033   unvisit_body (fndecl);
9034
9035   cgn = cgraph_node::get (fndecl);
9036   if (cgn && cgn->origin)
9037     nonlocal_vlas = new hash_set<tree>;
9038
9039   /* Make sure input_location isn't set to something weird.  */
9040   input_location = DECL_SOURCE_LOCATION (fndecl);
9041
9042   /* Resolve callee-copies.  This has to be done before processing
9043      the body so that DECL_VALUE_EXPR gets processed correctly.  */
9044   parm_stmts = do_parms ? gimplify_parameters () : NULL;
9045
9046   /* Gimplify the function's body.  */
9047   seq = NULL;
9048   gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
9049   outer_stmt = gimple_seq_first_stmt (seq);
9050   if (!outer_stmt)
9051     {
9052       outer_stmt = gimple_build_nop ();
9053       gimplify_seq_add_stmt (&seq, outer_stmt);
9054     }
9055
9056   /* The body must contain exactly one statement, a GIMPLE_BIND.  If this is
9057      not the case, wrap everything in a GIMPLE_BIND to make it so.  */
9058   if (gimple_code (outer_stmt) == GIMPLE_BIND
9059       && gimple_seq_first (seq) == gimple_seq_last (seq))
9060     outer_bind = as_a <gbind *> (outer_stmt);
9061   else
9062     outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
9063
9064   DECL_SAVED_TREE (fndecl) = NULL_TREE;
9065
9066   /* If we had callee-copies statements, insert them at the beginning
9067      of the function and clear DECL_VALUE_EXPR_P on the parameters.  */
9068   if (!gimple_seq_empty_p (parm_stmts))
9069     {
9070       tree parm;
9071
9072       gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
9073       gimple_bind_set_body (outer_bind, parm_stmts);
9074
9075       for (parm = DECL_ARGUMENTS (current_function_decl);
9076            parm; parm = DECL_CHAIN (parm))
9077         if (DECL_HAS_VALUE_EXPR_P (parm))
9078           {
9079             DECL_HAS_VALUE_EXPR_P (parm) = 0;
9080             DECL_IGNORED_P (parm) = 0;
9081           }
9082     }
9083
9084   if (nonlocal_vlas)
9085     {
9086       if (nonlocal_vla_vars)
9087         {
9088           /* tree-nested.c may later on call declare_vars (..., true);
9089              which relies on BLOCK_VARS chain to be the tail of the
9090              gimple_bind_vars chain.  Ensure we don't violate that
9091              assumption.  */
9092           if (gimple_bind_block (outer_bind)
9093               == DECL_INITIAL (current_function_decl))
9094             declare_vars (nonlocal_vla_vars, outer_bind, true);
9095           else
9096             BLOCK_VARS (DECL_INITIAL (current_function_decl))
9097               = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
9098                          nonlocal_vla_vars);
9099           nonlocal_vla_vars = NULL_TREE;
9100         }
9101       delete nonlocal_vlas;
9102       nonlocal_vlas = NULL;
9103     }
9104
9105   if ((flag_openacc || flag_openmp || flag_openmp_simd)
9106       && gimplify_omp_ctxp)
9107     {
9108       delete_omp_context (gimplify_omp_ctxp);
9109       gimplify_omp_ctxp = NULL;
9110     }
9111
9112   pop_gimplify_context (outer_bind);
9113   gcc_assert (gimplify_ctxp == NULL);
9114
9115 #ifdef ENABLE_CHECKING
9116   if (!seen_error ())
9117     verify_gimple_in_seq (gimple_bind_body (outer_bind));
9118 #endif
9119
9120   timevar_pop (TV_TREE_GIMPLIFY);
9121   input_location = saved_location;
9122
9123   return outer_bind;
9124 }
9125
9126 typedef char *char_p; /* For DEF_VEC_P.  */
9127
9128 /* Return whether we should exclude FNDECL from instrumentation.  */
9129
9130 static bool
9131 flag_instrument_functions_exclude_p (tree fndecl)
9132 {
9133   vec<char_p> *v;
9134
9135   v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
9136   if (v && v->length () > 0)
9137     {
9138       const char *name;
9139       int i;
9140       char *s;
9141
9142       name = lang_hooks.decl_printable_name (fndecl, 0);
9143       FOR_EACH_VEC_ELT (*v, i, s)
9144         if (strstr (name, s) != NULL)
9145           return true;
9146     }
9147
9148   v = (vec<char_p> *) flag_instrument_functions_exclude_files;
9149   if (v && v->length () > 0)
9150     {
9151       const char *name;
9152       int i;
9153       char *s;
9154
9155       name = DECL_SOURCE_FILE (fndecl);
9156       FOR_EACH_VEC_ELT (*v, i, s)
9157         if (strstr (name, s) != NULL)
9158           return true;
9159     }
9160
9161   return false;
9162 }
9163
9164 /* Entry point to the gimplification pass.  FNDECL is the FUNCTION_DECL
9165    node for the function we want to gimplify.
9166
9167    Return the sequence of GIMPLE statements corresponding to the body
9168    of FNDECL.  */
9169
9170 void
9171 gimplify_function_tree (tree fndecl)
9172 {
9173   tree parm, ret;
9174   gimple_seq seq;
9175   gbind *bind;
9176
9177   gcc_assert (!gimple_body (fndecl));
9178
9179   if (DECL_STRUCT_FUNCTION (fndecl))
9180     push_cfun (DECL_STRUCT_FUNCTION (fndecl));
9181   else
9182     push_struct_function (fndecl);
9183
9184   for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
9185     {
9186       /* Preliminarily mark non-addressed complex variables as eligible
9187          for promotion to gimple registers.  We'll transform their uses
9188          as we find them.  */
9189       if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
9190            || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
9191           && !TREE_THIS_VOLATILE (parm)
9192           && !needs_to_live_in_memory (parm))
9193         DECL_GIMPLE_REG_P (parm) = 1;
9194     }
9195
9196   ret = DECL_RESULT (fndecl);
9197   if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
9198        || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
9199       && !needs_to_live_in_memory (ret))
9200     DECL_GIMPLE_REG_P (ret) = 1;
9201
9202   bind = gimplify_body (fndecl, true);
9203
9204   /* The tree body of the function is no longer needed, replace it
9205      with the new GIMPLE body.  */
9206   seq = NULL;
9207   gimple_seq_add_stmt (&seq, bind);
9208   gimple_set_body (fndecl, seq);
9209
9210   /* If we're instrumenting function entry/exit, then prepend the call to
9211      the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
9212      catch the exit hook.  */
9213   /* ??? Add some way to ignore exceptions for this TFE.  */
9214   if (flag_instrument_function_entry_exit
9215       && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
9216       && !flag_instrument_functions_exclude_p (fndecl))
9217     {
9218       tree x;
9219       gbind *new_bind;
9220       gimple tf;
9221       gimple_seq cleanup = NULL, body = NULL;
9222       tree tmp_var;
9223       gcall *call;
9224
9225       x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9226       call = gimple_build_call (x, 1, integer_zero_node);
9227       tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9228       gimple_call_set_lhs (call, tmp_var);
9229       gimplify_seq_add_stmt (&cleanup, call);
9230       x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
9231       call = gimple_build_call (x, 2,
9232                                 build_fold_addr_expr (current_function_decl),
9233                                 tmp_var);
9234       gimplify_seq_add_stmt (&cleanup, call);
9235       tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
9236
9237       x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9238       call = gimple_build_call (x, 1, integer_zero_node);
9239       tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9240       gimple_call_set_lhs (call, tmp_var);
9241       gimplify_seq_add_stmt (&body, call);
9242       x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
9243       call = gimple_build_call (x, 2,
9244                                 build_fold_addr_expr (current_function_decl),
9245                                 tmp_var);
9246       gimplify_seq_add_stmt (&body, call);
9247       gimplify_seq_add_stmt (&body, tf);
9248       new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
9249       /* Clear the block for BIND, since it is no longer directly inside
9250          the function, but within a try block.  */
9251       gimple_bind_set_block (bind, NULL);
9252
9253       /* Replace the current function body with the body
9254          wrapped in the try/finally TF.  */
9255       seq = NULL;
9256       gimple_seq_add_stmt (&seq, new_bind);
9257       gimple_set_body (fndecl, seq);
9258       bind = new_bind;
9259     }
9260
9261   if ((flag_sanitize & SANITIZE_THREAD) != 0
9262       && !lookup_attribute ("no_sanitize_thread", DECL_ATTRIBUTES (fndecl)))
9263     {
9264       gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
9265       gimple tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
9266       gbind *new_bind = gimple_build_bind (NULL, tf, gimple_bind_block (bind));
9267       /* Clear the block for BIND, since it is no longer directly inside
9268          the function, but within a try block.  */
9269       gimple_bind_set_block (bind, NULL);
9270       /* Replace the current function body with the body
9271          wrapped in the try/finally TF.  */
9272       seq = NULL;
9273       gimple_seq_add_stmt (&seq, new_bind);
9274       gimple_set_body (fndecl, seq);
9275     }
9276
9277   DECL_SAVED_TREE (fndecl) = NULL_TREE;
9278   cfun->curr_properties = PROP_gimple_any;
9279
9280   pop_cfun ();
9281 }
9282
9283 /* Return a dummy expression of type TYPE in order to keep going after an
9284    error.  */
9285
9286 static tree
9287 dummy_object (tree type)
9288 {
9289   tree t = build_int_cst (build_pointer_type (type), 0);
9290   return build2 (MEM_REF, type, t, t);
9291 }
9292
9293 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9294    builtin function, but a very special sort of operator.  */
9295
9296 enum gimplify_status
9297 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9298 {
9299   tree promoted_type, have_va_type;
9300   tree valist = TREE_OPERAND (*expr_p, 0);
9301   tree type = TREE_TYPE (*expr_p);
9302   tree t;
9303   location_t loc = EXPR_LOCATION (*expr_p);
9304
9305   /* Verify that valist is of the proper type.  */
9306   have_va_type = TREE_TYPE (valist);
9307   if (have_va_type == error_mark_node)
9308     return GS_ERROR;
9309   have_va_type = targetm.canonical_va_list_type (have_va_type);
9310
9311   if (have_va_type == NULL_TREE)
9312     {
9313       error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9314       return GS_ERROR;
9315     }
9316
9317   /* Generate a diagnostic for requesting data of a type that cannot
9318      be passed through `...' due to type promotion at the call site.  */
9319   if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9320            != type)
9321     {
9322       static bool gave_help;
9323       bool warned;
9324
9325       /* Unfortunately, this is merely undefined, rather than a constraint
9326          violation, so we cannot make this an error.  If this call is never
9327          executed, the program is still strictly conforming.  */
9328       warned = warning_at (loc, 0,
9329                            "%qT is promoted to %qT when passed through %<...%>",
9330                            type, promoted_type);
9331       if (!gave_help && warned)
9332         {
9333           gave_help = true;
9334           inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9335                   promoted_type, type);
9336         }
9337
9338       /* We can, however, treat "undefined" any way we please.
9339          Call abort to encourage the user to fix the program.  */
9340       if (warned)
9341         inform (loc, "if this code is reached, the program will abort");
9342       /* Before the abort, allow the evaluation of the va_list
9343          expression to exit or longjmp.  */
9344       gimplify_and_add (valist, pre_p);
9345       t = build_call_expr_loc (loc,
9346                                builtin_decl_implicit (BUILT_IN_TRAP), 0);
9347       gimplify_and_add (t, pre_p);
9348
9349       /* This is dead code, but go ahead and finish so that the
9350          mode of the result comes out right.  */
9351       *expr_p = dummy_object (type);
9352       return GS_ALL_DONE;
9353     }
9354   else
9355     {
9356       /* Make it easier for the backends by protecting the valist argument
9357          from multiple evaluations.  */
9358       if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9359         {
9360           /* For this case, the backends will be expecting a pointer to
9361              TREE_TYPE (abi), but it's possible we've
9362              actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9363              So fix it.  */
9364           if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9365             {
9366               tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9367               valist = fold_convert_loc (loc, p1,
9368                                          build_fold_addr_expr_loc (loc, valist));
9369             }
9370
9371           gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9372         }
9373       else
9374         gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9375
9376       if (!targetm.gimplify_va_arg_expr)
9377         /* FIXME: Once most targets are converted we should merely
9378            assert this is non-null.  */
9379         return GS_ALL_DONE;
9380
9381       *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9382       return GS_OK;
9383     }
9384 }
9385
9386 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9387
9388    DST/SRC are the destination and source respectively.  You can pass
9389    ungimplified trees in DST or SRC, in which case they will be
9390    converted to a gimple operand if necessary.
9391
9392    This function returns the newly created GIMPLE_ASSIGN tuple.  */
9393
9394 gimple
9395 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9396 {
9397   tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9398   gimplify_and_add (t, seq_p);
9399   ggc_free (t);
9400   return gimple_seq_last_stmt (*seq_p);
9401 }
9402
9403 inline hashval_t
9404 gimplify_hasher::hash (const value_type *p)
9405 {
9406   tree t = p->val;
9407   return iterative_hash_expr (t, 0);
9408 }
9409
9410 inline bool
9411 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9412 {
9413   tree t1 = p1->val;
9414   tree t2 = p2->val;
9415   enum tree_code code = TREE_CODE (t1);
9416
9417   if (TREE_CODE (t2) != code
9418       || TREE_TYPE (t1) != TREE_TYPE (t2))
9419     return false;
9420
9421   if (!operand_equal_p (t1, t2, 0))
9422     return false;
9423
9424 #ifdef ENABLE_CHECKING
9425   /* Only allow them to compare equal if they also hash equal; otherwise
9426      results are nondeterminate, and we fail bootstrap comparison.  */
9427   gcc_assert (hash (p1) == hash (p2));
9428 #endif
9429
9430   return true;
9431 }