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