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