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