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